def test_axis_rolling(self): np.random.seed(1234) x_flat = np.random.randn(1024) _, _, z_flat = stft(x_flat) for a in range(3): newshape = [1,]*3 newshape[a] = -1 x = x_flat.reshape(newshape) _, _, z_plus = stft(x, axis=a) # Positive axis index _, _, z_minus = stft(x, axis=a-x.ndim) # Negative axis index assert_equal(z_flat, z_plus.squeeze(), err_msg=a) assert_equal(z_flat, z_minus.squeeze(), err_msg=a-x.ndim) # z_flat has shape [n_freq, n_time] # Test vs. transpose _, x_transpose_m = istft(z_flat.T, time_axis=-2, freq_axis=-1) _, x_transpose_p = istft(z_flat.T, time_axis=0, freq_axis=1) assert_allclose(x_flat, x_transpose_m, err_msg='istft transpose minus') assert_allclose(x_flat, x_transpose_p, err_msg='istft transpose plus')
def test_roundtrip_boundary_extension(self): np.random.seed(1234) # Test against boxcar, since window is all ones, and thus can be fully # recovered with no boundary extension settings = [ ('boxcar', 100, 10, 0), # Test no overlap ('boxcar', 100, 10, 9), # Test high overlap ] for window, N, nperseg, noverlap in settings: t = np.arange(N) x = 10*np.random.randn(t.size) _, _, zz = stft(x, nperseg=nperseg, noverlap=noverlap, window=window, detrend=None, padded=True, boundary=None) _, xr = istft(zz, noverlap=noverlap, window=window, boundary=False) for boundary in ['even', 'odd', 'constant', 'zeros']: _, _, zz_ext = stft(x, nperseg=nperseg, noverlap=noverlap, window=window, detrend=None, padded=True, boundary=boundary) _, xr_ext = istft(zz_ext, noverlap=noverlap, window=window, boundary=True) msg = '{0}, {1}, {2}'.format(window, noverlap, boundary) assert_allclose(x, xr, err_msg=msg) assert_allclose(x, xr_ext, err_msg=msg)
def test_roundtrip_padded_FFT(self): np.random.seed(1234) settings = [ ('hann', 1024, 256, 128, 512), ('hann', 1024, 256, 128, 501), ('boxcar', 100, 10, 0, 33), (('tukey', 0.5), 1152, 256, 64, 1024), ] for window, N, nperseg, noverlap, nfft in settings: t = np.arange(N) x = 10*np.random.randn(t.size) xc = x*np.exp(1j*np.pi/4) # real signal _, _, z = stft(x, nperseg=nperseg, noverlap=noverlap, nfft=nfft, window=window, detrend=None, padded=True) # complex signal _, _, zc = stft(xc, nperseg=nperseg, noverlap=noverlap, nfft=nfft, window=window, detrend=None, padded=True, return_onesided=False) tr, xr = istft(z, nperseg=nperseg, noverlap=noverlap, nfft=nfft, window=window) tr, xcr = istft(zc, nperseg=nperseg, noverlap=noverlap, nfft=nfft, window=window, input_onesided=False) msg = '{0}, {1}'.format(window, noverlap) assert_allclose(t, tr, err_msg=msg) assert_allclose(x, xr, err_msg=msg) assert_allclose(xc, xcr, err_msg=msg)
def testContribSignalSTFT(self): ws = 512 hs = 128 dims = (ws * 20,) shape = BATCH_DIMS + dims data = np.arange(np.prod(shape)) / np.prod(dims) np.random.seed(123) np.random.shuffle(data) data = np.reshape(data.astype(np.float32), shape) window = sps.get_window("hann", ws) expected = sps.stft( data, nperseg=ws, noverlap=ws - hs, boundary=None, window=window)[2] expected = np.swapaxes(expected, -1, -2) expected *= window.sum() # scipy divides by window sum with self.test_session() as sess: with self.test_scope(): ph = array_ops.placeholder( dtypes.as_dtype(data.dtype), shape=data.shape) out = signal.stft(ph, ws, hs) grad = gradients_impl.gradients(out, ph, grad_ys=array_ops.ones_like(out)) # For gradients, we simply verify that they compile & execute. value, _ = sess.run([out, grad], {ph: data}) self.assertAllClose(expected, value, rtol=RTOL, atol=ATOL)
def test_input_validation(self): assert_raises(ValueError, check_COLA, 'hann', -10, 0) assert_raises(ValueError, check_COLA, 'hann', 10, 20) assert_raises(ValueError, check_COLA, np.ones((2,2)), 10, 0) assert_raises(ValueError, check_COLA, np.ones(20), 10, 0) x = np.empty(1024) z = stft(x) assert_raises(ValueError, stft, x, window=np.ones((2,2))) assert_raises(ValueError, stft, x, window=np.ones(10), nperseg=256) assert_raises(ValueError, stft, x, nperseg=-256) assert_raises(ValueError, stft, x, nperseg=256, noverlap=1024) assert_raises(ValueError, stft, x, nperseg=256, nfft=8) assert_raises(ValueError, istft, x) # Not 2d assert_raises(ValueError, istft, z, window=np.ones((2,2))) assert_raises(ValueError, istft, z, window=np.ones(10), nperseg=256) assert_raises(ValueError, istft, z, nperseg=-256) assert_raises(ValueError, istft, z, nperseg=256, noverlap=1024) assert_raises(ValueError, istft, z, nperseg=256, nfft=8) assert_raises(ValueError, istft, z, nperseg=256, noverlap=0, window='hann') # Doesn't meet COLA assert_raises(ValueError, istft, z, time_axis=0, freq_axis=0) assert_raises(ValueError, _spectral_helper, x, x, mode='foo') assert_raises(ValueError, _spectral_helper, x[:512], x[512:], mode='stft') assert_raises(ValueError, _spectral_helper, x, x, boundary='foo')
def test_roundtrip_real(self): np.random.seed(1234) settings = [ ('boxcar', 100, 10, 0), # Test no overlap ('boxcar', 100, 10, 9), # Test high overlap ('bartlett', 101, 51, 26), # Test odd nperseg ('hann', 1024, 256, 128), # Test defaults (('tukey', 0.5), 1152, 256, 64), # Test Tukey ('hann', 1024, 256, 255), # Test overlapped hann ] for window, N, nperseg, noverlap in settings: t = np.arange(N) x = 10*np.random.randn(t.size) _, _, zz = stft(x, nperseg=nperseg, noverlap=noverlap, window=window, detrend=None, padded=False) tr, xr = istft(zz, nperseg=nperseg, noverlap=noverlap, window=window) msg = '{0}, {1}'.format(window, noverlap) assert_allclose(t, tr, err_msg=msg) assert_allclose(x, xr, err_msg=msg)
def test_roundtrip_complex(self): np.random.seed(1234) settings = [ ('boxcar', 100, 10, 0), # Test no overlap ('boxcar', 100, 10, 9), # Test high overlap ('bartlett', 101, 51, 26), # Test odd nperseg ('hann', 1024, 256, 128), # Test defaults (('tukey', 0.5), 1152, 256, 64), # Test Tukey ('hann', 1024, 256, 255), # Test overlapped hann ] for window, N, nperseg, noverlap in settings: t = np.arange(N) x = 10*np.random.randn(t.size) + 10j*np.random.randn(t.size) _, _, zz = stft(x, nperseg=nperseg, noverlap=noverlap, window=window, detrend=None, padded=False, return_onesided=False) tr, xr = istft(zz, nperseg=nperseg, noverlap=noverlap, window=window, input_onesided=False) msg = '{0}, {1}, {2}'.format(window, nperseg, noverlap) assert_allclose(t, tr, err_msg=msg) assert_allclose(x, xr, err_msg=msg) # Check that asking for onesided switches to twosided with suppress_warnings() as sup: sup.filter(UserWarning, "Input data is complex, switching to return_onesided=False") _, _, zz = stft(x, nperseg=nperseg, noverlap=noverlap, window=window, detrend=None, padded=False, return_onesided=True) tr, xr = istft(zz, nperseg=nperseg, noverlap=noverlap, window=window, input_onesided=False) msg = '{0}, {1}, {2}'.format(window, nperseg, noverlap) assert_allclose(t, tr, err_msg=msg) assert_allclose(x, xr, err_msg=msg)
def test_permute_axes(self): np.random.seed(1234) x = np.random.randn(1024) fs = 1.0 window = 'hann' nperseg = 16 noverlap = 8 f1, t1, Z1 = stft(x, fs, window, nperseg, noverlap) f2, t2, Z2 = stft(x.reshape((-1, 1, 1)), fs, window, nperseg, noverlap, axis=0) t3, x1 = istft(Z1, fs, window, nperseg, noverlap) t4, x2 = istft(Z2.T, fs, window, nperseg, noverlap, time_axis=0, freq_axis=-1) assert_allclose(f1, f2) assert_allclose(t1, t2) assert_allclose(t3, t4) assert_allclose(Z1, Z2[:, 0, 0, :]) assert_allclose(x1, x2[:, 0, 0])
def test_average_all_segments(self): np.random.seed(1234) x = np.random.randn(1024) fs = 1.0 window = 'hann' nperseg = 16 noverlap = 8 # Compare twosided, because onesided welch doubles non-DC terms to # account for power at negative frequencies. stft doesn't do this, # because it breaks invertibility. f, _, Z = stft(x, fs, window, nperseg, noverlap, padded=False, return_onesided=False, boundary=None) fw, Pw = welch(x, fs, window, nperseg, noverlap, return_onesided=False, scaling='spectrum', detrend=False) assert_allclose(f, fw) assert_allclose(np.mean(np.abs(Z)**2, axis=-1), Pw)
def test_roundtrip_float32(self): np.random.seed(1234) settings = [('hann', 1024, 256, 128)] for window, N, nperseg, noverlap in settings: t = np.arange(N) x = 10*np.random.randn(t.size) x = x.astype(np.float32) _, _, zz = stft(x, nperseg=nperseg, noverlap=noverlap, window=window, detrend=None, padded=False) tr, xr = istft(zz, nperseg=nperseg, noverlap=noverlap, window=window) msg = '{0}, {1}'.format(window, noverlap) assert_allclose(t, t, err_msg=msg) assert_allclose(x, xr, err_msg=msg, rtol=1e-4) assert_(x.dtype == xr.dtype)
def test_roundtrip_padded_signal(self): np.random.seed(1234) settings = [ ('boxcar', 101, 10, 0), ('hann', 1000, 256, 128), ] for window, N, nperseg, noverlap in settings: t = np.arange(N) x = 10*np.random.randn(t.size) _, _, zz = stft(x, nperseg=nperseg, noverlap=noverlap, window=window, detrend=None, padded=True) tr, xr = istft(zz, noverlap=noverlap, window=window) msg = '{0}, {1}'.format(window, noverlap) # Account for possible zero-padding at the end assert_allclose(t, tr[:t.size], err_msg=msg) assert_allclose(x, xr[:x.size], err_msg=msg)
def take_stft(self, x): TIME_WINDOW = 0.1 # SECONDS HIGHTEST_NOTE = 4000 # HZ down_frames, down_sample_factor = self.downsample(HIGHTEST_NOTE) fr = self.frame_rate / down_sample_factor frames_per_seg = fr * TIME_WINDOW N = len(down_frames) freq_shift = fftfreq(N, d=1 / (self.BPM_TO_HZ * HIGHTEST_NOTE * 2)) f, t, Zxx = stft(down_frames, fs=fr, nperseg=int(frames_per_seg)) mag = np.abs(Zxx) mag_avg = mag.mean() mag_std = mag.std() all_notes = [None] * t.size stds = [None] * t.size total_mags = [None] * t.size note_dict = {} plt.figure() for frame in range(t.size): mags = mag[:, frame] total_mags[frame] = np.sum(mags) stds[frame] = mags.std() peak_freqs = f[np.where(mags > mag_avg + 6 * mag_std)] peak_freqs = peak_freqs[np.where(freqs > 200)] np.sort(peak_freqs) all_notes[frame] = self.calc_note(peak_freqs[:5].tolist()) for note in all_notes[frame]: if note in note_dict: note_dict[note] += 1 else: note_dict[note] = 1 plt.scatter(t[frame] * np.ones(np.shape(peak_freqs[:5])), all_notes[frame], 9) plt.figure() sorted_note_hist = sorted(note_dict.items(), key=lambda x: x[1]) print('most common notes: ') print(sorted_note_hist[-1][0]) print(sorted_note_hist[-2][0]) print(sorted_note_hist[-3][0]) print(sorted_note_hist[-4][0]) print(sorted_note_hist[-5][0]) lp = butter_lowpass_filter(data=stds, cutoff=0.3, fs=1 / t[1] - t[0], order=6) plt.plot(t, lp) plt.scatter(t[detect_peaks(lp)], lp[detect_peaks(lp)]) plt.figure() print("num peaks: ", len(detect_peaks(lp))) # # plt.figure() # plt.plot(total_mags) # plt.figure() print(mode([f for array in all_notes for f in array])) # plt.pcolormesh(t, f[:300], mag[:300, :], vmin=0, vmax=np.amax(mag)) # for i, frame in enumerate(all_notes): # plt.scatter(t[i] * np.ones(np.shape(frame)), frame, s=9, c="red") plt.show()
import numpy as np import matplotlib.pyplot as plt # Lendo o arquivo Fs, x = wavfile.read("When I am laid in earth (Dido_s lament)_RED.wav") # Especificando o espectrograma wsize = 2048 nfft = 2 * wsize noverlap = round(wsize * 0.75) leapsize = wsize - noverlap f, t, Sx = signal.stft(x, nperseg=wsize, fs=Fs, window='hann', noverlap=noverlap, nfft=nfft) # Alguns valores relevantes para as contas nbins = len(Sx) nframes = len(Sx[0]) fmin = f[1] # Vendo o espectrograma plt.figure(figsize=(17, 6)) plt.pcolormesh(t, f, abs(Sx)) plt.ylabel('Frequência [Hz]') plt.xlabel('Tempo [s]') plt.ylim(0, 3000) plt.colorbar()
def pack_GRU(xdir): _, x = wavfile.read(xdir) _, _, Zxx = stft(x, freq) Zxx = log((abss(Zxx)) + 1e-7) return bark_dct(bark_rescale(fr2bark(Zxx)))
def prepare_pretrain_input(audioFile, visualFeaturesFile, targetFile, noise, numWords, charToIx, noiseSNR, audioParams, videoParams): """ Function to convert the data sample in the pretrain dataset into appropriate tensors. """ #reading the whole target file and the target with open(targetFile, "r") as f: lines = f.readlines() lines = [line.strip() for line in lines] trgt = lines[0][7:] words = trgt.split(" ") #if number of words in target is less than the required number of words, consider the whole target if len(words) <= numWords: trgtNWord = trgt sampFreq, inputAudio = wavfile.read(audioFile) vidInp = np.load(visualFeaturesFile) else: #make a list of all possible sub-sequences with required number of words in the target nWords = [ " ".join(words[i:i + numWords]) for i in range(len(words) - numWords + 1) ] nWordLens = np.array([len(nWord) + 1 for nWord in nWords]).astype(np.float) #choose the sub-sequence for target according to a softmax distribution of the lengths #this way longer sub-sequences (which are more diverse) are selected more often while #the shorter sub-sequences (which appear more frequently) are not entirely missed out ix = np.random.choice(np.arange(len(nWordLens)), p=softmax(nWordLens)) trgtNWord = nWords[ix] #reading the start and end times in the video corresponding to the selected sub-sequence startTime = float(lines[4 + ix].split(" ")[1]) endTime = float(lines[4 + ix + numWords - 1].split(" ")[2]) #loading the audio sampFreq, audio = wavfile.read(audioFile) inputAudio = audio[int(sampFreq * startTime):int(sampFreq * endTime)] #loading visual features videoFPS = videoParams["videoFPS"] vidInp = np.load(visualFeaturesFile) vidInp = vidInp[int(np.floor(videoFPS * startTime) ):int(np.ceil(videoFPS * endTime))] #converting each character in target to its corresponding index trgt = [charToIx[char] for char in trgtNWord] trgt.append(charToIx["<EOS>"]) trgt = np.array(trgt) trgtLen = len(trgt) #STFT feature extraction stftWindow = audioParams["stftWindow"] stftWinLen = audioParams["stftWinLen"] stftOverlap = audioParams["stftOverlap"] #pad the audio to get atleast 4 STFT vectors if len(inputAudio) < sampFreq * (stftWinLen + 3 * (stftWinLen - stftOverlap)): padding = int( np.ceil((sampFreq * (stftWinLen + 3 * (stftWinLen - stftOverlap)) - len(inputAudio)) / 2)) inputAudio = np.pad(inputAudio, padding, "constant") inputAudio = inputAudio / np.max(np.abs(inputAudio)) #adding noise to the audio if noise is not None: pos = np.random.randint(0, len(noise) - len(inputAudio) + 1) noise = noise[pos:pos + len(inputAudio)] noise = noise / np.max(np.abs(noise)) gain = 10**(noiseSNR / 10) noise = noise * np.sqrt( np.sum(inputAudio**2) / (gain * np.sum(noise**2))) inputAudio = inputAudio + noise #normalising the audio to unit power inputAudio = inputAudio / np.sqrt(np.sum(inputAudio**2) / len(inputAudio)) #computing the STFT and taking only the magnitude of it _, _, stftVals = signal.stft(inputAudio, sampFreq, window=stftWindow, nperseg=sampFreq * stftWinLen, noverlap=sampFreq * stftOverlap, boundary=None, padded=False) audInp = np.abs(stftVals) audInp = audInp.T #padding zero vectors to extend the audio and video length to a least possible integer length such that #video length = 4 * audio length if len(audInp) / 4 >= len(vidInp): inpLen = int(np.ceil(len(audInp) / 4)) leftPadding = int(np.floor((4 * inpLen - len(audInp)) / 2)) rightPadding = int(np.ceil((4 * inpLen - len(audInp)) / 2)) audInp = np.pad(audInp, ((leftPadding, rightPadding), (0, 0)), "constant") leftPadding = int(np.floor((inpLen - len(vidInp)) / 2)) rightPadding = int(np.ceil((inpLen - len(vidInp)) / 2)) vidInp = np.pad(vidInp, ((leftPadding, rightPadding), (0, 0)), "constant") else: inpLen = len(vidInp) leftPadding = int(np.floor((4 * inpLen - len(audInp)) / 2)) rightPadding = int(np.ceil((4 * inpLen - len(audInp)) / 2)) audInp = np.pad(audInp, ((leftPadding, rightPadding), (0, 0)), "constant") #checking whether the input length is greater than or equal to the required length #if not, extending the input by padding zero vectors reqInpLen = req_input_length(trgt) if inpLen < reqInpLen: leftPadding = int(np.floor((reqInpLen - inpLen) / 2)) rightPadding = int(np.ceil((reqInpLen - inpLen) / 2)) audInp = np.pad(audInp, ((4 * leftPadding, 4 * rightPadding), (0, 0)), "constant") vidInp = np.pad(vidInp, ((leftPadding, rightPadding), (0, 0)), "constant") inpLen = len(vidInp) audInp = torch.from_numpy(audInp) vidInp = torch.from_numpy(vidInp) inp = (audInp, vidInp) inpLen = torch.tensor(inpLen) trgt = torch.from_numpy(trgt) trgtLen = torch.tensor(trgtLen) return inp, trgt, inpLen, trgtLen
def separate_instruments(file_name="rhythm_birdland.wav"): # Read the file fs, x = read("./inputs/" + file_name) f, t, Sxx = spectrogram(x, fs) plt.pcolormesh(t, f, Sxx, norm=colors.LogNorm(vmin=Sxx.min(), vmax=Sxx.max())) plt.title('Spectrogram of x(t)') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.show() winlen = 1024 # Step 1: Generate STFT # Have used terminology from the paper # h and i represent indices of frequency and time bins h, i, F = stft(x=x, fs=fs, window='hann', nperseg=winlen, noverlap=int(winlen / 2), nfft=winlen, detrend=False, return_onesided=True, padded=True, axis=-1) # Step 2: Calculate a range-compressed version of the power spectrogram gamma = 0.3 W = np.power(np.abs(F), 2 * gamma) # Step 3: Initialise k_max = 100 H = 0.5 * W P = 0.5 * W alpha = 0.3 for k in range(k_max): # Step 4: Calculate update variable delta term_1 = np.zeros_like(H) term_2 = np.zeros_like(H) for i_iter in range(1, np.shape(H)[1] - 1): term_1[:, i_iter] = alpha * ((H[:, i_iter - 1] + H[:, i_iter + 1] - (2 * H[:, i_iter])) / 4) term_1[:, 0] = alpha * ((H[:, 1] - H[:, 0]) / 2) term_1[:, -1] = alpha * ((H[:, -2] - H[:, -1]) / 2) for h_iter in range(1, np.shape(H)[0] - 1): term_2[h_iter, :] = (1 - alpha) * ( (P[h_iter - 1, :] + P[h_iter + 1, :] - (2 * P[h_iter, :])) / 4) term_2[0, :] = (1 - alpha) * ((P[1, :] - P[0, :]) / 2) term_2[-1, :] = (1 - alpha) * ((P[-2, :] - P[-1, :]) / 2) delta = term_1 - term_2 # Reduce "step size" delta = delta * 0.9 # Step 5: Update H and P H = np.minimum(np.maximum(H + delta, 0), W) P = W - H # Step 6: Increment k (automatically through loop) # Step 7: Binarize the separation result H = np.where(np.less(H, P), 0, W) P = np.where(np.greater_equal(H, P), 0, W) # Step 8: Generate separate waveforms H_temp = np.power(H, (1 / (2 * gamma))) * np.exp( 1j * np.angle(F)) #ISTFT is taken first on this, with H P_temp = np.power(P, (1 / (2 * gamma))) * np.exp( 1j * np.angle(F)) # ISTFT is taken second on this, with P _, h = istft(H_temp, fs=fs, window='hann', nperseg=winlen, noverlap=int(winlen / 2), nfft=winlen, input_onesided=True) _, p = istft(P_temp, fs=fs, window='hann', nperseg=winlen, noverlap=int(winlen / 2), nfft=winlen, input_onesided=True) ##################################################################################################### plt.figure(1) plt.subplot(2, 1, 1) t_scale = np.linspace(0, len(h) / fs, len(h)) plt.plot(t_scale, h) plt.title('Time domain visualization of h(t)') plt.axis('tight') plt.grid('on') plt.ylabel('Amplitude') plt.xlabel('Time (s)') plt.subplot(2, 1, 2) t_scale = np.linspace(0, len(p) / fs, len(p)) plt.plot(t_scale, p) plt.title('Time domain visualization of p(t)') plt.axis('tight') plt.grid('on') plt.ylabel('Amplitude') plt.xlabel('Time (s)') plt.show() plt.figure(2) plt.subplot(2, 1, 1) f, t, Sxx = spectrogram(h, fs) plt.pcolormesh(t, f, Sxx, norm=colors.LogNorm(vmin=Sxx.min(), vmax=Sxx.max())) plt.title('Spectrogram of h(t)') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.subplot(2, 1, 2) f, t, Sxx = spectrogram(p, fs) plt.pcolormesh(t, f, Sxx, norm=colors.LogNorm(vmin=Sxx.min(), vmax=Sxx.max())) plt.title('Spectrogram of p(t)') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.show() write('./outputs/h_' + file_name, int(fs), np.int16(h)) write('./outputs/p_' + file_name, int(fs), np.int16(p)) plt.figure() ha, = plt.plot((h + p)[10000:10500], label='reconstructed') hb, = plt.plot(x[10000:10500], 'k--', label='original') plt.legend(handles=[ha, hb]) plt.title('Original and separated(10000:105000 samples) waveforms') plt.show() original_power = 10 * np.log10(np.sum(np.power(x, 2))) error = x - (h + p)[0:len(x)] noise_power = 10 * np.log10(np.sum(np.power(error, 2))) print("SNR is: %.2fdB" % (original_power - noise_power, ))
def make_spectrogram(sample, sample_rate): #freqs, times, spectrogram = signal.spectrogram(sample, sample_rate) freqs, times, spectrogram = signal.stft(sample, sample_rate) return np.log1p(np.abs(spectrogram.T)), freqs, times
os.mkdir(savedir) image_list = [] for i in range(start, stop, step): x = i savefigdir = "./gif/png/" + str(x) data = sdf.read(sdfdir + str(x).zfill(filenumber) + ".sdf", dict=True) Bz = data['Electric Field/Ey'] time = data['Header']['time'] bz = Bz.data density = data['Derived/Number_Density/electron1'].data bz = bz.T density = density.T bz_y0 = bz[500] k0 = 2 * 3.14 / 0.8e-6 f, t, zxx = signal.stft(bz_y0, fs=2 * 3.14 / const.delta_x / k0, nperseg=const.nperseg) fig, axs = plt.subplots(3, 1) im = axs[0].pcolormesh(bz, cmap=plt.get_cmap('bwr')) im2 = axs[1].pcolormesh(density, cmap=plt.get_cmap('gray')) im3 = axs[2].pcolormesh(t, f, np.abs(zxx), cmap=plt.get_cmap('BuPu')) axs[2].set_ylim((0, 2)) axs[0].set_title(time, fontsize=12, color='r') fig.savefig(savefigdir + "bz.png", dpi=200) image_list.append(str(savefigdir + "bz.png")) plt.cla() plt.clf() plt.close('all')
# break # 이미지 생성 부분 ****** # 왼손 left_data_len = len(data.left_data) cnt = 1 for i in data.left_data: i = pd.DataFrame(i) for idx in range(0, 3): imgname = "STFT_2000_over30/sub" + str(subject) + "/ch" + str( idx + 1) + "/sub" + str(subject) + "_left_" + str( cnt) + "_ch" + str(idx + 1) + ".png" # imgname="all_images/ch"+str(idx+1)+"/sub"+str(subject)+"_left_"+str(cnt)+"_ch"+str(idx+1)+".png" f, t, Z = signal.stft(i.iloc[:, idx], fs=FS, nperseg=TIME_WINDOW) Zr = abs(Z) plt.pcolormesh(Zr) plt.ylim(0, 40) plt.axis('off') #plt.show() plt.savefig(imgname, bbox_inches='tight', pad_inches=0, transparent=True) print("trial : {}의 ch{} 이미지 생성".format(cnt, idx + 1)) # break cnt = cnt + 1 ## ////////////////////////////////// ## break
plt.ylabel('PSD [V**2/Hz]') plt.show() #%% only noise fs = 10e3 N = 1e5 noise_power = 0.001 * fs / 2 time = np.arange(N) / fs #x = amp*np.sin(2*np.pi*freq*time) x = np.random.normal(scale=np.sqrt(noise_power), size=time.shape) #COT_intp(x,tTacho,PPR,fs,SampPerRev) #x = x+np.exp(1j*2*np.pi*2e3*time) dtx = np.cos(2 * np.pi * (200 * time / 2 + 100) * time) #deterministic #x = x+dtx x = x * dtx f, Pxx_den = signal.welch(x, fs, nperseg=512) plt.semilogy(f, Pxx_den) plt.ylim([0.5e-3, 1]) plt.xlim([0, fs / 2]) plt.xlabel('frequency [Hz]') plt.ylabel('PSD [V**2/Hz]') plt.show() #%% f, t, Zxx = signal.stft(x, fs=fs, nperseg=2**8) #% ploting STFT plt.figure() plt.pcolormesh(t, f, np.abs(Zxx)) plt.ylim([f[1], f[-1]]) plt.title('STFT Magnitude') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.show()
EPOCHS = 20 # tf.config.experimental_run_functions_eagerly(True) #model.fit(dataset, epochs=EPOCHS) history = model.fit(X[:20, :, :, :], M['vocals'][:20, :, :, :], batch_size=2, epochs=EPOCHS) """# 预测""" track = [mus[-1]] X, M = dataset(track) X_origin = stft(track[0].audio.T, nperseg=4096, noverlap=3072)[-1] print(X.shape, len(M), M['vocals'].shape, X_origin.shape) M_predict = model.predict(X) print(M_predict.shape) MM_predict = { 'vocals': M_predict, 'drums': M_predict, 'bass': M_predict, 'other': M_predict } def ichop(X_origin, M, time_scale=240, ratio_overlap=0.5): # 输入只一首歌
segment_num = 0 for line in wf.readlines(): # remove endline if present line = line[:line.find('\n')] segment_name, _, time_beg, time_len, word, _ = line.split(' ') file_name = args.data + '/split/' + split + '/' + segment_name.replace('-', '/')[:segment_name.rfind('-')+1] + segment_name + '.flac' if file_name != current_file_name: audio = sf.read(file_name) audio = audio[0] current_file_name = file_name segment_num = 0 start = int(float(time_beg) * 16000) end = int((float(time_beg) + float(time_len)) * 16000) f, t, seg_stft = stft(audio[start:end], window='hamming', nperseg=256, noverlap=128) h5f = h5py.File(args.dest_path + '/' + split + '/' + segment_name + '_' + str(segment_num) + '.h5', 'w') h5f.create_dataset('word_idx', data=[dictionary.index(word.lower())]) h5f.create_dataset('mag', data=np.abs(seg_stft)) h5f.close() segment_num = segment_num + 1
import soundfile as sf import numpy as np from sklearn.metrics import mean_squared_error clean_files = sorted(glob('stft_test_file/*.flac')) # test for stft and istft print("full complex number") mse = np.array([]) for file in clean_files: audio, _s = sf.read(file) audio = np.asarray(audio) # print("audio length=", len(audio)," ", len(audio)/16000, "s") _t, _f, Zxx = stft(audio, window='hamming', nperseg=256, noverlap=128, padded=True) # print("shape after stft =", Zxx.shape,"total: ",Zxx.shape[0],"*",Zxx.shape[1],"=",Zxx.shape[0]*Zxx.shape[1]) _t, audio_recovered = istft(Zxx, window='hamming', nperseg=256, noverlap=128) mag_only = np.abs(Zxx) _t, audio_mag_only_recoverd = istft(mag_only, window='hamming', nperseg=256, noverlap=128) _t, _f, Zxx_mag = stft(audio_mag_only_recoverd, window='hamming', nperseg=256,
def prepare_main_input(audioFile, visualFeaturesFile, targetFile, noise, reqInpLen, charToIx, noiseSNR, audioParams, videoParams): """ Function to convert the data sample in the main dataset into appropriate tensors. """ if targetFile is not None: #reading the target from the target file and converting each character to its corresponding index with open(targetFile, "r") as f: trgt = f.readline().strip()[7:] trgt = [charToIx[char] for char in trgt] trgt.append(charToIx["<EOS>"]) trgt = np.array(trgt) trgtLen = len(trgt) #the target length must be less than or equal to 100 characters (restricted space where our model will work) if trgtLen > 100: print("Target length more than 100 characters. Exiting") exit() #STFT feature extraction stftWindow = audioParams["stftWindow"] stftWinLen = audioParams["stftWinLen"] stftOverlap = audioParams["stftOverlap"] sampFreq, inputAudio = wavfile.read(audioFile) #pad the audio to get atleast 4 STFT vectors if len(inputAudio) < sampFreq * (stftWinLen + 3 * (stftWinLen - stftOverlap)): padding = int( np.ceil((sampFreq * (stftWinLen + 3 * (stftWinLen - stftOverlap)) - len(inputAudio)) / 2)) inputAudio = np.pad(inputAudio, padding, "constant") inputAudio = inputAudio / np.max(np.abs(inputAudio)) #adding noise to the audio if noise is not None: pos = np.random.randint(0, len(noise) - len(inputAudio) + 1) noise = noise[pos:pos + len(inputAudio)] noise = noise / np.max(np.abs(noise)) gain = 10**(noiseSNR / 10) noise = noise * np.sqrt( np.sum(inputAudio**2) / (gain * np.sum(noise**2))) inputAudio = inputAudio + noise #normalising the audio to unit power inputAudio = inputAudio / np.sqrt(np.sum(inputAudio**2) / len(inputAudio)) #computing STFT and taking only the magnitude of it _, _, stftVals = signal.stft(inputAudio, sampFreq, window=stftWindow, nperseg=sampFreq * stftWinLen, noverlap=sampFreq * stftOverlap, boundary=None, padded=False) audInp = np.abs(stftVals) audInp = audInp.T #loading the visual features vidInp = np.load(visualFeaturesFile) #padding zero vectors to extend the audio and video length to a least possible integer length such that #video length = 4 * audio length if len(audInp) / 4 >= len(vidInp): inpLen = int(np.ceil(len(audInp) / 4)) leftPadding = int(np.floor((4 * inpLen - len(audInp)) / 2)) rightPadding = int(np.ceil((4 * inpLen - len(audInp)) / 2)) audInp = np.pad(audInp, ((leftPadding, rightPadding), (0, 0)), "constant") leftPadding = int(np.floor((inpLen - len(vidInp)) / 2)) rightPadding = int(np.ceil((inpLen - len(vidInp)) / 2)) vidInp = np.pad(vidInp, ((leftPadding, rightPadding), (0, 0)), "constant") else: inpLen = len(vidInp) leftPadding = int(np.floor((4 * inpLen - len(audInp)) / 2)) rightPadding = int(np.ceil((4 * inpLen - len(audInp)) / 2)) audInp = np.pad(audInp, ((leftPadding, rightPadding), (0, 0)), "constant") #checking whether the input length is greater than or equal to the required length #if not, extending the input by padding zero vectors if inpLen < reqInpLen: leftPadding = int(np.floor((reqInpLen - inpLen) / 2)) rightPadding = int(np.ceil((reqInpLen - inpLen) / 2)) audInp = np.pad(audInp, ((4 * leftPadding, 4 * rightPadding), (0, 0)), "constant") vidInp = np.pad(vidInp, ((leftPadding, rightPadding), (0, 0)), "constant") inpLen = len(vidInp) audInp = torch.from_numpy(audInp) vidInp = torch.from_numpy(vidInp) inp = (audInp, vidInp) inpLen = torch.tensor(inpLen) if targetFile is not None: trgt = torch.from_numpy(trgt) trgtLen = torch.tensor(trgtLen) else: trgt, trgtLen = None, None return inp, trgt, inpLen, trgtLen
def to_matrix(self) -> NDArray[(Any, Any), Complex128]: freqs, times, spec = sig.stft(self.audio) return spec
re_im = np.concatenate((im, re), axis=1) del re, im #Add noise to the signal sd = np.std(re_im) noise = np.random.normal(0, sd * 0.01, (re_im.shape)) re_im_noisy = re_im + noise #Normalize the data from sklearn import preprocessing norm = preprocessing.Normalizer() norm_signal = norm.transform(re_im_noisy) ## Create the time-frequency map figures for each fingerprint (STFT) for i in range(norm_signal.shape[0]): f, t, Zxx = signal.stft(norm_signal[i, :], fs=1, window='hann', nperseg=300) image = plt.pcolormesh(t, f, np.abs(Zxx), cmap='nipy_spectral', shading='gouraud') plt.axis('off') plt.savefig("C:/{path}/re_im_norm_STFT/STFT_Fingerprint-%d.png" % i, bbox_inches='tight', pad_inches=0) del image plt.cla()
f_name = os.path.basename(cur_line) for ch in range(1, 9): if args.is_real == 1: dir_path = os.path.dirname(cur_line) f_name = os.path.basename(cur_line) part1 = f_name.split('-')[0] part2 = f_name.split('-')[1] part3 = f_name.split('-')[2].split('_')[1] f_name_new = part1 + '-' + part2 + '-' + '{}'.format(ch) + '_' + part3 f = os.path.join(dir_path, f_name_new) elif args.is_real == 0: f_name_no_ch = f_name.split('_')[0] f_with_ch = f_name_no_ch + '_ch{}.wav'.format(ch) f = os.path.join(dir_path, f_with_ch) audio_data = read_audio(f) fx, tx, s_x = signal.stft(audio_data, fs=16000, nperseg=512, noverlap=512-128, nfft=512) s_x = np.transpose(s_x) if ch == 1: T, F = s_x.shape Y = np.zeros((8, T, F), dtype=np.complex64) Y[ch-1, :, :] = s_x s_x_abs = 20 * log_sp(np.abs(s_x)) s_x_abs = stack_features(s_x_abs.astype(np.float32), 5) s_x_abs = Variable(s_x_abs) if args.gpu >= 0: s_x_abs.to_gpu(args.gpu) s_x_abs_list.append(s_x_abs) elif args.single == 1: audio_data = read_audio(cur_line) fx, tx, s_x = signal.stft(audio_data, fs=16000, nperseg=512, noverlap=512-128, nfft=512)
def st_ft(self): f, t, Zxx = stft(self.signal.reshape((-1)), fs=self.fs, detrend='linear') return f, t, Zxx
# Input data and resample mix = utils.load_wav(input_dir, STFTPara['fs']) ns = mix.shape[1] # STFT frames_ = np.floor((mix.shape[0] + 2 * STFTPara['window_shift']) / STFTPara['window_shift']) # to meet NOLA frames = int(np.ceil(frames_ / 8) * 8) X = np.zeros( (int(STFTPara['window_size'] / 2 + 1), int(frames), mix.shape[1]), dtype=np.complex) for n in range(mix.shape[1]): f, t, X[:, :int(frames_), n] = signal.stft( mix[:, n], nperseg=STFTPara['window_size'], window=STFTPara['type'], noverlap=STFTPara['window_size'] - STFTPara['window_shift']) # source separation Y = MVAE(X, AlgPara, NNPara, gpu) # projection back XbP = np.zeros((X.shape[0], X.shape[1], 1), dtype=np.complex) XbP[:, :, 0] = X[:, :, AlgPara['RefMic']] Z = utils.back_projection(Y, XbP) # iSTFT and save for n in range(ns): sep = signal.istft(Z[:, :, n], window=STFTPara['type'])[1] sep = sep / max(abs(sep)) * 30000
f, t, Zxx = stft(data, fs, nperseg=255, window = 'cosine', noverlap = 0.5 * 255) dataToSave[i]= abs(Zxx) i = i+1 """ for emotion in emotions.values(): for root, dirs, files in os.walk(absPath + '/dataToProcess' + '/' + emotion): dataToSave = [] isEmpty = True for file in files: fileName = root + '/' + file freq, data = readWav(fileName) f, t, Zxx = stft(data, fs, nperseg=255, window='cosine', noverlap=0.5 * 255) n = math.floor(Zxx.shape[1] / Zxx.shape[0]) for i in range(0, n): if isEmpty: nCols = 0 isEmpty = False else: nCols = dataToSave.shape[1] dataToSave = np.append( dataToSave, abs(Zxx[:, i * 128:(i + 1) * 128])).reshape(128, nCols + 128) if os.path.exists(absPath + '/hdf5_data.h5'):
from matplotlib.lines import Line2D from scipy import signal import numpy as np import threading import time from datetime import timedelta # Load the audio and get the raw data for transformation sound = AudioSegment.from_mp3("A Day Without Rain - Enya - Flora's Secret.mp3") sampling_rate = sound.frame_rate song_length = sound.duration_seconds left = sound.split_to_mono()[0] x = left.get_array_of_samples() # Fourier transform f, t, Zxx = signal.stft(x, fs=sampling_rate, nperseg=8820, noverlap=5292) y = np.abs(Zxx.transpose()) # Setup a separate thread to play the music music_thread = threading.Thread(target=play, args=(sound, )) class MusicAnimation(animation.TimedAnimation): # Matplotlib function to initialize animation def __init__(self, x, y): # Build the figure self.x = x self.y = y self._fig = plt.figure(figsize=(14, 6)) plt.style.use('seaborn-bright')
from matplotlib import pyplot as plt import sys # window sliding이 들어간 시간순 stft rawdatas = np.load('Rawdatas.npy', mmap_mode='r') print(np.shape(rawdatas)) type_n = np.shape(rawdatas)[1] print(30208000 / 2156) print(64 * 64) list = [] for c in range(0, type_n): print("count", c) datas = np.transpose(rawdatas)[0] _, _, Zxx = signal.stft(datas, nperseg=64 * 64, nfft=64 * 64) print(np.shape(Zxx)) # print(np.shape(Zxx), Zxx[0]) # Zxx = Zxx.reshape(8193, -1, 15) Zxx = np.transpose(Zxx) list.append(Zxx) list = np.stack(list) print(np.shape(list)) # 뭔가 잘못함 list = np.transpose(list, (1, 2, 0)) # for i in range(6, 30): # if np.shape(Zxx)[0] % i == 0: # divisor = i # print(i) # break
# spikes, states = generate_data(batch_size=batch_size, , ) # plt.plot(spikes.numpy()[0, :, :]) # plt.savefig('spikes.pdf') # plt.show() if 0: tmp_last_spikes = tf.transpose(spikes, [0, 2, 1]) result_tf, result_np = stft(tmp_last_spikes, window, window_size, overlap, debug=True) tmp_f, tmp_t, sci_res = scisig.stft(tmp_last_spikes.numpy(), window=window, nperseg=window_size, noverlap=overlap, axis=-1) # debug_here() plt.imshow(np.log((np.abs(result_tf.numpy()[0, 0, :, :].T)))) plt.colorbar() plt.show() error = np.linalg.norm((np.transpose(result_tf.numpy(), [0, 1, 3, 2]) - sci_res).flatten()) print('machine precision:', np.finfo(result_tf.numpy().dtype).eps) print('error_tf_scipy', error) error2 = np.linalg.norm( (np.transpose(result_np, [0, 1, 3, 2]) - sci_res).flatten())
import scipy.io.wavfile as wf import matplotlib.pyplot as plt from scipy import signal import numpy as np fmin = 0 fmax=0 lines = open('./giantsteps-tempo-dataset-master/splits/files.txt','r').readlines() for line in lines: fil_path= "./giantsteps-tempo-dataset-master/audio/"+line[:-4]+"wav" rate, data = wf.read(fil_path) amp = 20 * np.sqrt(20) f, t, Zxx = signal.stft(data, rate) print(line[:-5],len(f)) plt.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=amp) plt.title('STFT Magnitude') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.show() # In[11]: fil_path= "./giantsteps-tempo-dataset-master/audio/1479462.LOFI.wav" rate, data = wf.read(fil_path) f, t, Zxx = signal.stft(data, rate) song = signal.istft(Zxx,rate)
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()
def draw(x): #print "draw",x savefigdir = const.figdir + str(x) sdfdir = const.sdfdir + str(x).zfill(const.filenumber) + ".sdf" data = sdf.read(sdfdir, dict=True) #data=sdf.read(const.sdfdir+str(x).zfill(const.filenumber)+".sdf",dict=True) Bz = data['Electric Field/Ey'] global T time = data['Header']['time'] if time - const.window_start_time < 0: T = 0 else: T = time - const.window_start_time bz = Bz.data density = data['Derived/Number_Density/electron1'].data bz = bz.T density = density.T index = int(const.Ny / 2) bz_y0 = bz[index] k0 = 2 * 3.14 / const.lamada k1 = 2 * 3.14 / 1e-6 fs = 2 * 3.14 / const.delta_x / k0 f, t, zxx = signal.stft(bz_y0, fs=2 * 3.14 / const.delta_x, nperseg=const.nperseg) print t, t.shape ne = data['Derived/Number_Density/electron1'].data ne_y0 = ne[..., int(const.Ny / 2)] fig, axs = plt.subplots(2, 2) #plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0) ref = func.ref_a(x) ref = np.array(ref) im = axs[0][0].pcolormesh(bz, cmap=plt.get_cmap('bwr')) #fig.colorbar(im,ax=axs[0][0]) #print ref line2 = axs[1][0].plot(ref) #(density,cmap=plt.get_cmap('gray')) im3 = axs[0][1].pcolormesh(t * 2 * 3.14 / 1e-6, f / k0, np.abs(zxx), cmap=plt.get_cmap('BuPu'), shading='gouraud') #Xf_list=wigner.wigner(x) #im3=axs[1][0].pcolormesh(Xf_list,cmap=plt.get_cmap('BuPu'),rasterized=True) #im=axs[1][0].imshow(np.abs(zxx),extent=[],cmap=plt.get_cmap('BuPu')) line3 = axs[1][1].plot(ne_y0, 'g') ax2 = axs[1][1].twinx() #ax3=axs[0][1].twinx() hx = fftpack.hilbert(bz_y0) hy = np.sqrt(bz_y0**2 + hx**2) line4 = ax2.plot(hy, 'r') #line5=axs[0][1].plot(ref) #ax3.set_ylim((0.9,1,1)) axs[1][0].set_ylim((0.6, 1.2)) axs[1][0].set_ylabel('refractive_index') axs[0][1].set_ylim((0, 2)) axs[0][1].set_ylabel('k/k0') axs[1][1].set_ylabel('electron density') axs[1][1].set_xlabel("um") ax2.set_ylabel('electric field') axs[0][0].set_title("t=" + str(time), fontsize=12, color='r') axs[0][0].xaxis.set_major_formatter(FuncFormatter(x_formatter)) axs[0][1].xaxis.set_major_formatter(FuncFormatter(x_formatter2)) axs[1][0].xaxis.set_major_formatter(FuncFormatter(x_formatter)) axs[1][1].xaxis.set_major_formatter(FuncFormatter(x_formatter)) axs[1][1].xaxis.set_minor_locator(MultipleLocator(100)) ### fig.savefig(png_savedir + str(x) + "ref_k.png", dpi=200) image_list.append(png_savedir + str(x) + "ref_k.png") plt.close('all')
def spectral_analysis(data): """ A simple analysis for how to do spectral analysis on a stream, <data>, in real time. Also illustrates how plots can be updated live. """ sample_rate = 10000 seconds = 35 data_length = seconds * sample_rate seg_length = 500 noverlap = 0.8 segments = data_length // (seg_length * (1 - noverlap)) segments_per_second = segments // seconds num_buckets = 50 f, t, Zxx = stft(data[:data_length], fs=10000, window='hamming', nperseg=seg_length, noverlap=seg_length * noverlap) f = f[:num_buckets] Zxx = np.abs(Zxx[:num_buckets, :]) # Normal method of doing this -- looks a slight bit different with # vmin, vmax, cmap. # fig = plt.pcolormesh(t, f, np.abs(Zxx)) # plt.show() # Main figure. fig = plt.figure(1) # Axes of imshow for raw data. ax_raw = fig.add_subplot(211) im_raw, = ax_raw.plot(data[:data_length], color='#EB9904') # Axes of imshow for spectrum. ax_spec = fig.add_subplot(212) im_spec = ax_spec.imshow(Zxx, extent=[0, t[-1], 0, f[-1]], aspect='auto', origin='lowest', cmap='jet') pause = False def on_click(event): if event.key == 'p': nonlocal pause pause ^= True ticks = 0 def update_data(n): if pause: return nonlocal ticks ticks += 1 # Update raw plot. start = int(ticks / 10 * sample_rate) end = int((ticks + 30) / 10 * sample_rate) ax_raw.clear() ax_raw.set_ylim(-100, 100) ax_raw.plot(data[start:end], color='#EB9904') # Update spectrogram. start = int(ticks * segments_per_second * 0.1) end = int((ticks + 30) * segments_per_second * 0.1) im_spec.set_extent([ticks / 10, (ticks + 30) / 10, 0, f[-1]]) im_spec.set_data(Zxx[:, start:end]) fig.canvas.mpl_connect('key_press_event', on_click) ani = animation.FuncAnimation(fig, update_data, interval=100, blit=False, repeat=False) plt.show()
def STFT(fl): f, t, Zxx = signal.stft(fl, nperseg=64) img = np.abs(Zxx) / len(Zxx) return img
#畳み込んだ波形をファイルに書き込む write_file_from_time_signal( multi_conv_data_right_no_noise[0, :] * np.iinfo(np.int16).max / 20., "./ica_right_clean.wav", sample_rate) #畳み込んだ波形をファイルに書き込む write_file_from_time_signal( multi_conv_data[0, :] * np.iinfo(np.int16).max / 20., "./ica_in_left.wav", sample_rate) write_file_from_time_signal( multi_conv_data[0, :] * np.iinfo(np.int16).max / 20., "./ica_in_right.wav", sample_rate) #短時間フーリエ変換を行う f, t, stft_data = sp.stft(multi_conv_data, fs=sample_rate, window="hann", nperseg=N) #ICAの繰り返し回数 n_ica_iterations = 50 #ICAの分離フィルタを初期化 Wica = np.zeros(shape=(Nk, n_sources, n_sources), dtype=np.complex) Wica = Wica + np.eye(n_sources)[None, ...] Wiva = Wica.copy() Wiva_ip = Wica.copy() start_time = time.time() #自然勾配法に基づくIVA実行コード(引数に与える関数を変更するだけ)
def sequentialized_spectrum(batch): # Get maximum length of batch t = [] t_vec = [] Sxx_Vec = [] for each in batch: _, t, Sxx_Vec_Temp = signal.stft(each, fs=rate_repository[0], nperseg=stft_size, return_onesided=False) t_vec.append(t) Sxx_Vec.append(Sxx_Vec_Temp) maximum_length = findMaxlen(t_vec) max_run_total = int(math.ceil(float(maximum_length) / sequence_length)) final_data = np.zeros( [len(batch), max_run_total, stft_size, sequence_length], dtype=np.float32) true_time = np.zeros([len(batch), max_run_total], dtype=np.int32) # Read in a file and compute spectrum # for batch_idx, each_set in enumerate(batch): for batch_idx, Sxx in enumerate(Sxx_Vec): # f, t, Sxx = signal.stft(each_set, fs=rate_repository[0], nperseg=stft_size, return_onesided = False) # Magnitude and Phase Spectra # Mag = Sxx.real Mag = Sxx.imag # Get imaginary part of Sxx. This will try to get a imaginary model. t = t_vec[batch_idx] # # TESTING # _, outputData_ISTFT = signal.istft(Mag, fs=rate_repository[0], nperseg=stft_size, # input_onesided=False) # # outputData_ISTFT = ((outputData_ISTFT / norm_factor).real) / 0.75 # outputData_ISTFT = outputData_ISTFT.astype(np.int16) # # wav.write("0.TEST_REAL.wav", rate_repository[idx], outputData_ISTFT) # # _, outputData_ISTFT = signal.istft(Sxx, fs=rate_repository[0], nperseg=stft_size, # input_onesided=False) # # outputData_ISTFT = ((outputData_ISTFT / norm_factor).real) / 0.75 # outputData_ISTFT = outputData_ISTFT.astype(np.int16) # # wav.write("0.TEST_ORIG.wav", rate_repository[idx], outputData_ISTFT) # # TESTING END # Break up the spectrum in sequence_length sized data run_full_steps = float(len(t)) / sequence_length run_total = int(math.ceil(run_full_steps)) # Run a loop long enough to break up all the data in the file into chunks of sequence_size for step in range(run_total): begin_point = step * sequence_length end_point = begin_point + sequence_length m, n = Mag[:, begin_point:end_point].shape # Store each chunk sequentially in a new array, accounting for zero padding when close to the end of the file if n == sequence_length: final_data[batch_idx, step, :, :] = np.copy(Mag[:, begin_point:end_point]) true_time[batch_idx, step] = n else: final_data[batch_idx, step, :, :] = np.copy( create_final_sequence(Mag[:, begin_point:end_point], sequence_length)) true_time[batch_idx, step] = n final_data = np.transpose(final_data, (0, 1, 3, 2)) return final_data, true_time, maximum_length
def generate_features(local_fs, iq_data, spec_size=256, roll=True, plot_features=False, overlap=NOVERLAP): """ Generate classification features """ ## Generate normalised spectrogram NFFT = calculate_nseg(len(iq_data)) #Arb constant... Need to analyse TODO #print("NFFT:", NFFT) f, t, Zxx_cmplx = signal.stft(iq_data, local_fs, noverlap=NFFT * overlap, nperseg=NFFT, return_onesided=False) Zxx_mag = np.abs(np.power(Zxx_cmplx, 2)) Zxx_mag_fft = np.abs(fftn(Zxx_mag)) Zxx_mag_fft = np.fft.fftshift(Zxx_mag_fft) Zxx_mag_log = log_enhance(Zxx_mag, order=2) diff_array0 = np.diff(Zxx_mag_log, axis=0) diff_array1 = np.diff(Zxx_mag_log, axis=1) diff_array0_rs = normalise_spectrogram(diff_array0, spec_size, spec_size) diff_array1_rs = normalise_spectrogram(diff_array1, spec_size, spec_size) Zxx_phi = np.abs(np.unwrap(np.angle(Zxx_cmplx), axis=0)) Zxx_cec = np.abs(np.corrcoef(Zxx_mag_log, Zxx_mag_log)) Zxx_mag_rs = normalise_spectrogram(Zxx_mag_log, spec_size, spec_size) Zxx_phi_rs = normalise_spectrogram(Zxx_phi, spec_size, spec_size) Zxx_cec_rs = normalise_spectrogram(Zxx_cec, spec_size * 2, spec_size * 2) Zxx_cec_rs = blockshaped(Zxx_cec_rs, spec_size, spec_size) Zxx_cec_rs = Zxx_cec_rs[0] # We have a array suitable for NNet ## Generate spectral info by taking mean of spectrogram ## PSD = np.mean(Zxx_mag_rs, axis=1) Varience_Spectrum = np.var(Zxx_mag_rs, axis=1) Differential_Spectrum = np.sum(np.abs(diff_array1_rs), axis=1) Min_Spectrum = np.min(Zxx_mag_rs, axis=1) Max_Spectrum = np.max(Zxx_mag_rs, axis=1) Zxx_mag_hilb = np.abs(signal.hilbert(Zxx_mag.astype(np.float), axis=1)) if plot_features: nx = 3 ny = 4 plt.subplot(nx, ny, 1) plt.title("Magnitude Spectrum") plt.xlabel("Time") plt.ylabel("Frequency") plt.pcolormesh(Zxx_mag_rs) ## +1 to stop 0s plt.subplot(nx, ny, 2) plt.title("Max Spectrum") plt.xlabel("Frequency") plt.ylabel("Power") plt.plot(Max_Spectrum) plt.subplot(nx, ny, 3) plt.title("PSD") plt.xlabel("Frequency") plt.ylabel("Power") plt.plot(PSD) plt.subplot(nx, ny, 4) plt.title("Autoorrelation Coefficient (Magnitude)") plt.pcolormesh(Zxx_cec_rs) plt.subplot(nx, ny, 5) plt.title("Frequency Diff Spectrum") plt.xlabel("Time") plt.ylabel("Frequency") plt.pcolormesh(diff_array0) plt.subplot(nx, ny, 6) plt.title("Time Diff Spectrum") plt.xlabel("Time") plt.ylabel("Frequency") plt.pcolormesh(diff_array1) plt.subplot(nx, ny, 7) plt.title("Varience Spectrum") plt.xlabel("Frequency") plt.ylabel("Power") plt.plot(Varience_Spectrum) plt.subplot(nx, ny, 8) plt.title("Differential Spectrum") plt.xlabel("Frequency") plt.ylabel("Power") plt.plot(Differential_Spectrum) plt.subplot(nx, ny, 9) plt.title("Min Spectrum") plt.xlabel("Frequency") plt.ylabel("Power") plt.plot(Min_Spectrum) plt.subplot(nx, ny, 10) plt.title("FT Spectrum") plt.xlabel(" ") plt.ylabel(" ") plt.pcolormesh(Zxx_mag_fft) plt.subplot(nx, ny, 11) plt.title("Hilbert Spectrum") plt.xlabel(" ") plt.ylabel(" ") plt.pcolormesh(Zxx_mag_hilb) mng = plt.get_current_fig_manager() ## Make full screen mng.full_screen_toggle() plt.show() output_dict = {} output_dict['magnitude'] = Zxx_mag_rs output_dict['phase'] = Zxx_phi_rs output_dict['corrcoef'] = Zxx_cec_rs output_dict['psd'] = PSD output_dict['variencespectrum'] = Varience_Spectrum output_dict['differentialspectrumdensity'] = Differential_Spectrum output_dict['differentialspectrum_freq'] = diff_array0_rs output_dict['differentialspectrum_time'] = diff_array1_rs output_dict['min_spectrum'] = Min_Spectrum output_dict['max_spectrum'] = Max_Spectrum output_dict['fft_spectrum'] = normalise_spectrogram( Zxx_mag_fft, spec_size, spec_size) output_dict['hilb_spectrum'] = normalise_spectrogram( Zxx_mag_hilb, spec_size, spec_size) return output_dict
titles = ['spatial nonmatch feature nonmatch','spatial match feature nonmatch','spatial nonmatch feature match','spatial match feature match'] if setting['detrend'] == 1: for i in range(4): for j in range(len(yy_list)): model = np.polyfit(tt, to_plot[i, :, j], 2) predicted = np.polyval(model, tt) to_plot[i, :, j] = to_plot[i, :, j] - predicted ylim = [-0.4,0.4] if setting['plot_fft'] == 1 else [-0.2,0.2] if setting['plot_fft'] == 1: temp = [] for i in range(4): fft_i = [] for j in range(len(yy_list)): f, t, yf = signal.stft(to_plot[i, :, j], 100, window='hann', nperseg=len(tt)) fft_i.append(np.abs(yf[:,1])) temp.append(np.stack(fft_i,axis = 1)) to_plot = np.stack(temp) x = f xlim = [0,15] ylim = [0,10] if setting['plotting'] == 'rt' else [0,0.1] def plot_err_shade(x,Y,type='std'): if setting['shuffleYN']: Y = np.reshape(Y,[Y.shape[0],len(uniq_subj),1000]) Y = np.mean(Y,axis=1) y = np.mean(Y,axis=1) if type=='std': error = 1.96*np.std(Y,axis=1) elif type=='se':