C943 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

What the code does

  • Windowed sinc: taps h[n]=w[n]sin⁡(ωc(n−M))/(π(n−M)) for n = 0 to N − 1, with M = (N − 1)/2 and the center tap ωc/π. The Hamming window is w[n]=0.54−0.46cos⁡(2πn/(N−1)).
  • Kaiser sizing: N=⌈(A−8)/(2.285Δω)+1⌉ and β by the piecewise formula of lesson 41 (0 below 21 dB).
  • Bilinear first-order low-pass: Ωc=tan⁡(πfc/fs), b=[k,k] and a=[1,(Ωc−1)/(Ωc+1)] with k=Ωc/(1+Ωc).
  • 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 ωc=π/2. What are its center tap and the taps next to it?

Show the solution
  1. Center. The Hamming window is 1 there: ωc/π=0.5
  2. Window one away. w=0.54−0.46cos⁡(2π×9/20)=0.977
  3. Tap one away. The ideal tap is sin⁡(π/2)/π=0.318, so 0.318w=0.318×0.977=0.311
  4. Even offsets. Every even offset is zero for this cutoff, because sin⁡(πk/2)=0 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
  1. Passband. It stays within 0.03 dB of flat up to 1 kHz.
  2. 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.
  3. A longer filter. At 57 taps the worst point is 52.1 dB down and the design passes.
  4. 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?

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.