def Hamming(self, button5, button8, button4): button5.config(state="disabled") button8.config(state="disabled") button4.config(state="disabled") if (self.canvas2 == None): self.x = 1 rate, k = wavfile.read(filename) self.f1, self.a1 = plot.subplots(1) self.a1.clear() self.a1.set_title('Spectro') plot.xlabel("Time(s)") plot.ylabel("frequency(Hz)") try: self.a1.specgram(k, Fs=rate, window=scipy.hamming(256), NFFT=256, cmap='Purples') except ValueError: k = k[:, 1] self.a1.specgram(k, Fs=rate, window=scipy.hamming(256), NFFT=256, cmap='Purples') self.canvas2 = FigureCanvasTkAgg(self.f1) self.canvas2.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True) self.toolbar = NavigationToolbar2Tk(self.canvas2, app) self.toolbar.update() self.canvas2._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True) self.f2, self.a2 = plot.subplots(1) Time = np.linspace(0, len(k) / rate, num=len(k)) self.a2.clear() self.a2.set_title('Sound') self.a2.plot(Time, k, color="m") plot.xlabel("Time(s)") plot.ylabel("Amplitude") #formatter = matplotlib.ticker.FuncFormatter(lambda ms, x: time.strftime('%M:%S', time.gmtime(ms // 1000))) #self.a2.xaxis.set_major_formatter(formatter) self.canvas3 = FigureCanvasTkAgg(self.f2) self.canvas3.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True) self.canvas3._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
def fft(self, window="hanning", nfft=None): from numpy.fft.fftpack import fft as npfft from numpy.fft import fftfreq as npfftfreq from scipy import hamming, hanning sig = self.get_data() n = sig.shape[0] if window == "hamming": win = hamming(n) elif window == "hanning": win = hanning(n) elif window == "square": win = 1 else: raise StandardError("Windows is not %s" % (window,)) #: FFT, 折り返しこみ if nfft is None: nfft = n spec = npfft(sig * win, n=nfft) #: Freq, 折り返しこみ freq = npfftfreq(nfft, d=1. / self.get_fs()) # : 折り返しを削除して返却 se = round(nfft / 2) spectrum = SpectrumData(data=spec[:se], xdata=freq[:se], name=self.name) spectrum.set_fs(self.get_fs()) return spectrum
def __init__(self, win_size=320, hop_size=160, requires_grad=False): super(STFT, self).__init__() self.win_size = win_size self.hop_size = hop_size self.n_overlap = self.win_size // self.hop_size self.requires_grad = requires_grad win = torch.from_numpy(scipy.hamming(self.win_size).astype(np.float32)) win = F.relu(win) win = nn.Parameter(data=win, requires_grad=self.requires_grad) self.register_parameter('win', win) fourier_basis = np.fft.fft(np.eye(self.win_size)) fourier_basis_r = np.real(fourier_basis).astype(np.float32) fourier_basis_i = np.imag(fourier_basis).astype(np.float32) self.register_buffer('fourier_basis_r', torch.from_numpy(fourier_basis_r)) self.register_buffer('fourier_basis_i', torch.from_numpy(fourier_basis_i)) idx = torch.tensor(range(self.win_size // 2 - 1, 0, -1), dtype=torch.long) self.register_buffer('idx', idx) self.eps = torch.finfo(torch.float32).eps
def fft(self, window="hanning", nfft=None): from numpy.fft.fftpack import fft as npfft from numpy.fft import fftfreq as npfftfreq from scipy import hamming, hanning sig = self.get_data() n = sig.shape[0] if window == "hamming": win = hamming(n) elif window == "hanning": win = hanning(n) elif window == "square": win = 1 else: raise StandardError("Windows is not %s" % (window, )) #: FFT, 折り返しこみ if nfft is None: nfft = n spec = npfft(sig * win, n=nfft) #: Freq, 折り返しこみ freq = npfftfreq(nfft, d=1. / self.get_fs()) # : 折り返しを削除して返却 se = round(nfft / 2) spectrum = SpectrumData(data=spec[:se], xdata=freq[:se], name=self.name) spectrum.set_fs(self.get_fs()) return spectrum
def apply_tf(data, fftLen, step, tf_config, src_index, noise_amp=0): """ 入力波形に対して、周波数領域での伝達関数の適用を行い、逆変換を行い、多チャンネル波形の形式で出力する Args: data (ndarray): 単チャンネル信号(#sample) fftLen (int): 窓関数 step (int): シフト幅 tf_config (Dict): HARK_TF_PARSERで取得できる伝達関数 src_index (int): 伝達関数インデックス noise_amp (float): ノイズの大きさ Returns: ndarray: 伝達関数適用後の信号(channel x #sample) """ win = hamming(fftLen) # ハミング窓 mch_data = apply_tf_spec(data, fftLen, step, tf_config, src_index, noise_amp) out_wavdata = [] for mic_index in range(mch_data.shape[0]): spec = mch_data[mic_index] ### iSTFT resyn_data = micarrayx.istft(spec, win, step) out_wavdata.append(resyn_data) # concat waves mch_wavdata = np.vstack(out_wavdata) return mch_wavdata
def noise_detection(ntime): with open('hoge.wav', 'rb') as f: data = np.frombuffer(f.read()[44:], dtype='int16').astype( np.float32) / 32767 start = int(ntime * 48000 / 1024) end = start + 1024 * 3 noise_data = data[start:end] noise_data = analyze.resampling(noise_data, 48000, 16000) n_spec = sp.fft(noise_data * sp.hamming(1024)) n_pow = sp.absolute(n_spec)**2 start = 0 result = [] while True: end = start + 1024 * 3 if end > len(data): break ndata = analyze.resampling(data[start:end], 48000, 16000) ndata = analyze.spectrum_subtraction(ndata, n_pow) result.extend(ndata) start = end v = (np.array(result) * 32767).astype(np.int16) with wave.Wave_write('result.wav') as w: w.setnchannels(1) w.setsampwidth(2) w.setframerate(16000) w.writeframes(v.tobytes('C'))
def apply_tf_spec(data,fftLen, step,tf_config,src_index,noise_amp=0): win = hamming(fftLen) # ハミング窓 ### STFT spectrogram = simmch.stft(data, win, step) spec=spectrogram[:, : fftLen / 2 + 1] #spec = [4,3,2,1,2*,3*] ### Apply TF tf=tf_config["tf"][src_index] #print "# position:",tf["position"] pos=tf["position"] th=math.atan2(pos[1],pos[0])# -pi ~ pi #print "# theta(deg):",th/math.pi*180 out_data=[] for mic_index in xrange(tf["mat"].shape[0]): tf_mono=tf["mat"][mic_index] #print "# src spectrogram:",spec.shape #print "# tf spectrogram:",tf_mono.shape tf_spec=spec*tf_mono spec_c=np.conjugate(tf_spec[:,:0:-1]) out_spec=np.c_[tf_spec,spec_c[:,1:]] noise_spec=np.zeros_like(out_spec) v_make_noise = np.vectorize(make_noise) noise_spec=v_make_noise(noise_spec) out_spec=out_spec+noise_amp*noise_spec out_data.append(out_spec) mch_data=np.array(out_data) return mch_data
def stft(x, fs, frame_time, hop_time): frame_samples = int(frame_time*fs) hop_samples = int(hop_time*fs) w = scipy.hamming(frame_samples) X = scipy.array([ scipy.fft(w*x[i:i+frame_samples]) for i in range(0, len(x)-frame_samples, hop_samples)]) return X
def stft(x, framesz, hop): w = scipy.hamming(framesz) X = scipy.array([ scipy.fft(w * x[i:i + framesz]) for i in range(0, len(x) - framesz, hop) ]) return X
def test_sweep(self): amp = db_to_ratio(-6) te = 8 t = numpy.linspace(0, 8, self.from_rate * 8) x = signal.chirp(t, f0=0, f1=self.from_rate, t1=te, method='quadratic') * amp print("done chirp") y = self.resample(x) print("done filtering") nfft = 64 win = scipy.hamming(nfft) plot.subplot(211) plot.specgram(x, NFFT=nfft, Fs=self.from_rate, noverlap=nfft / 2, window=win) plot.subplot(212) plot.specgram(y, NFFT=nfft, Fs=self.to_rate, noverlap=nfft / 2, window=win) plot.show()
def stft(x, fs, framesz, hop): framesamp = int(framesz*fs) hopsamp = int(hop*fs) w = scipy.hamming(framesamp) X = scipy.array([scipy.fft(w*x[i:i+framesamp]) for i in range(0, len(x)-framesamp, hopsamp)]) return X
def apply_tf_spec(data, fftLen, step, tf_config, src_index, noise_amp=0): win = hamming(fftLen) # ハミング窓 ### STFT spectrogram = simmch.stft(data, win, step) spec = spectrogram[:, :fftLen / 2 + 1] #spec = [4,3,2,1,2*,3*] ### Apply TF tf = tf_config["tf"][src_index] #print "# position:",tf["position"] pos = tf["position"] th = math.atan2(pos[1], pos[0]) # -pi ~ pi #print "# theta(deg):",th/math.pi*180 out_data = [] for mic_index in xrange(tf["mat"].shape[0]): tf_mono = tf["mat"][mic_index] #print "# src spectrogram:",spec.shape #print "# tf spectrogram:",tf_mono.shape tf_spec = spec * tf_mono spec_c = np.conjugate(tf_spec[:, :0:-1]) out_spec = np.c_[tf_spec, spec_c[:, 1:]] noise_spec = np.zeros_like(out_spec) v_make_noise = np.vectorize(make_noise) noise_spec = v_make_noise(noise_spec) out_spec = out_spec + noise_amp * noise_spec out_data.append(out_spec) mch_data = np.array(out_data) return mch_data
def stft(x, fs, framesz, hop): "short-term frequency transform" framesamp = int(framesz*fs) hopsamp = int(hop*fs) w = scipy.hamming(framesamp) X = scipy.array([scipy.fft(w*x[i:i+framesamp]) for i in range(0, len(x)-framesamp, hopsamp)]) #freqs = fftfreq(framesamp, hop) return X#,freqs
def get_angle(rci, nfft=128): """ Provides the Range-Angle image """ win = hamming(rci.shape[0]) win.shape = (len(win), 1) win = win.repeat(rci.shape[1], axis=1) return fftpack.fft(win * rci, nfft, axis=0)
def stft(input_data, sample_rate, window_size, hop_size): window = scipy.hamming(window_size) output = scipy.array([ scipy.fft(window * input_data[i:i + window_size]) for i in range(0, len(input_data) - window_size, hop_size) ]) return output
def add(self, x, fs, label): x = self.preprocess(x, fs) X = scipy.array([self.feature(x[i:i+self.fftsz]) for i in range(0, x.size-self.fftsz, self.hop)]) h = scipy.hamming(7)[:,None] X = scipy.signal.convolve(X, h, mode='valid') for i in range(len(X)-self.segsz): self.db.add(X[i:i+self.segsz].reshape(-1), (label, i))
def __init__(self): self.wavefile = None self.wavedata = None self.hamming_win = sp.hamming(self.STFT_SIZE) self.freq_probe_index_list = self.__fftprobe(self.STFT_SIZE, self.FREQ_PROBE_NUM, self.LOWER_HERTZ, self.UPPER_HERTZ)
def get_angle(rci, nfft=128): """ Provides the Range-Angle image """ win = hamming(rci.shape[0]) win.shape = (len(win),1) win = win.repeat(rci.shape[1],axis=1) return fftpack.fft(win*rci,nfft,axis=0)
def frame(x, fs, framesz, hop): framesamp = int(framesz * fs) hopsamp = int(hop * fs) w = scipy.hamming(framesamp) return scipy.array([ w * x[i:i + framesamp] for i in range(0, len(x) - framesamp, hopsamp) ])
def stft(x, window_len=4096, window_shift=2048): w = scipy.hamming(window_len) X = scipy.array([ scipy.fft(w * x[i:i + window_len]) for i in range(0, len(x) - window_len, window_shift) ]) return scipy.absolute(X[:, 0:window_len / 2])
def run(self, spikesorter, k_max=1, k_inc=1, from_fullband=False, median_thresh=5., consistent_across_channels = False, consistent_across_segments = True, merge_method = 'fast', sweep_clean_size = 0.8*pq.ms, ): sps = spikesorter # What is the source signal? if (from_fullband or (sps.filtered_sigs is None)): MTEO_sigs = np.empty( sps.full_band_sigs.shape, dtype = object) sigs=sps.full_band_sigs else: MTEO_sigs = np.empty( sps.filtered_sigs.shape, dtype = object) sigs=sps.filtered_sigs # Compute MTEO signals for c,s in np.ndindex(sigs.shape) : sig = sigs[c, s] kTEO_sig=np.zeros(sig.size) #compute all k-TEO, including k_max if possible for k in range(1,k_max+1,k_inc): s1 = sig[0:-2*k] s2 = sig[k:-k] s3 = sig[2*k:] # standard kTEO signal kTEO_sig[k:-k]=ne.evaluate("s2**2-s1*s3") hamm = hamming(4*(k+1)+1)#smoothing window norm=np.sqrt(3*(hamm**2.)+(hamm.sum())**2.) hamm = hamm/norm # normalization of the window #proposed by Choi et al. to prevent excess of false detections at small k kTEO_sig=np.convolve(kTEO_sig,hamm,'same') if k==1: MTEO_sig=kTEO_sig.copy() else: #take the max over all kTEO iteratively MTEO_sig=ne.evaluate("where(MTEO_sig<kTEO_sig,kTEO_sig,MTEO_sig)") MTEO_sigs[c,s]=MTEO_sig # Threshold estimation thresholds = np.zeros(MTEO_sigs.shape, dtype = float) for c, s in np.ndindex(MTEO_sigs.shape): sig = MTEO_sigs[c, s] thresholds[c, s] = abs(median_thresh) * np.median(abs(sig)) / .6745 # Detect sweep_size = int((sps.sig_sampling_rate*sweep_clean_size).simplified) sps.spike_index_array = threshold_detection_multi_channel_multi_segment( MTEO_sigs, thresholds, '+', consistent_across_channels,consistent_across_segments, sweep_size, merge_method = merge_method,)
def stft(x, window_len=kWindowLength, window_shift=kWindowShift): w = scipy.hamming(window_len) X = scipy.array([ scipy.fft(w * x[i:i + window_len]) for i in range(0, len(x) - window_len, window_shift) ]) return np.array(scipy.absolute(X[:, 0:window_len / 2]))
def toiq(frame,nfft=2048): """ Converts ADC output to range in-phase (I) and quadrature (Q) data, i.e., complex range profile """ win = hamming(frame.shape[1]) win.shape = (1,len(win)) win = win.repeat(frame.shape[0],axis=0) data = fftpack.ifft(win*frame,nfft,axis=1) return data[:,0:nfft/2]
def toiq(frame, nfft=2048): """ Converts ADC output to range in-phase (I) and quadrature (Q) data, i.e., complex range profile """ win = hamming(frame.shape[1]) win.shape = (1, len(win)) win = win.repeat(frame.shape[0], axis=0) data = fftpack.ifft(win * frame, nfft, axis=1) return data[:, 0:nfft / 2]
def stft(x, fs, framesz, hop): """ Short-time fourier transform an input """ framesamp = int(framesz*fs) hopsamp = int(hop*fs) w = scipy.hamming(framesamp) X = scipy.array([scipy.fft(w*x[i:i+framesamp]) for i in range(0, len(x)-framesamp, hopsamp)]) return X
def ramps(data, fs, duration=10, shape='raisedcosine', set='onoff'): '''Applies ramps to the onsets and/or offsets of a signal Parameters ---------- sig : array The input signal. fs : scalar The sampling frequency. duration : scalar The duration of each ramp, in ms [default = 10]. shape : string Specifies the shape of the ramp. Possibilities include: 'raisedcosine' [default] 'hanning' 'hamming' 'linear' set : string Specifies where to apply ramps: 'on' : apply to onset of signal only 'off' : apply to offest only 'onoff' : apply to both Returns ------- y : array The ramped signal. ''' dur = np.int(np.round(np.float32(duration)*(np.float32(fs)/1000.))) wspace=np.round(2.*dur) if shape is 'raisedcosine': rf = np.power((((np.cos(np.pi+2*np.pi*np.arange(0,wspace-1)/(wspace-1)))*.5)+.5),2) elif shape is 'hanning': rf = hanning(wspace) elif shape is 'hamming': rf = hamming(wspace) elif shape is 'linear': r = np.linspace(0, 1, dur) rf = np.concatenate((r, r[::-1])) else: raise Exception("shape not recognized") f_ramp = np.ones(data.shape[0]) if set in ['on', 'onoff']: f_ramp[0:dur] = rf[0:dur] if set in ['off', 'onoff']: durp1 = dur-1 f_ramp[-(durp1):] = rf[-(durp1):] # if len(data.shape) == 2: # f_ramp_1d = rf.copy() # for c in range(data.shape[1]-1): # f_ramp = np.column_stack((f_ramp, f_ramp_1d)) return (data.T * f_ramp).T
def stft(x, fs, framesz, hop): """ stft(samples, sample_rate, width, stride) width and stride are in seconds. returns iterator of fft-coefficient arrays. """ framesamp = int(framesz*fs) hopsamp = int(hop*fs) w = scipy.hamming(framesamp) return [numpy.absolute(numpy.fft.rfft(w*x[i:i+framesamp])) for i in xrange(0, len(x)-framesamp, hopsamp)]
def add(self, x, fs, label): x = self.preprocess(x, fs) X = scipy.array([ self.feature(x[i:i + self.fftsz]) for i in range(0, x.size - self.fftsz, self.hop) ]) h = scipy.hamming(7)[:, None] X = scipy.signal.convolve(X, h, mode='valid') for i in range(len(X) - self.segsz): self.db.add(X[i:i + self.segsz].reshape(-1), (label, i))
def cq_fft(sig, fs, q_rate = q_rate_def, fmin = fmin_default, fmax = fmax_default, fratio = fratio_default, win = hamming, spThresh = 0.0054): # 100 frames per 1 second nhop = int(round(0.01 * fs)) # Calculate Constant-Q Properties nfreq = get_num_freq(fmin, fmax, fratio) # number of freq bins freqs = get_freqs(fmin, nfreq, fratio) # freqs [Hz] Q = int((1. / ((2 ** fratio) - 1)) * q_rate) # Q value # Preparation L = len(sig) nframe = L / nhop # number of time frames # N > max(N_k) fftLen = int(2 ** (ceil(log2(int(float(fs * Q) / freqs[0]))))) h_fftLen = fftLen / 2 # =================== # カーネル行列の計算 # =================== sparseKernel = zeros([nfreq, fftLen], dtype = complex128) for k in xrange(nfreq): tmpKernel = zeros(fftLen, dtype = complex128) freq = freqs[k] # N_k N_k = int(float(fs * Q) / freq) # FFT窓の中心を解析部分に合わせる. startWin = (fftLen - N_k) / 2 tmpKernel[startWin : startWin + N_k] = (hamming(N_k) / N_k) * exp(two_pi_j * Q * arange(N_k, dtype = float64) / N_k) # FFT (kernel matrix) sparseKernel[k] = fft(tmpKernel) ### 十分小さい値を0にする sparseKernel[abs(sparseKernel) <= spThresh] = 0 ### スパース行列に変換する sparseKernel = csr_matrix(sparseKernel) ### 複素共役にする sparseKernel = sparseKernel.conjugate() / fftLen ### New signal (for Calculation) new_sig = zeros(len(sig) + fftLen, dtype = float64) new_sig[h_fftLen : -h_fftLen] = sig ret = zeros([nframe, nfreq], dtype = complex128) for iiter in xrange(nframe): #print iiter + 1, "/", nframe istart = iiter * nhop iend = istart + fftLen # FFT (input signal) sig_fft = fft(new_sig[istart : iend]) # 行列積 ret[iiter] = sig_fft * sparseKernel.T return ret, freqs
def stft(fs, data, frame_size=0.05, hop=0.025): frame_samp = int(frame_size * fs) hop_samp = int(hop * fs) w = scipy.hamming(frame_samp) ans = scipy.array([np.fft.rfft(w * data[i:i + frame_samp]) for i in range(0, len(data) - frame_samp, hop_samp)]) ans = scipy.log10(scipy.absolute(ans.T)) labels = {'xlabel': 'Czas [s]', 'ylabel': u'Częstotliwość [Hz]', 'zlabel': 'Amplituda'} return {'y_vector': ans, 'x_vector': None, 'labels': labels, 'yticks': get_freqs_ticks(fs, len(data), len(ans)), 'xticks': get_time_ticks(fs, len(data), len(ans.T))}
def getOutputStream(self): dataInput = (np.frombuffer(self.stream.read(self.stream.get_read_available()), dtype=np.short)[-self.frames:]) self.timeData = dataInput dataInput = dataInput / 32678.0 hammingWindow = sp.hamming(2048) self.dataStream = np.array(abs(hammingWindow * sp.fft(dataInput))[:self.frames//10]) limit = np.full(len(self.dataStream), 0.003) plt.plot(self.xAxis[:len(self.dataStream)], self.dataStream, 'b-', self.xAxis[:len(self.dataStream)], limit, 'r--') plt.pause(0.1) plt.clf() return self.dataStream
def stft(self, x, fs, framesz, hop): # framesamp = int(framesz * fs) # hopsamp = int(hop * fs) framesamp = framesz hopsamp = hop w = scipy.hamming(framesamp) X = scipy.array([scipy.fft(w * x[i:i + framesamp])for i in range(0, len(x) - framesamp, hopsamp)]) print X.shape X = X[:, :int(framesz / 2)] print "== fin stft ==" return X
def tdft(audio, srate, windowsize, windowshift,fftsize): """Calculate the real valued fast Fourier transform of a segment of audio multiplied by a a Hamming window. Then, convert to decibels by multiplying by 20*log10. Repeat for all segments of the audio.""" windowsamp = int(windowsize*srate) shift = int(windowshift*srate) window = scipy.hamming(windowsamp) spectrogram = scipy.array([20*scipy.log10(abs(np.fft.rfft(window*audio[i:i+windowsamp],fftsize))) for i in range(0, len(audio)-windowsamp, shift)]) return spectrogram
def stft(x, fs, framesz, hop): """ x - signal fs - sample rate framesz - frame size hop - hop size (frame size = overlap + hop size) """ framesamp = int(framesz * fs) hopsamp = int(hop * fs) w = scipy.hamming(framesamp) X = scipy.array([scipy.fft(w * x[i : i + framesamp]) for i in range(0, len(x) - framesamp, hopsamp)]) return X
def calcfft(data, sampling_rate): # DC component remove data = data[:] - np.mean(data) data = data * hamming(len(data)) #済 一時変数hamm_windowの削除 sample_freq = fftpack.fftfreq(data[:].size, 1. / float(sampling_rate)) y_fft = fftpack.fft(data[:]) pidxs = np.where(sample_freq > 0) _freqs, powers = sample_freq[pidxs], 10. * \ np.log10(np.abs(y_fft)[pidxs] ** 2) del _freqs return powers
def stft(x, fs, framesz, hop): "short-term frequency transform" framesamp = int(framesz * fs) hopsamp = int(hop * fs) w = scipy.hamming(framesamp) X = scipy.array([ scipy.fft(w * x[i:i + framesamp]) for i in range(0, len(x) - framesamp, hopsamp) ]) # freqs = fftfreq(framesamp, hop) return X # ,freqs
def stft0(x, fs, framesz, hop): framesamp = int(framesz * fs) hopsamp = int(hop * fs) w = scipy.hamming(framesamp) #print len(w), len(x[0:0+framesamp]), framesamp #tmp = w*x[0:0+framesamp] X = scipy.array([ scipy.fft(w * x[i:i + framesamp]) for i in range(0, len(x) - framesamp, hopsamp) ]) return X
def compute_music_power(wav_filename,tf_config, normalize_factor, fftLen,stft_step, min_freq,max_freq,src_num, music_win_size,music_step): setting={} # read wav print "... reading", wav_filename wav_data=simmch.read_mch_wave(wav_filename) wav=wav_data["wav"]/normalize_factor fs=wav_data["framerate"] # print info print "# #channels : ", wav_data["nchannels"] setting["nchannels"]=wav_data["nchannels"] print "# sample size : ", wav.shape[1] setting["nsamples"]=wav.shape[1] print "# sampling rate : ", fs,"Hz" setting["framerate"]=fs print "# duration : ", wav_data["duration"],"sec" setting["duration"]=wav_data["duration"] # reading data df=fs*1.0/fftLen # cutoff bin min_freq_bin=int(np.ceil(min_freq/df)) max_freq_bin=int(np.floor(max_freq/df)) print "# min freq. :",min_freq_bin*df , "Hz" setting["min_freq"]=min_freq_bin*df print "# max freq. :",max_freq_bin*df , "Hz" setting["max_freq"]=max_freq_bin*df print "# freq. step:",df, "Hz" setting["freq_step"]=df print "# min freq. bin index:",min_freq_bin print "# max freq. bin index:",max_freq_bin # apply STFT win = hamming(fftLen) # ハミング窓 spec=simmch.stft_mch(wav,win,stft_step) spec_m=spec[:,:,min_freq_bin:max_freq_bin] # apply MUSIC method ## power[frame, freq, direction_id] print "# src_num:",src_num setting["src_num"]=src_num setting["step_ms"]=1000.0/fs*stft_step setting["music_step_ms"]=1000.0/fs*stft_step*music_step power=compute_music_spec(spec_m,src_num,tf_config,df,min_freq_bin, win_size=music_win_size, step=music_step) p=np.sum(np.real(power),axis=1) m_power=10*np.log10(p+1.0) m_full_power=10*np.log10(np.real(power)+1.0) return spec,m_power,m_full_power,setting
def query(self, x, fs): t0 = time.clock() labels = set() x = self.preprocess(x, fs) X = scipy.array([self.feature(x[i:i+self.fftsz]) for i in range(0, x.size-self.fftsz, self.hop)]) h = scipy.hamming(7)[:,None] X = scipy.signal.convolve(X, h, mode='valid') for i in range(len(X)-self.segsz): labels = labels.union( self.db.query(X[i:i+self.segsz].reshape(-1)) ) print 'Query Time: ', (time.clock() - t0) return self.results(labels)
def apply_tf(data, fftLen, step, tf_config, src_index, noise_amp=0): win = hamming(fftLen) # ハミング窓 mch_data = apply_tf_spec(data, fftLen, step, tf_config, src_index, noise_amp) out_wavdata = [] for mic_index in xrange(mch_data.shape[0]): spec = mch_data[mic_index] ### iSTFT resyn_data = simmch.istft(spec, win, step) out_wavdata.append(resyn_data) # concat waves mch_wavdata = np.vstack(out_wavdata) return mch_wavdata
def stft(x, fs, framesz, hop): """x is the time-domain signal fs is the sampling frequency framesz is the frame size, in seconds hop is the the time between the start of consecutive frames, in seconds """ framesamp = int(framesz*fs) hopsamp = int(hop*fs) w = scipy.hamming(framesamp) X = scipy.array([scipy.fft(w*x[i:i+framesamp]) for i in range(0, len(x)-framesamp, hopsamp)]) return X
def stft(x, fs, framesz, hop): """ x - signal fs - sample rate framesz - frame size hop - hop size (frame size = overlap + hop size) """ audio=np.reshape(x, (-1)) framesamp = int(framesz*fs) hopsamp = int(hop*fs) w = scipy.hamming(framesamp) X = scipy.array([scipy.fftpack.fft(w*audio[i:i+framesamp]) for i in range(0, len(x)-framesamp, hopsamp)]) return X, X[:,:X.shape[1]/2+1]
def istft(X, fs, framesz, hop): """ inverse stft """ framesamp = int(framesz*fs) hopsamp = int(hop*fs) newsamps = X.shape[1] xlen = hopsamp*(newsamps-1)+framesamp x = scipy.zeros(xlen) w = scipy.hamming(framesamp) # TODO .. look into double hamming weighting vs alternate compensation for overlap for n,i in enumerate(range(0, xlen-framesamp, hopsamp)): x[i:i+framesamp] += np.real(np.fft.ifft(X[:,n]))*w return x
def istft(X, fs, T, hop, j=0): """ T - signal length """ x = scipy.zeros(T * fs) framesamp = X.shape[1] hopsamp = int(hop * fs) for n, i in enumerate(range(0, len(x) - framesamp, hopsamp)): x[i : i + framesamp] += scipy.real(scipy.ifft(X[n])) # calculate xx, the sum of the windows, to scale results at the ends. xx = scipy.zeros(T * fs) w = scipy.hamming(framesamp) for i in range(0, len(x) - framesamp, hopsamp): xx[i : i + framesamp] += w xx = np.maximum(xx, 0.01) return x / xx
def stft(x, fs, framesz, hop): """x is the time-domain signal fs is the sampling frequency framesz is the frame size, in seconds hop is the the time between the start of consecutive frames, in seconds """ framesamp = int(framesz * fs) hopsamp = int(hop * fs) w = scipy.hamming(framesamp) X = scipy.array([ scipy.fft(w * x[i:i + framesamp]) for i in range(0, len(x) - framesamp, hopsamp) ]) return X
def tdft(audio, srate, windowsize, windowshift, fftsize): """Calculate the real valued fast Fourier transform of a segment of audio multiplied by a a Hamming window. Then, convert to decibels by multiplying by 20*log10. Repeat for all segments of the audio.""" windowsamp = int(windowsize * srate) shift = int(windowshift * srate) window = scipy.hamming(windowsamp) spectrogram = scipy.array([ 20 * scipy.log10( abs(np.fft.rfft(window * audio[i:i + windowsamp], fftsize))) for i in range(0, len(audio) - windowsamp, shift) ]) return spectrogram
def stft(self, x, fs, framesz, hop): # framesamp = int(framesz * fs) # hopsamp = int(hop * fs) framesamp = framesz hopsamp = hop w = scipy.hamming(framesamp) X = scipy.array([ scipy.fft(w * x[i:i + framesamp]) for i in range(0, len(x) - framesamp, hopsamp) ]) print X.shape X = X[:, :int(framesz / 2)] print "== fin stft ==" return X
def _make_window(self): """Make a Hamming window function. The window function has a length equal to quarter of the length of the input signal. :return: Hamming window function. :rtype: array-like """ h = np.floor(self.n_fbins / 4.0) h += 1 - np.remainder(h, 2) from scipy import hamming fwindow = hamming(int(h)) fwindow = fwindow / np.linalg.norm(fwindow) return fwindow
def mteo(data, kvalues=[1, 3, 5], condense=True): """multiresolution teager energy operator using given k-values [MTEO] The multi-resolution teager energy operator (MTEO) applies TEO operators of varying k-values and returns the reduced maximum response TEO for each input sample. To assure a constant noise power over all kteo channels, we convolve the individual kteo responses with a window: h_k(i) = hamming(4k+1) / sqrt(3sum(hamming(4k+1)^2) + sum(hamming(4k+1)) ^2), as suggested in Choi et al., 2006. :type data: ndarray :param data: The signal to operate on. ndim=1 :type kvalues: list :param kvalues: List of k-values to run the kteo for. If you want to give a single k-value, either use the kteo directly or put it in a list like [2]. :type condense: bool :param condense: if True, use max operator condensing onto one time series, else return a multichannel version with one channel per kvalue. Default=True :return: ndarray- Array of same shape as the input signal, holding the response of the kteo which response was maximum after smoothing for each sample in the input signal. """ # inits rval = sp.zeros((data.size, len(kvalues))) # evaluate the kteos for i, k in enumerate(kvalues): try: rval[:, i] = kteo(data, k) win = sp.hamming(4 * k + 1) win /= sp.sqrt(3 * (win ** 2).sum() + win.sum() ** 2) rval[:, i] = sp.convolve(rval[:, i], win, 'same') except: rval[:, i] = 0.0 log.warning('MTEO: could not calculate kteo for k=%s, ' 'data-length=%s', k, data.size) rval[:max(kvalues), i] = rval[-max(kvalues):, i] = 0.0 # return if condense is True: rval = rval.max(axis=1) return rval
def mteo(data, kvalues=[1, 3, 5], condense=True): """multiresolution teager energy operator using given k-values [MTEO] The multi-resolution teager energy operator (MTEO) applies TEO operators of varying k-values and returns the reduced maximum response TEO for each input sample. To assure a constant noise power over all kteo channels, we convolve the individual kteo responses with a window: h_k(i) = hamming(4k+1) / sqrt(3sum(hamming(4k+1)^2) + sum(hamming(4k+1)) ^2), as suggested in Choi et al., 2006. :type data: ndarray :param data: The signal to operate on. ndim=1 :type kvalues: list :param kvalues: List of k-values to run the kteo for. If you want to give a single k-value, either use the kteo directly or put it in a list like [2]. :type condense: bool :param condense: if True, use max operator condensing onto one time series, else return a multichannel version with one channel per kvalue. Default=True :return: ndarray- Array of same shape as the input signal, holding the response of the kteo which response was maximum after smoothing for each sample in the input signal. """ # inits rval = sp.zeros((data.size, len(kvalues))) # evaluate the kteos for i, k in enumerate(kvalues): try: rval[:, i] = kteo(data, k) win = sp.hamming(4 * k + 1) win /= sp.sqrt(3 * (win**2).sum() + win.sum()**2) rval[:, i] = sp.convolve(rval[:, i], win, 'same') except: rval[:, i] = 0.0 log.warning( 'MTEO: could not calculate kteo for k=%s, ' 'data-length=%s', k, data.size) rval[:max(kvalues), i] = rval[-max(kvalues):, i] = 0.0 # return if condense is True: rval = rval.max(axis=1) return rval
def istft(X, fs, T, hop): """ T - signal length """ # why do I need it!!??!? length = T*fs x = scipy.zeros(T*fs) framesamp = X.shape[1] hopsamp = int(hop*fs) for n,i in enumerate(range(0, len(x)-framesamp, hopsamp)): x[i:i+framesamp] += scipy.real(scipy.ifft(X[n])) # calculate the inverse envelope to scale results at the ends. env = scipy.zeros(T*fs) w = scipy.hamming(framesamp) for i in range(0, len(x)-framesamp, hopsamp): env[i:i+framesamp] += w env[-(length%hopsamp):] += w[-(length%hopsamp):] env = np.maximum(env, .01) return x/env # right side is still a little messed up...
def generate_frequency_series(self, NFFT, step, window='hamming'): w =scipy.hamming(NFFT) if len(self.signal.shape)==2: signal = np.array([np.fft.rfft(w*self.signal[:,i:i+NFFT]/NFFT) for i in range(0, self.signal.shape[1]-NFFT, step)]) else: signal = np.array([np.fft.rfft(w*self.signal[i:i+NFFT]/NFFT) for i in range(0, self.signal.shape[1]-NFFT, step)]) timebase = np.array([np.average(self.timebase[i:i+NFFT]) for i in range(0, self.signal.shape[1]-NFFT, step)]) #This is borrowed from here: #http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.fft.rfftfreq.html d = (self.timebase[1] - self.timebase[0]) val = 1.0/(NFFT*d) N = NFFT//2 + 1 frequency_base = np.round((np.arange(0, N, dtype=int)) * val,4) return FrequencyseriesData(frequency_base = frequency_base, timebase=timebase, signal=signal,channels=self.channels,NFFT=NFFT, window='hamming',step=step)
def stft(x, amp_phase=0, fs=1, framesz=320., hop=160.): """ x - signal fs - sample rate framesz - frame size hop - hop size (frame size = overlap + hop size) """ framesamp = int(framesz*fs) hopsamp = int(hop*fs) w = scipy.hamming(framesamp) X = scipy.array([scipy.fft(w*x[i:i+framesamp]) for i in range(0, len(x)-framesamp, hopsamp)]) Xamp = np.abs(X) Xphase = np.angle(X) if amp_phase: return X, Xamp, Xphase else: return X
def stft(x, fs, framesz, hop, demean=False): if demean: x = x - np.mean(x) framesamp = int(framesz*fs) hopsamp = int(hop*fs) w = scipy.hamming(framesamp) X = np.zeros([int(fs/2),len(range(0, len(x)-framesamp, hopsamp) )]) row_ctr = 0 for i in range(0, len(x)-framesamp, hopsamp): weighted = w*x[i:i+framesamp] ps = np.abs(np.fft.fft(weighted))**2 freqs = np.fft.fftfreq(w.size, 1/Fs) idxs = freqs>=0 col = np.transpose(-10*np.log(ps[idxs])) X[:,row_ctr] = col row_ctr+=1 return X
def stft(x, fs, framesz, hop): """ rolling/hopping hamming window FFT x .. data - 1D numpy array fs .. data rate in samples per sec framesz .. frame size in seconds hop .. sampling resoltuion in seconds """ framesamp = int(framesz*fs) hopsamp = int(hop*fs) xlen = len(x) newsamps = (xlen-framesamp)/hopsamp + 1 #print framesamp, newsamps, xlen, hopsamp w = scipy.hamming(framesamp) X = np.empty((framesamp, newsamps), dtype=complex) freqs = np.fft.fftfreq(framesamp) * fs for n, i in enumerate(range(0, xlen-framesamp, hopsamp)): X[:,n] = np.fft.fft(w*x[i:i+framesamp]) return X, float(fs)*newsamps/xlen, freqs
def apply_tf(data,fftLen, step,tf_config,src_index,noise_amp=0): win = hamming(fftLen) # ハミング窓 mch_data=apply_tf_spec(data,fftLen, step,tf_config,src_index,noise_amp) out_wavdata=[] for mic_index in xrange(mch_data.shape[0]): spec=mch_data[mic_index] ### iSTFT resyn_data = simmch.istft(spec, win, step) out_wavdata.append(resyn_data) # concat waves mch_wavdata=np.vstack(out_wavdata) return mch_wavdata