def get_amplitude_envelope(data, fs=30000.0, lowpass=8000.0, highpass=1000.0, rectify_lowpass=600.0, spec_sample_rate=200.0, spec_freq_spacing=200.0, mode="broadband"): """Compute an amplitude envelope of a signal This can be done in two modes: "broadband" or "max_zscore" Broadband mode is the normal amplitude envelope calcualtion. In broadband mode, the signal is bandpass filtered, rectified, and then lowpass filtered. Max Zscore mode is useful to detect calls which may be more localized in the frequency domain from background (white) noise. In max_zscore mode, a spectrogram is computed with spec_sample_rate time bins and spec_freq_spacing frequency bins. For each time bin, the power in each frequency bin is zscored and the max zscored value is assigned to the envelope for that bin. """ spectral = True filtered = highpass_filter(data.T, fs, highpass, filter_order=10).T filtered = lowpass_filter(filtered.T, fs, lowpass, filter_order=10).T if mode == "max_zscore": t_spec, f_spec, spec, _ = spectrogram( filtered, fs, spec_sample_rate=spec_sample_rate, freq_spacing=spec_freq_spacing, cmplx=False) spec = np.abs(spec) std = np.std(spec, axis=0) zscored = (spec - np.mean(spec, axis=0)) / std filtered = np.max(zscored, axis=0) filtered -= np.min(filtered) return scipy.signal.resample(filtered, len(data)) elif mode == "broadband": # Rectify and lowpass filter filtered = np.abs(filtered) filtered = lowpass_filter(filtered.T, fs, rectify_lowpass).T return filtered else: raise ValueError("Invalid amp_env_mode {}".format(amp_env_mode))
def compute(self): result = {} for ch in self.channels: t_spec, f_spec, spec, _ = spectrogram(self.data[:, ch], *self.args, **self.kwargs) result[ch] = (t_spec, f_spec, spec) self.finished.emit(self.key, result)
# np.save("%sSeg2.npy" %fname[:-4], seg2) # else: # print('short seg') # shutil.move('/Users/Alicia/Desktop/BirdCallProject/Freq_20_10000/filteredCalls/%s' %fname, \ # '/Users/Alicia/Desktop/BirdCallProject/Freq_20_10000/reSegmentedCalls/%s' %fname) # if len(minimum) == 2: # seg1 = sound[:minimum[0]] # seg2 = sound[minimum[0]:minimum[1]] # seg3 = sound[minimum[1]:] # print(len(seg1), len(seg2), len(seg3)) # if len(seg1) > 1323 and len(seg2) > 1323 and len(seg3) > 1323: # np.save("%sSeg1.npy" %fname[:-4], seg1) # np.save("%sSeg2.npy" %fname[:-4], seg2) # np.save("%sSeg3.npy" %fname[:-4], seg3) # else: # print('short seg') # shutil.move('/Users/Alicia/Desktop/BirdCallProject/Freq_20_10000/filteredCalls/%s' %fname, \ # '/Users/Alicia/Desktop/BirdCallProject/Freq_20_10000/reSegmentedCalls/%s' %fname) # plt.plot(sound_env/sound_env.max(), color="red", linewidth=2) plt.figure() (tDebug, freqDebug, specDebug, rms) = spectrogram(sound, fs, 1000.0, 50, min_freq=0, max_freq=10000, nstd=6, cmplx=True) plot_spectrogram(tDebug, freqDebug, specDebug) plt.show()
buffer = 0.08 t_arr, sig = audio_signal.time_slice( max(0, center_of_mass - buffer), min(audio_signal.t_max, center_of_mass + buffer)) sig = sig - np.mean(sig, axis=0) sig = bandpass_filter(sig.T, audio_signal.sampling_rate, 1000, 8000).T specs = [] all_calls.append(sig) for ch in range(sig.shape[1]): # Sligtly lower resolution on the spectrograms can make this go faster # Can increase the params to 1000, 50 for a higher resolution spectrogram _, _, spec, _ = spectrogram(sig[:, ch], audio_signal.sampling_rate, 500, 100, min_freq=1000, max_freq=8000, cmplx=False) specs.append(spec) all_call_spectrograms.append(np.array(specs)) all_call_spectrograms = np.array(all_call_spectrograms) all_calls = np.array(all_calls) np.save(os.path.join(output_folder, "spectrograms.npy"), all_call_spectrograms) np.save(os.path.join(output_folder, "calls.npy"), all_calls) print("""
import scipy.spatial as ss import scipy.stats as sst from scipy.special import digamma, gamma from math import log, pi, exp from scipy.integrate import odeint from AttractorReconstructUtilities import TimeSeries3D, TimeDelayReconstruct from matplotlib import style from sciPlot import updateParams from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib.ticker import MultipleLocator, FixedLocator from soundsig.sound import WavFile, plot_spectrogram, spectrogram updateParams() X = np.load( '/Users/Alicia/Desktop/BirdCallProject/Freq_250_12000/filteredCalls/LblBla4548_130418-DC-46.npy' ) X = X[2900:4000] (tDebug, freqDebug, specDebug, rms) = spectrogram(X, 44100, 2000.0, 50, min_freq=0, max_freq=22050, nstd=6, cmplx=True) fig, ax = plt.subplots(figsize=(5, 2)) plot_spectrogram(tDebug, freqDebug, specDebug, ax=ax, colorbar=False) ax.set_xlabel('t (s)') plt.show() #fig.savefig('Fig5Bifurcations.svg', dpi=300, bbox_inches='tight', transparent=True)
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from ipywidgets import interactive from ipywidgets import FloatProgress from IPython.display import display import scipy.spatial as ss import scipy.stats as sst from scipy.special import digamma,gamma from math import log,pi,exp from scipy.integrate import odeint from AttractorReconstructUtilities import TimeSeries3D, TimeDelayReconstruct from matplotlib import style from sciPlot import updateParams from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib.ticker import MultipleLocator, FixedLocator from soundsig.sound import WavFile, plot_spectrogram, spectrogram updateParams() TenSongs = np.load('/Users/Alicia/Desktop/SongAttractor/Attractor3D/TenSongs.npy', allow_pickle=True) TenSongs = TenSongs[()] #Attractor = TimeDelayReconstruct(TenSongs['BDY'][37300:48000], 2, 3) (tDebug ,freqDebug ,specDebug , rms) = spectrogram(TenSongs['BDY'][37300:48000], 44100, 2000.0, 50, min_freq=0, max_freq=22050, nstd=6, cmplx=True) fig, ax =plt.subplots(figsize=(5,2)) plot_spectrogram(tDebug, freqDebug, specDebug, ax=ax, colorbar=False) ax.set_xlabel('t (s)') plt.show() #fig.savefig('Fig3BDYSpectrogram.svg', dpi=300, bbox_inches='tight', transparent=True)
def extract_spectrograms( audio_signal, intervals, buffer=0.02, # buffer around original interval to keep spec_buffer=0.04, # buffer so that everything thas the right padding. ): """Extract spectrograms from audio_signal denoted by intervals list intervals is a list of (t_start, t_end) tuples """ _time = time.time() print("Extracting spectrograms from {} intervals".format(len(intervals))) # all_calls = [] all_call_spectrograms = [] for idx, (t1, t2) in enumerate(intervals): print("Working on {}/{} ({:.2f}s elapsed)".format( idx + 1, len(intervals), time.time() - _time), end="\r") # Recentered signal with a small buffer of 40ms on either side t_arr, sig = audio_signal.time_slice( max(0, t1 - buffer), min(audio_signal.t_max, t2 + buffer)) sig = sig - np.mean(sig, axis=0) sig = bandpass_filter(sig.T, audio_signal.sampling_rate, 1000, 8000).T amp_env = get_amplitude_envelope(sig, fs=audio_signal.sampling_rate, lowpass=8000, highpass=1000) # Compute the temporal center of mass of the signal center_of_mass = t1 - buffer + np.sum( (t_arr * np.sum(amp_env, axis=1))) / np.sum(amp_env) # Recentered signal with a small buffer of 40ms on either side t_arr, sig = audio_signal.time_slice( max(0, center_of_mass - spec_buffer), min(audio_signal.t_max, center_of_mass + spec_buffer)) sig = sig - np.mean(sig, axis=0) sig = bandpass_filter(sig.T, audio_signal.sampling_rate, 1000, 8000).T specs = [] # all_calls.append(sig) for ch in range(sig.shape[1]): # Sligtly lower resolution on the spectrograms can make this go faster # Can increase the params to 1000, 50 for a higher resolution spectrogram _, _, spec, _ = spectrogram(sig[:, ch], audio_signal.sampling_rate, 500, 100, min_freq=1000, max_freq=8000, cmplx=False) specs.append(spec) all_call_spectrograms.append(np.array(specs)) all_call_spectrograms = np.array(all_call_spectrograms) # all_calls = np.array(all_calls) return all_call_spectrograms
#os.chdir('/Users/Alicia/Desktop/BirdCallProject/Freq_20_10000/dataAnalysis') allSegments = np.load('allSegment_Len500.npy') birdName = np.load('birdName.npy') birdName = np.repeat(birdName, 3) allFund = np.load('fund_Len500.npy')[:, None] allFundFiltered = allFund[~np.isnan(allFund)] tdata = np.arange(500) num = np.int(500 / allFundFiltered[0] * 669) (resampledSeg, resampledtdata) = resample(allSegments[0], num=num, t=tdata) #plt.plot(tdata, allSegments[0]) #plt.plot(resampledtdata, resampledSeg, '--') (tDebug, freqDebug, specDebug, rms) = spectrogram(allSegments[0], 44100, 1000.0, 100, min_freq=0, max_freq=10000, nstd=6, cmplx=True) plot_spectrogram(tDebug, freqDebug, specDebug) (tDebug, freqDebug, specDebug, rms) = spectrogram(resampledSeg, 44100, 1000.0, 100, min_freq=0, max_freq=10000, nstd=6, cmplx=True) plt.figure() plot_spectrogram(tDebug, freqDebug, specDebug)