def filter_trig_poly(u, h, dt, M): """ Filter a trigonometric polynomial with a filter's impulse response. Parameters ---------- u : numpy.ndarray Input signal. h : numpy.ndarray Impulse response of filter. dt : float Time resolution. M : int Trigonometric polynomial order. Returns ------- v : numpy.ndarray Filtered signal. Notes ----- Assumes that `u` is defined over times `dt*arange(0, len(t))` and that `dt*len(u)` is equal to the period of the trigonometric polynomial. """ # Get the Dirichlet coefficients of the signal: am = tp.get_dirichlet_coeffs(u, dt, M) # Get the Dirichlet coefficients of the filter: hm = tp.get_dirichlet_coeffs(h, dt, M) # Construct the filtered signal using the above coefficients: T = dt*len(u) Omega = 2*np.pi*M/T t = np.arange(0, len(u))*dt v = np.zeros(len(u), np.complex) for m in xrange(-M, M+1): v += hm[m+M]*am[m+M]*em(m, t, Omega, M)*np.sqrt(T)/dt return np.real(v)
def filter_trig_poly(u, h, dt, M): """ Filter a trigonometric polynomial with a filter's impulse response. Parameters ---------- u : numpy.ndarray Input signal. h : numpy.ndarray Impulse response of filter. dt : float Time resolution. M : int Trigonometric polynomial order. Returns ------- v : numpy.ndarray Filtered signal. Notes ----- Assumes that `u` is defined over times `dt*arange(0, len(t))` and that `dt*len(u)` is equal to the period of the trigonometric polynomial. """ # Get the Dirichlet coefficients of the signal: am = tp.get_dirichlet_coeffs(u, dt, M) # Get the Dirichlet coefficients of the filter: hm = tp.get_dirichlet_coeffs(h, dt, M) # Construct the filtered signal using the above coefficients: T = dt * len(u) Omega = 2 * np.pi * M / T t = np.arange(0, len(u)) * dt v = np.zeros(len(u), np.complex) for m in xrange(-M, M + 1): v += hm[m + M] * am[m + M] * em(m, t, Omega, M) * np.sqrt(T) / dt return np.real(v)
N = len(u) return np.real(np.fft.ifft(np.fft.fft(np.hstack((u, np.zeros(len(h)-1))))*\ np.fft.fft(np.hstack((h, np.zeros(len(u)-1))))))[0:N] if __name__ == '__main__': print 'generating trigonometric polynomial signal..' M = 250 Omega = 2*np.pi*2000 T = 2*np.pi*M/Omega dt = 1e-5 t = np.arange(0, T, dt) u = tp.gen_trig_poly(T, dt, M) am_rec = tp.get_dirichlet_coeffs(u, dt, M) # Try to recover the Dirichlet coefficients of the generated signal # using different methods. Note that this only works if u contains an # entire period of the signal (i.e., arange(0, T, dt)): print 'reconstructing signal from recovered coefficients..' u_rec = tp.gen_trig_poly(T, dt, am_rec) pl.plot_compare(t, u, u_rec, 'Signal Reconstruction', output_name + str(output_count) + output_ext) output_count += 1 # Create a filter: h = make_gammatone(t, 16, 0) hm = tp.get_dirichlet_coeffs(h, dt, M) h_rec = tp.gen_trig_poly(T, dt, hm) pl.plot_compare(t, h, h_rec, 'Filter Reconstruction',
return np.real(np.fft.ifft(np.fft.fft(np.hstack((u, np.zeros(len(h)-1))))*\ np.fft.fft(np.hstack((h, np.zeros(len(u)-1))))))[0:N] if __name__ == '__main__': print 'generating trigonometric polynomial signal..' M = 250 Omega = 2 * np.pi * 2000 T = 2 * np.pi * M / Omega dt = 1e-5 t = np.arange(0, T, dt) u = tp.gen_trig_poly(T, dt, M) am_rec = tp.get_dirichlet_coeffs(u, dt, M) # Try to recover the Dirichlet coefficients of the generated signal # using different methods. Note that this only works if u contains an # entire period of the signal (i.e., arange(0, T, dt)): print 'reconstructing signal from recovered coefficients..' u_rec = tp.gen_trig_poly(T, dt, am_rec) pl.plot_compare(t, u, u_rec, 'Signal Reconstruction', output_name + str(output_count) + output_ext) output_count += 1 # Create a filter: h = make_gammatone(t, 16, 0) hm = tp.get_dirichlet_coeffs(h, dt, M) h_rec = tp.gen_trig_poly(T, dt, hm) pl.plot_compare(t, h, h_rec, 'Filter Reconstruction',