<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> def find_thres ( spectrogram , percentile , base ) : <EOL> "<STR_LIT>" <EOL> dim = spectrogram . shape <EOL> window = spectrogram [ <NUM_LIT:0> : dim [ <NUM_LIT:0> ] , base : dim [ <NUM_LIT:1> ] ] <EOL> threshold = np . percentile ( window , percentile ) <EOL> return threshold <EOL> def peak_pick ( S , f_dim1 , t_dim1 , f_dim2 , t_dim2 , threshold , base ) : <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> a = len ( S ) <EOL> b = len ( S [ <NUM_LIT:1> ] ) <EOL> peaks = [ ] <EOL> t_coords = [ ] <EOL> f_coords = [ ] <EOL> "<STR_LIT>" <EOL> for i in range ( <NUM_LIT:0> , a , t_dim1 ) : <EOL> for j in range ( base , b , f_dim1 ) : <EOL> if i + t_dim1 < a and j + f_dim1 < b : <EOL> window = S [ i : i + t_dim1 , j : j + f_dim1 ] <EOL> elif i + t_dim1 < a and j + f_dim1 >= b : <EOL> window = S [ i : i + t_dim1 , j : b ] <EOL> elif i + t_dim1 >= a and j + f_dim1 < b : <EOL> window = S [ i : a , j : j + f_dim1 ] <EOL> else : <EOL> window = S [ i : a , j : b ] <EOL> "<STR_LIT>" <EOL> if np . amax ( window ) >= threshold : <EOL> row , col = np . unravel_index ( np . argmax ( window ) , window . shape ) <EOL> t_coords . append ( i + row ) <EOL> f_coords . append ( j + col ) <EOL> "<STR_LIT>" <EOL> for k in range ( <NUM_LIT:0> , len ( f_coords ) ) : <EOL> fmin = f_coords [ k ] - f_dim2 <EOL> fmax = f_coords [ k ] + f_dim2 <EOL> tmin = t_coords [ k ] - t_dim2 <EOL> tmax = t_coords [ k ] + t_dim2 <EOL> if fmin < base : <EOL> fmin = base <EOL> if fmax > b : <EOL> fmax = b <EOL> if tmin < <NUM_LIT:0> : <EOL> tmin = <NUM_LIT:0> <EOL> if tmax > a : <EOL> tmax = a <EOL> window = S [ tmin : tmax , fmin : fmax ] <EOL> "<STR_LIT>" <EOL> if not window . size : <EOL> continue <EOL> "<STR_LIT>" <EOL> if S [ t_coords [ k ] , f_coords [ k ] ] < np . amax ( window ) : <EOL> t_coords [ k ] = - <NUM_LIT:1> <EOL> f_coords [ k ] = - <NUM_LIT:1> <EOL> "<STR_LIT>" <EOL> f_coords [ : ] = ( value for value in f_coords if value != - <NUM_LIT:1> ) <EOL> t_coords [ : ] = ( value for value in t_coords if value != - <NUM_LIT:1> ) <EOL> for x in range ( <NUM_LIT:0> , len ( f_coords ) ) : <EOL> peaks . append ( ( t_coords [ x ] , f_coords [ x ] , S [ t_coords [ x ] , f_coords [ x ] ] ) ) <EOL> return peaks <EOL> def reduce_peaks ( peaks , fftsize , high_peak_threshold , low_peak_threshold ) : <EOL> "<STR_LIT>" <EOL> low_peaks = [ ] <EOL> high_peaks = [ ] <EOL> for item in peaks : <EOL> if ( item [ <NUM_LIT:1> ] > ( fftsize / <NUM_LIT:4> ) ) : <EOL> high_peaks . append ( item ) <EOL> else : <EOL> low_peaks . append ( item ) <EOL> reduced_peaks = [ ] <EOL> for item in peaks : <EOL> if ( item [ <NUM_LIT:1> ] > ( fftsize / <NUM_LIT:4> ) ) : <EOL> if ( item [ <NUM_LIT:2> ] > np . percentile ( high_peaks , high_peak_threshold , axis = <NUM_LIT:0> ) [ <NUM_LIT:2> ] ) : <EOL> reduced_peaks . append ( item ) <EOL> else : <EOL> continue <EOL> else : <EOL> if ( item [ <NUM_LIT:2> ] > np . percentile ( low_peaks , low_peak_threshold , axis = <NUM_LIT:0> ) [ <NUM_LIT:2> ] ) : <EOL> reduced_peaks . append ( item ) <EOL> else : <EOL> continue <EOL> return reduced_peaks </s>
