S7 · Lesson 48 · Filter design lab
Designing a Kaiser-window FIR filter and a bilinear Butterworth IIR filter from their specifications, and estimating power spectra by Welch's method, in Python
Filter design programs hide a few dozen lines of arithmetic. In this lab you'll write those lines: a Kaiser-window FIR designer that takes band edges and a ripple and returns the coefficients, a Butterworth IIR designer that prewarps, picks the order, places the poles and maps them through the bilinear transform, and a Welch spectrum estimator scaled correctly in units per hertz. The tests check each design the way an engineer would, by evaluating it on a dense frequency grid against the specification, using the examples from lessons 19, 17 and 25.
Predict
The Kaiser formulas give M = 37 for = 0.4π, = 0.6π and δ = 0.001. Will the finished filter meet δ = 0.001 exactly at every passband and stopband frequency?
Very nearly. Kaiser fitted his β and M formulas to many designs, so they predict the length and shape well but not to the last digit; the test allows a 20% margin and the design uses much less. When a specification must be met exactly, designers check the response and add a tap if needed, which is what the dense-grid test does.
Name it
The three procedures
Kaiser window (lesson 22): ; β from Kaiser's three-range formula; ; ; with np.kaiser(M + 1, beta) (NumPy's sinc is ).
Bilinear Butterworth (lesson 19, T = 1): prewarp ; ; ; analog poles for k = 1, …, N; digital poles ; all N zeros at z = −1; scale so H(1) = 1. np.poly(roots) gives polynomial coefficients.
Welch (lesson 31): Hann segments of length L hopping L/2; average ; divide by for a density; double every bin except DC and Nyquist for the one-sided spectrum.
Worked example
What does the Butterworth procedure give for Oppenheim and Schafer's Example 7.3?
Show the solution
- Prewarp. = 0.6498 and = 1.0191.
- Order and cutoff. N = ⌈5.30⌉ = 6 and = 0.766.
- Analog poles. The six poles lie on a circle of that radius in the left half-plane, at angles 105°, 135°, 165° and their conjugates.
- Digital poles and zeros. The bilinear map puts the poles inside the unit circle near z = 1, and the six zeros at −1 make the stopband fall steeply toward π.
Answer N = 6 and = 0.766, with analog poles at 105°, 135° and 165° (and their conjugates) and all six zeros at z = −1.
Worked example
Check the Welch scaling with white noise of variance 4 at 8 kHz.
Show the solution
- Expected density. The one-sided density should be flat at
- Total power. Summing the estimate over all bins times the bin width should return the variance, 4.
- A tone. A tone , of power 1, should integrate to 1 across the few bins its Hann window spreads it over.
Answer a flat density of 0.001 per hertz that integrates to the variance, 4.
Check
Step 1. Write kaiser_lowpass(wp, ws, delta): a Kaiser-window lowpass from its band edges and ripple.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write butter_bilinear(wp, ws, Ap, As): a digital Butterworth lowpass by the bilinear transform.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write welch(x, L, fs): a one-sided power spectral density estimate in units² per hertz.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does the Butterworth designer set the cutoff from the stopband edge rather than the passband edge?
Once the order has been rounded up to a whole number, the filter is steeper than the specification strictly needs, so there's slack to place the cutoff anywhere in a small range and still meet both requirements. Setting so the stopband edge lands exactly at the required attenuation uses all of the slack on the passband side, giving it less loss than the 1 dB allowed. Setting it from the passband edge would meet the passband exactly and exceed the stopband requirement instead. Either is correct; choosing the stopband is common because bilinear designs often matter most where the stopband begins, and it makes the test at come out exactly at .
Practice
Keep practicing
3 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.