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

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 ωp = 0.4π, ωs = 0.6π and δ = 0.001. Will the finished filter meet δ = 0.001 exactly at every passband and stopband frequency?

Name it

The three procedures

Kaiser window (lesson 22): A=−20log⁡δ; β from Kaiser's three-range formula; M=⌈(A−8)/(2.285Δω)⌉; ωc=(ωp+ωs)/2; h[n]=ωcπsinc(ωc(n−M/2)π)w[n] with np.kaiser(M + 1, beta) (NumPy's sinc is sin⁡(πx)/(πx)).

Bilinear Butterworth (lesson 19, T = 1): prewarp Ω=2tan⁡(ω/2); N=⌈log⁡[(10As/10−1)/(10Ap/10−1)]2log⁡(Ωs/Ωp)⌉; Ωc=Ωs/(10As/10−1)1/2N; analog poles sk=Ωcejπ(2k+N−1)/2N for k = 1, …, N; digital poles zk=1+sk/21−sk/2; 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 |X|2; divide by fs∑w2 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
  1. Prewarp. Ωp = 0.6498 and Ωs = 1.0191.
  2. Order and cutoff. N = ⌈5.30⌉ = 6 and Ωc = 0.766.
  3. 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.
  4. 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 Ωc = 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
  1. Expected density. The one-sided density should be flat at 2σ2fs=88000=0.001per hertz
  2. Total power. Summing the estimate over all bins times the bin width fs/L should return the variance, 4.
  3. A tone. A tone 2cos⁡(⋅), 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?

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.