C9 · Lesson 43 · Filter design lab
Designing FIR and IIR filters in Python, and checking them against a specification
A filter specification is a promise: this much ripple in the passband, at least this much loss in the stopband, between these frequencies. Lessons 41 and 42 gave formulas for meeting one. In this lab you'll turn the formulas into code: generate windowed-sinc taps, size Kaiser and Butterworth designs, build a bilinear low-pass, and write the checker that tells you whether a design keeps its promise.
- The curve is a Butterworth low-pass, designed by the bilinear transform at 8 kHz so it loses exactly 1 dB at 1 kHz, for order N.
- The specification asks for at least 40 dB of loss from 2 kHz up: the readout gives what this order achieves there.
- Step N up from 2. Order 5 falls just short at 2 kHz; order 6 clears it, as the order formula predicts (5.99, rounded up).
Predict
A 6th-order Butterworth design meets the specification above. An FIR filter designed with a Kaiser window for the same edges and 40 dB needs how many taps?
19: , rounded up. The IIR filter needs about 15 coefficients for its three second-order sections, so here the two are close, and the FIR filter buys exactly linear phase. With narrower transitions the gap widens quickly in the IIR filter's favor.
Name it
What the code does
- Windowed sinc: taps for n = 0 to N − 1, with M = (N − 1)/2 and the center tap . The Hamming window is .
- Kaiser sizing: and β by the piecewise formula of lesson 41 (0 below 21 dB).
- Bilinear first-order low-pass: , and with .
- Butterworth order: prewarp both edges, then the order formula, rounded up.
- Checking a specification: evaluate the response on a dense frequency grid (a zero-padded FFT of the taps does it for an FIR filter), then find the largest passband deviation and the smallest stopband loss.
Worked example
A 21-tap Hamming low-pass has . What are its center tap and the taps next to it?
Show the solution
- Center. The Hamming window is 1 there:
- Window one away.
- Tap one away. The ideal tap is , so
- Even offsets. Every even offset is zero for this cutoff, because there.
Answer the center tap is 0.5, and the taps next to it are 0.311.
Worked example
Lesson 41 sized a Hamming low-pass for 1 kHz and 1.5 kHz at 8 kHz, with 50 dB of stopband loss, at 53 taps. What does a checker find?
Show the solution
- Passband. It stays within 0.03 dB of flat up to 1 kHz.
- Stopband. The worst point from 1.5 kHz up is only 49.6 dB down: it misses by 0.4 dB, because the formula's transition width is an estimate.
- A longer filter. At 57 taps the worst point is 52.1 dB down and the design passes.
- A shorter one. A 31-tap version loses only 20.6 dB at 1.5 kHz, still in its transition band.
Answer the 53-tap design misses the stopband spec by 0.4 dB (49.6 dB down); at 57 taps it passes, 52.1 dB down.
Check
A bilinear Butterworth at 8 kHz loses 1 dB at 1 kHz. What does order 5 lose at 2 kHz?
Check
Write hamming_lowpass(wc, N): the N taps (N odd) of a Hamming-windowed ideal low-pass with cutoff wc radians per sample, as a list.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write kaiser_design(A, dw): for stopband attenuation A dB and transition dw rad/sample, return (N, beta) with N rounded up to an integer and β by Kaiser's formula (0 for A < 21).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write bilinear_lowpass(fc, fs): the first-order bilinear low-pass with its −3 dB point at fc, as (b, a) with b = [b0, b1] and a = [1, a1].
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write butter_order(fp, fst, Ap, As, fs): the Butterworth order for at most Ap dB loss up to fp and at least As dB from fst, designed by the bilinear transform at fs (prewarp both edges).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write meets_spec(h, fs, fp, fst, ripple_db, atten_db): True if the FIR filter h stays within ±ripple_db of 0 dB up to fp and at least atten_db down from fst to fs/2. Evaluate it with a 16384-point zero-padded rfft.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why check a filter against its specification on a dense frequency grid, rather than trusting the design formulas?
The design formulas are estimates. Window lengths come from approximate transition widths, Kaiser's formulas are curve fits, and rounding, quantized coefficients or an unlucky cutoff can leave a design a decibel or two short at one frequency. A filter's worst ripple or weakest stopband point can fall between any coarse set of test frequencies, so the response has to be evaluated finely enough to catch every peak, which a heavily zero-padded FFT does cheaply. Checking the actual taps against the actual limits turns "should work" into "does work", and it catches mistakes such as a wrong cutoff or a forgotten prewarp that formulas would never reveal.
Practice
Keep practicing
One kind of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.