def octave_filters(Nbands, BandsPerOctave): # Bandpass Filter Generation pbrip = .5 # Pass band ripple sbrip = 50 # Stop band rejection #Filter order order = 2 fi, f_low, f_high = octave_frequencies(Nbands, BandsPerOctave) fs = 44100 # sampling rate wi = fi/(fs/2.) # normalized frequencies w_low = f_low/(fs/2.) w_high = f_high/(fs/2.) B = [] A = [] # For each band for w, wl, wh in zip(wi, w_low, w_high): # normalized frequency vector freq = [wl, wh] # could be another IIR filter [b, a] = ellip(order, pbrip, sbrip, freq, btype='bandpass') B += [b] A += [a] return [B, A, fi, f_low, f_high]
def octave_filters_oneoctave(total_band_count, bands_per_octave): # Bandpass Filter Generation pbrip = .5 # Pass band ripple sbrip = 50 # Stop band rejection # Filter order order = 2 fi, f_low, f_high = octave_frequencies(total_band_count, bands_per_octave) fi = fi[-bands_per_octave:] f_low = f_low[-bands_per_octave:] f_high = f_high[-bands_per_octave:] fs = SAMPLING_RATE wi = fi / (fs / 2.) # normalized frequencies w_low = f_low / (fs / 2.) w_high = f_high / (fs / 2.) w_high = (w_high < 1.) * w_high + (w_high >= 1.) * 1. B = [] A = [] # For each band for w, wl, wh in zip(wi, w_low, w_high): # normalized frequency vector freq = [wl, wh] # could be another IIR filter [b, a] = ellip(order, pbrip, sbrip, freq, btype='bandpass') B += [b.tolist()] A += [a.tolist()] return [B, A, fi, f_low, f_high]
def octave_filters(total_band_count, bands_per_octave): # Bandpass Filter Generation pbrip = .5 # Pass band ripple sbrip = 50 # Stop band rejection # Filter order order = 2 fi, f_low, f_high = octave_frequencies(total_band_count, bands_per_octave) fs = SAMPLING_RATE wi = fi / (fs / 2.) # normalized frequencies w_low = f_low / (fs / 2.) w_high = f_high / (fs / 2.) w_high = (w_high < 1.) * w_high + (w_high >= 1.) * 1. B = [] A = [] # For each band for w, wl, wh in zip(wi, w_low, w_high): # normalized frequency vector freq = [wl, wh] # could be another IIR filter [b, a] = ellip(order, pbrip, sbrip, freq, btype='bandpass') B += [b] A += [a] return [B, A, fi, f_low, f_high]
def octave_filters(Nbands, BandsPerOctave): # Bandpass Filter Generation pbrip = .5 # Pass band ripple sbrip = 50 # Stop band rejection #Filter order order = 2 fi, f_low, f_high = octave_frequencies(Nbands, BandsPerOctave) fs = 44100 # sampling rate wi = fi / (fs / 2.) # normalized frequencies w_low = f_low / (fs / 2.) w_high = f_high / (fs / 2.) B = [] A = [] # For each band for w, wl, wh in zip(wi, w_low, w_high): # normalized frequency vector freq = [wl, wh] # could be another IIR filter [b, a] = ellip(order, pbrip, sbrip, freq, btype='bandpass') B += [b] A += [a] return [B, A, fi, f_low, f_high]
N = 2**13 fs = 44100. x = arange(0,N)/fs f = 63.4 Nw = 2**13 window = zeros(N) window[:Nw] = hamming(Nw) y = window*sin(2*pi*x*f) pbrip = .5 sbrip = 50 order = 2 fi = 63.41 flow = 62.5 fhigh = 64.33 wi = fi/(fs/2.) wlow = flow/(fs/2.) whigh = fhigh/(fs/2.) w = [wlow, whigh] [b, a] = ellip(order, pbrip, sbrip, w, btype='bandpass') yf, zf = lfilter(b, a, y, zi=zeros(max(len(a),len(b))-1)) yf2, zf = lfilter(b, a, 0.*y, zi=zf) yf3, zf = lfilter(b, a, 0.*y, zi=zf) print(x[-1]+x, yf2) clf() subplot(211); plot(x,y); plot(x, yf); plot(x[-1]+x, yf2); plot(2*x[-1]+x, yf3) subplot(212); loglog(fftshift(fftfreq(N,1./fs))[N/2:],fftshift(abs((fft(y)))**2)[N/2:]); loglog(fftshift(fftfreq(N,1./fs))[N/2:],fftshift(abs((fft(yf)))**2)[N/2:]); ylim(ymin=1e-6) show()
from numpy.fft import * import numpy as np #def fftsmooth(y, cutfreq=0.004): def fftsmooth(y, cutfreq=0.01): freq = fftfreq(len(y)) trans = rfft(y) #print 'freq:', freq[:trans.size-1], freq[0], freq[trans.size-2],trans.size-1 #print 'freq cut:',cutfreq/freq[trans.size-2]*(trans.size-1) ncut = int(cutfreq/freq[trans.size-2]*(trans.size-1)) newtrans = np.array([0+0j for i,t in enumerate(trans) if i <= ncut] + [t for i,t in enumerate(trans) if i>ncut]) smoothedy = irfft(newtrans) return smoothedy from scipy.signal import lfilter#, medfilt from scipy.signal import filter_design #b,a = filter_design.butter(1, 0.03, btype='high', ouput='ba') b,a = filter_design.ellip(1, 1., 1., 0.03, btype='high', output='ba') def filtersmooth(y): #return medfilt(y, kernel_size=251) return lfilter(b,a,y)
from numpy.fft import * import numpy as np #def fftsmooth(y, cutfreq=0.004): def fftsmooth(y, cutfreq=0.01): freq = fftfreq(len(y)) trans = rfft(y) #print 'freq:', freq[:trans.size-1], freq[0], freq[trans.size-2],trans.size-1 #print 'freq cut:',cutfreq/freq[trans.size-2]*(trans.size-1) ncut = int(cutfreq / freq[trans.size - 2] * (trans.size - 1)) newtrans = np.array([0 + 0j for i, t in enumerate(trans) if i <= ncut] + [t for i, t in enumerate(trans) if i > ncut]) smoothedy = irfft(newtrans) return smoothedy from scipy.signal import lfilter #, medfilt from scipy.signal import filter_design #b,a = filter_design.butter(1, 0.03, btype='high', ouput='ba') b, a = filter_design.ellip(1, 1., 1., 0.03, btype='high', output='ba') def filtersmooth(y): #return medfilt(y, kernel_size=251) return lfilter(b, a, y)
Nw = 2**13 window = zeros(N) window[:Nw] = hamming(Nw) y = window * sin(2 * pi * x * f) pbrip = .5 sbrip = 50 order = 2 fi = 63.41 flow = 62.5 fhigh = 64.33 wi = fi / (fs / 2.) wlow = flow / (fs / 2.) whigh = fhigh / (fs / 2.) w = [wlow, whigh] [b, a] = ellip(order, pbrip, sbrip, w, btype='bandpass') #cheb1ord(wp, ws, 1., sbrip) #cheby1(N, rp, Wn, btype='low', analog=0, output='ba') yf = lfilter(b, a, y) yf2 = lfilter(b, a, yf) clf() subplot(211) plot(x, y) plot(x, yf) plot(x, yf2) subplot(212) loglog( fftshift(fftfreq(N, 1. / fs))[N / 2:], fftshift(abs((fft(y)))**2)[N / 2:])