def setUp(self): filepath = 'tests/example 1.wav' self.fs, self.sig = wav_2_mono(filepath, normalised=True) self.sig = np.ascontiguousarray(self.sig) self.args = dict(nfft=nfft, noverlap=noverlap, win_length=win_length, fs=self.fs, wav_file_path=None, start=0, end=None, sig=self.sig, center=True)
def setUp(self): filepath = 'tests/example 1.wav' self.fs, self.sig = wav_2_mono(filepath, normalised=True) self.sig = np.ascontiguousarray(self.sig) self.args = dict(nfft=nfft, noverlap=noverlap, win_length=win_length, fs=self.fs, wav_file_path=None, start=0, end=None, sig=self.sig, center=True) saved = DotMap(loadmat('tests/time-domain.mat')) self.energy = saved.energy.ravel() self.lat = saved.lat.ravel()[0] self.tc = saved.tc.ravel()[0]
def setUp(self): filepath = 'tests/example 1.wav' self.fs, _ = wav_2_mono(filepath, normalised=False) long_segment = wavfile.read_segment(filepath, beg_ms=100, end_ms=300, mono=True) self.long_segment = np.ascontiguousarray(long_segment) short_segment = wavfile.read_segment(filepath, beg_ms=100, end_ms=149, mono=True) self.short_segment = np.ascontiguousarray(short_segment)
def test_read_segment(self): filepath = 'tests/example 1.wav' fs, sig = wav_2_mono(filepath, normalised=True) full_siglen = len(sig) max_end_ms = int(np.floor(full_siglen / fs * 1000)) # Test read_segment for the full duration (end_ms = None, beg_ms = 0) segment0 = wavfile.read_segment(filepath, mono=True) self.assertEqual(full_siglen, len(segment0)) # Test reading segments of different length from different starting points. # The returned segment must have the prescribed length for beg_ms in [0, 1, 2, 20, 30, 100]: for length_ms in [1, 100, 150, 153, 200, max_end_ms - beg_ms]: end_ms = beg_ms + length_ms segment1 = wavfile.read_segment(filepath, beg_ms=beg_ms, end_ms=end_ms, mono=True) segment1_len_ms = np.round(len(segment1) * 1000 / fs) self.assertEqual(segment1_len_ms, length_ms) framelength = 256 for beg_ms in [0, 1, 2, 20, 30, 100]: for length_ms in [1, 100, 150, 153, 200, max_end_ms - beg_ms]: correct_length = int(length_ms * fs / 1000) if correct_length % framelength != 0: correct_length = np.ceil( correct_length / framelength) * framelength end_ms = beg_ms + length_ms segment1 = wavfile.read_segment(filepath, beg_ms=beg_ms, end_ms=end_ms, mono=True, winlen=framelength) self.assertEqual(correct_length, len(segment1))
def setUp(self): filepath = 'tests/example 1.wav' self.fs, self.sig = wav_2_mono(filepath, normalised=True) self.sig = np.ascontiguousarray(self.sig) saved = DotMap(loadmat('tests/freq-domain.mat')) self.winlen = saved.winlen.ravel()[0] self.nfft = saved.nfft.ravel()[0] self.noverlap = saved.noverlap.ravel()[0] self.sdecrease = saved.sdecrease.ravel() self.window = saved.window.ravel() self.args = dict(nfft=self.nfft, noverlap=self.noverlap, window=self.window, win_length=self.winlen, fs=self.fs, wav_file_path=None, start=0, end=None, sig=self.sig, center=False)
def setUp(self): filepath = 'tests/example 1.wav' self.fs, self.sig = wav_2_mono(filepath, normalised=True) self.sig = np.ascontiguousarray(self.sig) self.args = dict(nfft=nfft, noverlap=noverlap, win_length=win_length, fs=self.fs, wav_file_path=None, start=0, end=None, sig=self.sig, center=True) saved = DotMap(loadmat('tests/lpc.mat')) self.lpc_spect = saved.lpc_spect self.lpc_cepst = saved.lpc_cepst self.lp_coeffs = saved.lp_coeffs self.winlen = saved.winlen.ravel()[0] self.nfft = saved.nfft.ravel()[0] self.order = saved.order.ravel()[0] self.noverlap = saved.noverlap.ravel()[0] self.window = saved.window.ravel() self.args = dict(nfft=self.nfft, noverlap=self.noverlap, window=self.window, win_length=self.winlen, fs=self.fs, wav_file_path=None, start=0, end=None, sig=self.sig, center=False, order=self.order)
def extract_spectrogram(): """ Extract raw sepectrograms for all segments (Not the masked spectrogram from Luscinia) :return: """ audio_to_segs = {} for segment in Segment.objects.all(): audio_file = segment.audio_file if audio_file not in audio_to_segs: audio_to_segs[audio_file] = [(segment.id, segment.start_time_ms, segment.end_time_ms)] else: audio_to_segs[audio_file].append( (segment.id, segment.start_time_ms, segment.end_time_ms)) n = len(audio_to_segs) bar = Bar('Exporting spects ...', max=n) for audio_file, seg_list in audio_to_segs.items(): count = 0 for seg_id, start, end in seg_list: seg_spect_path = spect_fft_path(seg_id, 'syllable') if os.path.isfile(seg_spect_path): count += 1 if count == len(seg_list): bar.next() continue filepath = wav_path(audio_file) fs, sig = wav_2_mono(filepath) duration_ms = len(sig) * 1000 / fs _, _, s = signal.stft(sig, fs=fs, window=window, noverlap=noverlap, nfft=window_size, return_onesided=True) file_spect = np.abs(s * scale) height, width = np.shape(file_spect) file_spect = np.flipud(file_spect) try: file_spect = np.log10(file_spect) file_spect = ((file_spect - global_min_spect_pixel) / interval64) file_spect[np.isinf(file_spect)] = 0 file_spect = file_spect.astype(np.int) file_spect = file_spect.reshape((width * height, ), order='C') file_spect[file_spect >= 64] = 63 file_spect_rgb = np.empty((height, width, 3), dtype=np.uint8) file_spect_rgb[:, :, 0] = cm_red[file_spect].reshape( (height, width)) * 255 file_spect_rgb[:, :, 1] = cm_green[file_spect].reshape( (height, width)) * 255 file_spect_rgb[:, :, 2] = cm_blue[file_spect].reshape( (height, width)) * 255 file_spect_img = Image.fromarray(file_spect_rgb) file_spect_path = spect_fft_path(audio_file.id, 'song') ensure_parent_folder_exists(file_spect_path) if not os.path.isfile(file_spect_path): file_spect_img.save(file_spect_path, format='PNG') for seg_id, start, end in seg_list: roi_start = int(start / duration_ms * width) roi_end = int(np.ceil(end / duration_ms * width)) seg_spect_rgb = file_spect_rgb[:, roi_start:roi_end, :] seg_spect_img = Image.fromarray(seg_spect_rgb) seg_spect_path = spect_fft_path(seg_id, 'syllable') ensure_parent_folder_exists(seg_spect_path) if not os.path.isfile(seg_spect_path): seg_spect_img.save(seg_spect_path, format='PNG') except Exception as e: warning('Error occured at song id: {}'.format(audio_file.id)) raise e bar.next() bar.finish()