def output2_plot(self): if 'exportpath' in globals() and os.path.exists(exportpath): spectrogram(os.path.join(exportpath, 'SEN_2.wav'), 'Output Audio Sample Two', 'output02.png') Pixmap = QPixmap('output02.png') self.Spectrogram.setPixmap(Pixmap) else: QMessageBox.about(self, 'Warning', "First run Acoustic Separation Model")
def input_plot(self): if 'filename' in globals() and os.path.exists(filename): spectrogram(os.path.join(filename), 'Input Audio Sample', 'input.png') Pixmap = QPixmap('input.png') self.Spectrogram.setPixmap(Pixmap) else: QMessageBox.about( self, 'Warning', "First add external audio file or record audio file")
def plotAll(m): pitch = [] end = 0 for index, r in enumerate(m.speechSegment): pitch = pitch + [0] * (r[0] - end) + m.pitch[index] end = r[1] pitch = pitch + [0] * (m.frameNum - m.speechSegment[-1][1]) pl.subplot(611) pl.plot(m.absVolume) #pl.xlabel('Frame Num') pl.ylabel('Volume') #pl.grid(True) for s in m.speechSegment: pl.plot([s[0], s[0]], [0, max(m.absVolume)], color='red') pl.plot([s[1], s[1]], [0, max(m.absVolume)], color='green') pl.subplot(612) pl.plot(m.zcr) pl.ylabel('Zero Cross Rate') #pl.xlabel('Frame Num') pl.subplot(613) pl.plot(pitch) #pl.xlabel('Frame Num') pl.ylabel('Pitch') for index, s in enumerate(m.pitchSeg): for p in s: pl.plot([ p[0] + m.speechSegment[index][0], p[0] + m.speechSegment[index][0] ], [0, 500], color='red') pl.plot([ p[1] + m.speechSegment[index][0], p[1] + m.speechSegment[index][0] ], [0, 500], color='green') pl.subplot(614) pl.plot(m.f1) pl.ylabel('Formant 1') pl.subplot(615) spectrogram(m) pl.ylabel("spectrogram") pl.xlabel('Frame Num') pl.subplot(616) pl.ylabel("Energy Below 250Hz") pl.plot(m.energyBelow250) pl.show()
def spectrogram_process(self): """Processing spectrogram :returns: TODO """ spectrogram_obj = spectrogram.spectrogram(self._aiff_datas, self._temporal_data_point_list, self._temporal_point) return np.asarray(spectrogram_obj.spectrogram_generate())
def calculateSpectro(sound, spec_sample_rate=1000, freq_spacing=210, min_freq=0, max_freq=10000): t,f,spec,spec_rms = spectrogram(sound, 44100, spec_sample_rate=spec_sample_rate, freq_spacing=freq_spacing, \ min_freq=min_freq, max_freq=max_freq, cmplx=True) spectro = 20 * np.log10(np.abs(spec)) return spectro
def plotAll(m): pitch = [] end = 0 for index,r in enumerate(m.speechSegment): pitch = pitch + [0]*(r[0]-end) + m.pitch[index] end = r[1] pitch = pitch + [0]*(m.frameNum-m.speechSegment[-1][1]) pl.subplot(611) pl.plot(m.absVolume) #pl.xlabel('Frame Num') pl.ylabel('Volume') #pl.grid(True) for s in m.speechSegment: pl.plot([s[0],s[0]],[0,max(m.absVolume)],color='red') pl.plot([s[1],s[1]],[0,max(m.absVolume)],color='green') pl.subplot(612) pl.plot(m.zcr) pl.ylabel('Zero Cross Rate') #pl.xlabel('Frame Num') pl.subplot(613) pl.plot(pitch) #pl.xlabel('Frame Num') pl.ylabel('Pitch') for index,s in enumerate(m.pitchSeg): for p in s: pl.plot([p[0]+m.speechSegment[index][0],p[0]+m.speechSegment[index][0]],[0,500],color='red') pl.plot([p[1]+m.speechSegment[index][0],p[1]+m.speechSegment[index][0]],[0,500],color='green') pl.subplot(614) pl.plot(m.f1) pl.ylabel('Formant 1') pl.subplot(615) spectrogram(m) pl.ylabel("spectrogram") pl.xlabel('Frame Num') pl.subplot(616) pl.ylabel("Energy Below 250Hz") pl.plot(m.energyBelow250) pl.show()
def _audio_file_spectrogram(self, _file, nfft, duration, overlap): # first get the spec_params and let the client setup the canvas fs = _file.sample_rate spec_params = get_audio_spectrogram_params(_file, fs, duration, nfft, overlap) self.send_spectrogram_new(spec_params) # now lets compute the spectrogram and send it over data = _file[:spec_params.nsamples].sum(axis=1) for chunk in grouper(data, spec_params.chunksize, spec_params.nfft): chunk = np.array(chunk) spec = spectrogram(chunk, spec_params) self.send_spectrogram_update(spec)
def _audio_file_spectrogram(self, _file, nfft, duration, overlap): # first get the spec_params and let the client setup the canvas fs = _file.sample_rate spec_params = get_audio_spectrogram_params( _file, fs, duration, nfft, overlap) self.send_spectrogram_new(spec_params) # now lets compute the spectrogram and send it over data = _file[:spec_params.nsamples].sum(axis=1) for chunk in grouper(data, spec_params.chunksize, spec_params.nfft): chunk = np.array(chunk) spec = spectrogram(chunk, spec_params) self.send_spectrogram_update(spec)
def __process_wav(self, wav, label_dict, class_name, f_label, f_label_bg, f_meta, additional_meta=""): print("Preparing file: ", wav) y, sr = librosa.load(wav) spec = spectrogram(y) spec, bg = self.bg_subtraction(spec) chunks = self.make_chunks(spec) name, ftype = wav.split(".") name = name.split("/")[-1] for i, chunk in enumerate(chunks): out_fname = str.format("{}/{}_{}.pkl", self.out_path, name, i) pickle.dump(chunk, open(out_fname, "wb"), protocol=2) self.label_lock.acquire() f_label.write(out_fname + " " + str(label_dict[class_name]) + "\n") f_meta.write("{} {} {} {} {}\n".format(out_fname, sr, chunk.shape[0], chunk.shape[1], additional_meta.encode("utf-8"))) f_label.flush() f_meta.flush() self.label_lock.release() if bg.shape[1] == 0 or spec.shape[1] == 0: return bg = np.true_divide(bg, np.max(spec)) chunks_bg = self.make_chunks(bg) for i, chunk in enumerate(chunks_bg): out_fname = str.format("{}/bg/{}_bg_{}.pkl", self.out_path, name, i) self.bg_lock.acquire() f_label_bg.write(out_fname + " " + str(label_dict[class_name]) + "\n") f_label_bg.flush() self.bg_lock.release() pickle.dump(chunk, open(out_fname, "wb"), protocol=2)
# Let's look at some difference spectrograms... voiced_events = [] unvoiced_events = [] trial_events = grp.events().query_events( 'TRIAL' ) for event in trial_events: if 'DAH' in event['name']: voiced_events.append( event ) if 'GAH' in event['name']: voiced_events.append( event ) if 'BAH' in event['name']: voiced_events.append( event ) if 'TAH' in event['name']: unvoiced_events.append( event ) if 'KAH' in event['name']: unvoiced_events.append( event ) if 'PAH' in event['name']: unvoiced_events.append( event ) voiced_events = np.array( voiced_events ) unvoiced_events = np.array( unvoiced_events ) from spectrogram import spectrogram, _plot_specgram voiced_spec, f, t = spectrogram( grp.eeg(), 'AMIC15', voiced_events, progress_bar = True ) unvoiced_spec, f, t = spectrogram( grp.eeg(), 'AMIC15', unvoiced_events, progress_bar = True ) diff_spec = np.subtract( voiced_spec, unvoiced_spec ) _plot_specgram( voiced_spec, f, t, title = "Voiced", low_freq = 0, high_freq = 2000, clamp = 3.0 ) _plot_specgram( unvoiced_spec, f, t, title = "Unvoiced", low_freq = 0, high_freq = 2000, clamp = 3.0 ) _plot_specgram( diff_spec, f, t, title = "Voiced - Unvoiced", low_freq = 0, high_freq = 2000, clamp = 3.0 ) # Sonification! from util import listen listen( grp.eeg()[ grp.events().query('KAH', length = 10000), 'AMIC15' ], grp.eeg().get_rate() ) listen( grp.eeg()[ grp.events().query('TRIAL', length = 10000), 'PMIC9' ], grp.eeg().get_rate() )
Segment / np.abs(Segment).max() * np.max(allSegments) for Segment in allSegments ]) allSpectros = np.array( [np.ravel(calculateSpectro(Seg)) for Seg in allSegments]) allSpectros = allSpectros - allSpectros.max() allFund = np.load('fund_Len500.npy') nonanIndFund = ~np.isnan(allFund) #lessInd = np.where(allFund<800)[0] nonanInd = [d and m for d, m in zip(nonanIndFund, nonanIndFund)] #allFundFiltered = allFund[nonanInd] ##print(allFundFiltered[:100]) birdName = birdName[nonanInd] allSpectros = allSpectros[nonanInd] (tDebug, freqDebug, specDebug, rms) = spectrogram(allPeriods[0], 44100, 1000.0, 210, min_freq=0, max_freq=10000, nstd=2, cmplx=True) plot_spectrogram(tDebug, freqDebug, specDebug) plt.show() Ind = np.arange(len(birdName)) np.random.shuffle(Ind) birdNameShuffled = birdName[Ind] allSpectros = allSpectros[Ind] #scores, pValues, nhighScores, ncounter = pwLogiRegression(allSpectros, birdNameShuffled, nonanIndFeature=None, cv=True, printijScores=False)
import os import numpy as np from functions import list_songs from spectrogram import spectrogram os.makedirs('./spectro', exist_ok=True) diff = 'expert' precision = 2 for f in list_songs(diff): H = spectrogram(f, precision) np.save(f'./spectro/{f}', H)
def load_npy(self,np_path): #print("Loading npy file: ", np_path) y = pickle.load(open(np_path, "rb")) return spectrogram(y)
chirp = Signal('chirp', 'LFM_1K_5K.wav') chirp.generateValsFromFile() chirp.values /= np.max(chirp.values) chirp.values -= np.mean(chirp.values) outside = Signal('outside', '171013104635_1_B.wav') outside.generateValsFromFile() outside.values /= np.max(outside.values) plt.figure(figsize=(17 * 2 / 3, 22 * 2 / 3)) plt.tight_layout() # plot signals' spectrograms plt.subplot(321) plt.title("Chirp") specs, f, t = spectrogram(chirp, 1000, 200) specs = specs[:, ::20] t = t[::20] print('Heatmap size:', np.shape(specs)) t, f = np.meshgrid(t, f) plt.xlabel('Time (s)') plt.ylabel('Frequency (Hz)') plt.ylim(0, 6000) plt.xlim(0, 0.25) plt.pcolormesh(t, f, specs, vmin=-150, cmap="BuPu") print("@debug created first spectrogram") plt.subplot(322) plt.title("Outside") specs2, f2, t2 = spectrogram(outside, 1000, 200) specs2 = specs2[:, ::20]