Source code for vak.prep.spectrogram_dataset.spect
"""functions for making spectrogramfilters adapted from SciPy cookbookhttps://scipy-cookbook.readthedocs.io/items/ButterworthBandpass.htmlspectrogram adapted from code by Kyle Kastner and Tim Sainburghttps://github.com/timsainb/python_spectrograms_and_inversion"""importnumpyasnpfrommatplotlib.mlabimportspecgramfromscipy.signalimportbutter,lfilter
[docs]defspectrogram(dat,samp_freq,fft_size=512,step_size=64,thresh=None,transform_type=None,freq_cutoffs=None,):"""creates a spectrogram Parameters ---------- dat : numpy.ndarray audio signal samp_freq : int sampling frequency in Hz fft_size : int size of window for Fast Fourier transform, number of time bins. step_size : int step size for Fast Fourier transform transform_type : str one of {'log_spect', 'log_spect_plus_one'}. 'log_spect' transforms the spectrogram to log(spectrogram), and 'log_spect_plus_one' does the same thing but adds one to each element. Default is None. If None, no transform is applied. thresh: int threshold minimum power for log spectrogram freq_cutoffs : tuple of two elements, lower and higher frequencies. Return ------ spect : numpy.ndarray spectrogram freqbins : numpy.ndarray vector of centers of frequency bins from spectrogram timebins : numpy.ndarray vector of centers of time bins from spectrogram """noverlap=fft_size-step_sizeiffreq_cutoffs:dat=butter_bandpass_filter(dat,freq_cutoffs[0],freq_cutoffs[1],samp_freq)# below only take [:3] from return of specgram because we don't need the imagespect,freqbins,timebins=specgram(dat,fft_size,samp_freq,noverlap=noverlap)[:3]iftransform_type:iftransform_type=="log_spect":spect/=spect.max()# volume normalize to max 1spect=np.log10(spect)# take logifthresh:# I know this is weird, maintaining 'legacy' behaviorspect[spect<-thresh]=-thresheliftransform_type=="log_spect_plus_one":spect=np.log10(spect+1)ifthresh:spect[spect<thresh]=threshelse:ifthresh:spect[spect<thresh]=(thresh# set anything less than the threshold as the threshold)iffreq_cutoffs:f_inds=np.nonzero((freqbins>=freq_cutoffs[0])&(freqbins<freq_cutoffs[1]))[0]# returns tuplespect=spect[f_inds,:]freqbins=freqbins[f_inds]returnspect,freqbins,timebins