Source code for proc
import scipy.signal as sig
import pylab as pl
import wf
#------------------------------------------------------------
### FIR filter class ###
#------------------------------------------------------------
[docs]class FIRFilter :
"""Finite Response Filter based on the Kaiser window"""
"""fc - cutoff, bwt - transition region width, ripple in dB"""
#------------------------------------------------------------
def __init__(self,ns,fs,fc,bwt,ripple) :
self.ns = ns
self.fs = fs
self.fc = fc
self.bwt = bwt
self.ripple = ripple
self.fn = self.fs/2. # Nyquist frequency
# Normalise the desired width of the transition band
self.bwtn = self.bwt/self.fn
# Compute the order and Kaiser parameter for the FIR filter.
self.N, self.beta = sig.kaiserord(self.ripple,self.bwtn)
print "Filter order:", self.N
print "Filter beta:", self.beta
# Use firwin with a Kaiser window to create a lowpass FIR filter.
self.coeff = sig.firwin(self.N, self.fc/self.fn, window=('kaiser', self.beta))
#------------------------------------------------------------
[docs] def filter(self,wfin) :
"""Filter the waveform"""
wfout = wf.WfDouble(wfin.getNs(),wfin.getFs(),wfin.getT0())
# Apply the FIR filter
wfout.setValues(sig.lfilter(self.coeff, 1.0, wfin.getWf()))
return wfout
#------------------------------------------------------------
[docs] def plotCoeff(self) :
"""Plot the filter coefficients"""
pl.plot(self.coeff, 'bo-')
pl.title('Filter Coefficients')
pl.grid(True)
#------------------------------------------------------------
[docs] def plotResponse(self) :
"""Plot the filter response"""
w, h = sig.freqz(self.coeff, worN=8000)
pl.plot( (w/pl.pi)*self.fn, pl.absolute(h) )
pl.xlabel('Frequency')
pl.ylabel('Gain')
pl.title('Frequency Response')
pl.grid(True)
#------------------------------------------------------------
### ###
#------------------------------------------------------------
#------------------------------------------------------------