Filter Design

Assuming you know what filter you wish to design, this section will help you do so. Filter design is an advanced topic, and as such this section is meant for those who require a few pointers on designing filters in Python with SciPy.

The scipy.signal module includes several functions to help design a filter. The function iirdesign() is used for designing an IIR filter. It is quite complete, and it's best to read the online help and follow it through. Other useful IIR design filters include butter(), chebyl(), cheby2(), and ellip(). FIR filter design functionality is provided with functions remez() and firwin(). I won't be covering those, but should you need to use them, the online help is quite informative. Finally, if you'd like to view the frequency response of a filter, use the functions freqz() and freqs().

The code in Listing 8-7 will design a low-pass Butterworth filter (an IIR filter) and plot its frequency response.

Listing 8-7. Frequency Response of a Filter

N = 256 # number of points for freqz Wc = 0.2 # 3dB point Order =3 # filter order

# design a Butterworth filter

# calculate the frequency response [w, h] = signal.freqz(b, a, N)

# plot the results figure()

plot(arange(N)/float(N), 20*logl0(abs(h)), lw=2) title('Frequency response') xlabel('Frequency (normalized)') ylabel('dB')

plot(arange(N)/float(N), 20*logl0(abs(h)), lw=2) title('Frequency response (3dB point)') xlabel('Frequency (normalized)') ylabel('dB')

I've made use of two functions: butter() and freqz(). The function butter() designs an IIR filter with specified parameters (order and cutoff frequency), and the function freqz() returns a frequency response. Note that the frequency response is a complex number, and so I've plot the amplitude in dB of the absolute value: 20*logl0(abs(h)), as shown in Figure 8-14.

Figure 8-14. Frequency response of a low-pass filter

To filter data given a specific filter, use the function scipy.lfilter(b, a, x). Let's turn to an example.

Was this article helpful?

0 0

Post a comment