def filter(self, floatdata): y, dec, zfs = octave_filter_bank_decimation(self.bdec, self.adec, self.boct, self.aoct, floatdata, zis=self.zfs) self.zfs = zfs return y, dec
def filter(self, floatdata): #y, dec, zfs = octave_filter_bank_decimation(self.bdec, self.adec, self.boct, self.aoct, floatdata) y, dec, zfs = octave_filter_bank_decimation(self.bdec, self.adec, self.boct, self.aoct, floatdata, zis=self.zfs) #y, zfs = octave_filter_bank(self.b_nodec, self.a_nodec, floatdata); dec = [1.]*len(y) #y, zfs = octave_filter_bank(self.b_nodec, self.a_nodec, floatdata, zis=self.zfs); dec = [1.]*len(y) self.zfs = zfs return y, dec
def main(): from matplotlib.pyplot import semilogx, plot, show, xlim, ylim, figure, legend, subplot, bar from numpy.fft import fft, fftfreq, fftshift, ifft from numpy import log10, linspace, interp, angle, array, concatenate N = 2048 * 2 * 2 fs = float(SAMPLING_RATE) channel_count = 20 low_freq = 20. impulse = zeros(N) impulse[N / 2] = 1 f = 1000. # impulse = sin(2*pi*f*arange(0, N/fs, 1./fs)) # [ERBforward, ERBfeedback] = MakeERBFilters(fs, channel_count, low_freq) # y = ERBFilterBank(ERBforward, ERBfeedback, impulse) bands_per_octave = 3 total_band_count = NOCTAVE * bands_per_octave [B, A, fi, fl, fh] = octave_filters(total_band_count, bands_per_octave) y, zfs = octave_filter_bank(B, A, impulse) # print "Filter lengths without decimation" # for b, a in zip(B, A): # print len(b), len(a) response = 20. * log10(abs(fft(y))) freqScale = fftfreq(N, 1. / fs) figure() subplot(211) for i in range(0, response.shape[0]): semilogx(freqScale[0:int(N / 2)], response[i, 0:int(N / 2)]) xlim(fs / 2000, fs) ylim(-70, 10) subplot(212) m = 0 for f in fi: p = 10. * log10((y[m]**2).mean()) m += 1 semilogx(f, p, 'ko') Ndec = 3 fc = 0.5 # other possibilities # (bdec, adec) = ellip(Ndec, 0.05, 30, fc) # print bdec # (bdec, adec) = cheby1(Ndec, 0.05, fc) # (bdec, adec) = butter(Ndec, fc) (bdec, adec) = iirdesign(0.48, 0.50, 0.05, 70, analog=0, ftype='ellip', output='ba') # bdec = firwin(30, fc) # adec = [1.] figure() subplot(211) response = 20. * log10(abs(fft(impulse))) plot(fftshift(freqScale), fftshift(response), label="impulse") y = lfilter(bdec, adec, impulse) response = 20. * log10(abs(fft(y))) plot(fftshift(freqScale), fftshift(response), label="lowpass") ydec = y[::2].repeat(2) response = 20. * log10(abs(fft(ydec))) plot(fftshift(freqScale), fftshift(response), label="lowpass + dec2 + repeat2") ydec2 = interp(list(range(0, len(y))), list(range(0, len(y), 2)), y[::2]) response = 20. * log10(abs(fft(ydec2))) plot(fftshift(freqScale), fftshift(response), label="lowpass + dec2 + interp2") ydec3 = y[::2] response = 20. * log10(abs(fft(ydec3))) freqScale2 = fftfreq(int(N / 2), 2. / fs) plot(freqScale2, fftshift(response), label="lowpass + dec2") legend(loc="lower left") subplot(212) plot(list(range(0, len(impulse))), impulse, label="impulse") plot(list(range(0, len(impulse))), y, label="lowpass") plot(list(range(0, len(impulse))), ydec, label="lowpass + dec2 + repeat2") plot(list(range(0, len(impulse))), ydec2, label="lowpass + dec2 + interp2") plot(list(range(0, len(impulse), 2)), ydec3, label="lowpass + dec2") legend() [boct, aoct, fi, flow, fhigh] = octave_filters_oneoctave(total_band_count, bands_per_octave) y, dec, zfs = octave_filter_bank_decimation(bdec, adec, boct, aoct, impulse) # print "Filter lengths with decimation" # print len(bdec), len(adec) # for b, a in zip(boct, aoct): # print len(b), len(a) figure() subplot(211) for yone, d in zip(y, dec): response = 20. * log10(abs(fft(yone)) * d) freqScale = fftfreq(int(N / d), 1. / (fs / d)) semilogx(freqScale[0:int(N / (2 * d))], response[0:int(N / (2 * d))]) xlim(fs / 2000, fs) ylim(-70, 10) subplot(212) m = 0 for i in range(0, NOCTAVE): for f in fi: p = 10. * log10((y[m]**2).mean()) semilogx(f / dec[m], p, 'ko') m += 1 [boct, aoct, fi, flow, fhigh] = octave_filters_oneoctave(total_band_count, bands_per_octave) y1, dec, zfs = octave_filter_bank_decimation(bdec, adec, boct, aoct, impulse[0:int(N / 2)]) y2, dec, zfs = octave_filter_bank_decimation(bdec, adec, boct, aoct, impulse[int(N / 2):], zis=zfs) y = [] for y1one, y2one in zip(y1, y2): y += [concatenate((y1one, y2one))] figure() subplot(211) for yone, d in zip(y, dec): response = 20. * log10(abs(fft(yone)) * d) freqScale = fftfreq(int(N / d), 1. / (fs / d)) semilogx(freqScale[0:int(N / (2 * d))], response[0:int(N / (2 * d))]) xlim(fs / 2000, fs) ylim(-70, 10) subplot(212) m = 0 for i in range(0, NOCTAVE): for f in fi: p = 10. * log10((y[m]**2).mean()) semilogx(f / dec[m], p, 'ko') m += 1 generate_filters_params() show()
def main(): from matplotlib.pyplot import semilogx, plot, show, xlim, ylim, figure, legend, subplot, bar from numpy.fft import fft, fftfreq, fftshift, ifft from numpy import log10, linspace, interp, angle, array, concatenate N = 2048 * 2 * 2 fs = float(SAMPLING_RATE) channel_count = 20 low_freq = 20. impulse = zeros(N) impulse[N / 2] = 1 f = 1000. # impulse = sin(2*pi*f*arange(0, N/fs, 1./fs)) # [ERBforward, ERBfeedback] = MakeERBFilters(fs, channel_count, low_freq) # y = ERBFilterBank(ERBforward, ERBfeedback, impulse) bands_per_octave = 3 total_band_count = NOCTAVE * bands_per_octave [B, A, fi, fl, fh] = octave_filters(total_band_count, bands_per_octave) y, zfs = octave_filter_bank(B, A, impulse) # print "Filter lengths without decimation" # for b, a in zip(B, A): # print len(b), len(a) response = 20. * log10(abs(fft(y))) freqScale = fftfreq(N, 1. / fs) figure() subplot(211) for i in range(0, response.shape[0]): semilogx(freqScale[0:int(N / 2)], response[i, 0:int(N / 2)]) xlim(fs / 2000, fs) ylim(-70, 10) subplot(212) m = 0 for f in fi: p = 10. * log10((y[m] ** 2).mean()) m += 1 semilogx(f, p, 'ko') Ndec = 3 fc = 0.5 # other possibilities # (bdec, adec) = ellip(Ndec, 0.05, 30, fc) # print bdec # (bdec, adec) = cheby1(Ndec, 0.05, fc) # (bdec, adec) = butter(Ndec, fc) (bdec, adec) = iirdesign(0.48, 0.50, 0.05, 70, analog=0, ftype='ellip', output='ba') # bdec = firwin(30, fc) # adec = [1.] figure() subplot(211) response = 20. * log10(abs(fft(impulse))) plot(fftshift(freqScale), fftshift(response), label="impulse") y = lfilter(bdec, adec, impulse) response = 20. * log10(abs(fft(y))) plot(fftshift(freqScale), fftshift(response), label="lowpass") ydec = y[::2].repeat(2) response = 20. * log10(abs(fft(ydec))) plot(fftshift(freqScale), fftshift(response), label="lowpass + dec2 + repeat2") ydec2 = interp(list(range(0, len(y))), list(range(0, len(y), 2)), y[::2]) response = 20. * log10(abs(fft(ydec2))) plot(fftshift(freqScale), fftshift(response), label="lowpass + dec2 + interp2") ydec3 = y[::2] response = 20. * log10(abs(fft(ydec3))) freqScale2 = fftfreq(int(N / 2), 2. / fs) plot(freqScale2, fftshift(response), label="lowpass + dec2") legend(loc="lower left") subplot(212) plot(list(range(0, len(impulse))), impulse, label="impulse") plot(list(range(0, len(impulse))), y, label="lowpass") plot(list(range(0, len(impulse))), ydec, label="lowpass + dec2 + repeat2") plot(list(range(0, len(impulse))), ydec2, label="lowpass + dec2 + interp2") plot(list(range(0, len(impulse), 2)), ydec3, label="lowpass + dec2") legend() [boct, aoct, fi, flow, fhigh] = octave_filters_oneoctave(total_band_count, bands_per_octave) y, dec, zfs = octave_filter_bank_decimation(bdec, adec, boct, aoct, impulse) # print "Filter lengths with decimation" # print len(bdec), len(adec) # for b, a in zip(boct, aoct): # print len(b), len(a) figure() subplot(211) for yone, d in zip(y, dec): response = 20. * log10(abs(fft(yone)) * d) freqScale = fftfreq(int(N / d), 1. / (fs / d)) semilogx(freqScale[0:int(N / (2 * d))], response[0:int(N / (2 * d))]) xlim(fs / 2000, fs) ylim(-70, 10) subplot(212) m = 0 for i in range(0, NOCTAVE): for f in fi: p = 10. * log10((y[m] ** 2).mean()) semilogx(f / dec[m], p, 'ko') m += 1 [boct, aoct, fi, flow, fhigh] = octave_filters_oneoctave(total_band_count, bands_per_octave) y1, dec, zfs = octave_filter_bank_decimation(bdec, adec, boct, aoct, impulse[0:int(N / 2)]) y2, dec, zfs = octave_filter_bank_decimation(bdec, adec, boct, aoct, impulse[int(N / 2):], zis=zfs) y = [] for y1one, y2one in zip(y1, y2): y += [concatenate((y1one, y2one))] figure() subplot(211) for yone, d in zip(y, dec): response = 20. * log10(abs(fft(yone)) * d) freqScale = fftfreq(int(N / d), 1. / (fs / d)) semilogx(freqScale[0:int(N / (2 * d))], response[0:int(N / (2 * d))]) xlim(fs / 2000, fs) ylim(-70, 10) subplot(212) m = 0 for i in range(0, NOCTAVE): for f in fi: p = 10. * log10((y[m] ** 2).mean()) semilogx(f / dec[m], p, 'ko') m += 1 generate_filters_params() show()