def _istftm(self, X_hat=None, Phi_hat=None, pvoc=False, usewin=True, resamp=None): """ :: Inverse short-time Fourier transform magnitude. Make a signal from a |STFT| transform. Uses phases from self.STFT if Phi_hat is None. Inputs: X_hat - N/2+1 magnitude STFT [None=abs(self.STFT)] Phi_hat - N/2+1 phase STFT [None=exp(1j*angle(self.STFT))] pvoc - whether to use phase vocoder [False] usewin - whether to use overlap-add [False] Returns: x_hat - estimated signal """ if not self._have_stft: return None X_hat = P.np.abs(self.STFT) if X_hat is None else P.np.abs(X_hat) if pvoc: self._pvoc(X_hat, Phi_hat, pvoc) else: Phi_hat = P.angle(self.STFT) if Phi_hat is None else Phi_hat self.X_hat = X_hat * P.exp( 1j * Phi_hat ) if usewin: self.win = P.hanning(self.nfft) self.win *= 1.0 / ((float(self.nfft)*(self.win**2).sum())/self.nhop) else: self.win = P.ones(self.nfft) if resamp: self.win = sig.resample(self.win, int(P.np.round(self.nfft * resamp))) fp = self._check_feature_params() self.x_hat = self._overlap_add(P.real(self.nfft * P.irfft(self.X_hat.T)), usewin=usewin, resamp=resamp) if self.verbosity: print "Extracted iSTFTM->self.x_hat" return self.x_hat
def spec_env(frame, coefs): mags = abs(frame) N = len(mags) ceps = pl.rfft(pl.log(mags[:N - 1])) ceps[coefs:] = 0 mags[:N - 1] = pl.exp(pl.irfft(ceps)) return mags
def _istftm(self, X_hat=None, Phi_hat=None, pvoc=False, usewin=True, resamp=None): """ :: Inverse short-time Fourier transform magnitude. Make a signal from a |STFT| transform. Uses phases from self.STFT if Phi_hat is None. Inputs: X_hat - N/2+1 magnitude STFT [None=abs(self.STFT)] Phi_hat - N/2+1 phase STFT [None=exp(1j*angle(self.STFT))] pvoc - whether to use phase vocoder [False] usewin - whether to use overlap-add [False] Returns: x_hat - estimated signal """ if not self._have_stft: return None X_hat = self.X if X_hat is None else P.np.abs(X_hat) if pvoc: self._pvoc(X_hat, Phi_hat, pvoc) else: Phi_hat = P.angle(self.STFT) if Phi_hat is None else Phi_hat self.X_hat = X_hat * P.exp(1j * Phi_hat) if usewin: if self.win is None: self.win = P.ones( self.wfft) if self.window == 'rect' else P.np.sqrt( P.hanning(self.wfft)) if len(self.win) != self.nfft: self.win = P.r_[self.win, P.np.zeros(self.nfft - self.wfft)] if len(self.win) != self.nfft: error.BregmanError( "features_base.Features._istftm(): assertion failed len(self.win)==self.nfft" ) else: self.win = P.ones(self.nfft) if resamp: self.win = sig.resample(self.win, int(P.np.round(self.nfft * resamp))) fp = self._check_feature_params() self.x_hat = self._overlap_add(P.real(P.irfft(self.X_hat.T)), usewin=usewin, resamp=resamp) if self.verbosity: print("Extracted iSTFTM->self.x_hat") return self.x_hat
def _icqft(self, V_hat): """ :: Inverse constant-Q Fourier transform. Make a signal from a constant-Q transform. """ if not self._have_cqft: return False fp = self._check_feature_params() X_hat = pylab.array( pylab.dot(self.Q.T, V_hat) ) * pylab.exp( 1j * pylab.angle(self.STFT) ) self.x_hat = self._overlap_add( pylab.real(fp['nfft'] * pylab.irfft(X_hat.T)) ) if fp['verbosity']: print "Extracted iCQFT->x_hat" return True
def true_spec_env(frame, coefs, thresh): N = len(frame) form = pl.zeros(N) lmags = pl.log(abs(frame[:N - 1])) mags = lmags go = True while (go): ceps = pl.rfft(lmags) ceps[coefs:] = 0 form[:N - 1] = pl.irfft(ceps) go = False for i in range(0, N - 1): if lmags[i] < form[i]: lmags[i] = form[i] diff = mags[i] - form[i] if diff > thresh: go = True return pl.exp(form)
def true_spec_env(amps,coefs,thresh): N = len(amps) sm = 10e-15 form = pl.zeros(N) lmags = pl.log(amps[:N-1]+sm) mags = lmags check = True while(check): ceps = pl.rfft(lmags) ceps[coefs:] = 0 form[:N-1] = pl.irfft(ceps) for i in range(0,N-1): if lmags[i] < form[i]: lmags[i] = form[i] diff = mags[i] - form[i] if diff > thresh: check = True else: check = False return pl.exp(form)+sm
def _istftm(self, X_hat, Phi_hat=None): """ :: Inverse short-time Fourier transform magnitude. Make a signal from a |STFT| transform. Uses phases from self.STFT if Phi_hat is None. """ if not self._have_stft: return False if Phi_hat is None: Phi_hat = pylab.exp( 1j * pylab.angle(self.STFT)) fp = self._check_feature_params() X_hat = X_hat * Phi_hat self.x_hat = self._overlap_add( pylab.real(fp['nfft'] * pylab.irfft(X_hat.T)) ) if fp['verbosity']: print "Extracted iSTFTM->self.x_hat" return True
def _istftm(self, X_hat=None, Phi_hat=None, pvoc=False, usewin=True, resamp=None): """ :: Inverse short-time Fourier transform magnitude. Make a signal from a |STFT| transform. Uses phases from self.STFT if Phi_hat is None. Inputs: X_hat - N/2+1 magnitude STFT [None=abs(self.STFT)] Phi_hat - N/2+1 phase STFT [None=exp(1j*angle(self.STFT))] pvoc - whether to use phase vocoder [False] usewin - whether to use overlap-add [False] Returns: x_hat - estimated signal """ if not self._have_stft: return None X_hat = self.X if X_hat is None else P.np.abs(X_hat) if pvoc: self._pvoc(X_hat, Phi_hat, pvoc) else: Phi_hat = P.angle(self.STFT) if Phi_hat is None else Phi_hat self.X_hat = X_hat * P.exp(1j * Phi_hat) if usewin: if self.win is None: self.win = P.ones(self.wfft) if self.window == 'rect' else P.np.sqrt( P.hanning(self.wfft)) if len(self.win) != self.nfft: self.win = P.r_[self.win, P.np.zeros(self.nfft - self.wfft)] if len(self.win) != self.nfft: error.BregmanError( "features_base.Features._istftm(): assertion failed len(self.win)==self.nfft") else: self.win = P.ones(self.nfft) if resamp: self.win = sig.resample( self.win, int(P.np.round(self.nfft * resamp))) fp = self._check_feature_params() self.x_hat = self._overlap_add( P.real(P.irfft(self.X_hat.T)), usewin=usewin, resamp=resamp) if self.verbosity: print("Extracted iSTFTM->self.x_hat") return self.x_hat
def pconv(input, ir, S=1024): N = len(ir) L = len(input) M = S * 2 P = int(pl.ceil(N / S)) H = pl.zeros((P, M // 2 + 1)) + 0j X = pl.zeros((P, M // 2 + 1)) + 0j x = pl.zeros(M) o = pl.zeros(M) s = pl.zeros(S) for i in range(0, P): p = (P - 1 - i) * S ll = len(ir[p:p + S]) x[:ll] = ir[p:p + S] H[i] = pl.rfft(x) y = pl.zeros(L + N - 1) n = S i = 0 for p in range(0, L + N - S, S): if p > L: x = pl.zeros(M) else: if n + p > L: n = L - p x[n:] = pl.zeros(M - n) x[:n] = input[p:p + n] X[i] = pl.rfft(x) i = (i + 1) % P O = pl.zeros(M // 2 + 1) + 0j for j in range(0, P): k = (j + i) % P O += H[j] * X[k] o = pl.irfft(O) y[p:p + S] = o[:S] + s s = o[S:] return y
def conv(input, ir): N = len(ir) L = len(input) M = 2 while (M <= 2 * N - 1): M *= 2 h = pl.zeros(M) x = pl.zeros(M) y = pl.zeros(L + N - 1) h[0:N] = ir H = pl.rfft(h) n = N for p in range(0, L, N): if p + n > L: n = L - p x[n:] = pl.zeros(M - n) x[0:n] = input[p:p + n] y[p:p + 2 * n - 1] += pl.irfft(H * pl.rfft(x))[0:2 * n - 1] return y
def istft(X, w, n): N = len(w) k = pl.arange(0, N / 2 + 1) xn = pl.irfft(X * pl.exp(-2 * pl.pi * 1j * k * n / N)) return xn * w
# Return the Discrete Fourier Transform sample frequencies logging.debug('compute the Fourier Transform sample frequencies') freq = fftfreq(len(data), d=1.0/samprate)[range(0,npts/2+1)] # the last element is negative, because of the symmetry, but should # be positive fft_values[-1] *= -1 convolved = [] for f in freq: w = f * 2 * pi convolved.append( response._eval_response(respaddr, w ) ) data = irfft( convolved * fft_values ) logging.debug( 'Total samples: %s ' % len(data) ) rec.trputdata( data ) response._free_response ( respaddr ) return tr def decimate_trace( tr, newsps ): """ Function to correctly apply some decimation filters """ logging.debug('Now try to decimate the data')
pl.figure(figsize=(8, 5)) pl.subplot(211) pl.title('waveform') end = N t = pl.arange(0, end) / float(sr) pl.xlim(0, t[end - 1]) pl.ylim(-1.1, 1.1) pl.xlabel("time (s)") pl.plot(t, sig[0:end], 'k') pl.subplot(212) pl.title('spectral envelope') x = pl.arange(0, N / 2) bins = x * sr / N spec = pl.fft(sig / max(sig)) mags = abs(spec / N) logm = pl.log(mags) ceps = pl.rfft(logm) ceps2 = pl.zeros(len(ceps)) ceps2[0:50] = ceps[0:50] specenv = pl.exp(pl.irfft(ceps2)) spec1 = 20 * pl.log10(mags / max(mags)) pl.ylim(-40, 1) pl.ylabel("amp (dB)") pl.xlabel("freq (Hz)") pl.xlim(0, 2000) pl.tight_layout() pl.plot(bins[0:N / 2], spec1[0:N / 2], 'k-', linewidth=2) pl.show()
def istft(X, w): xn = pl.irfft(X) return xn * w
import pylab as pl pi = pl.pi T = 1000 t = pl.arange(0, T) x = 0.51 - 0.49 * pl.cos(2 * pi * t / T) y = pl.sin(2 * pi * 10 * t / T) X = pl.rfft(y * x) pl.plot(pl.irfft(X)) pl.show()