FIR filters and windows

Design a filter by truncating the ideal one — and choose a window to control what the truncation costs.

What you'll learn
  • Design low-, high- and band-pass FIR filters with the window method
  • Explain how tap count and window shape set transition width and stopband depth
  • Read a filter's frequency response and check it against its job

A finite impulse response (FIR) filter is a convolution with a fixed, finite kernel: its taps. The ideal low-pass filter — pass everything below \(f_c\), block everything above — has an impulse response that is a sinc, \(\sin(2\pi f_c n)/(\pi n)\), which runs on forever. The window method keeps \(N\) taps of it, tapers them with a window so the cut is gentle, and normalises the sum to 1. That is what scipy.signal.firwin does.

Windowed-sinc design as the tap count grows from 5 to 61. Top: the taps. Bottom: the exact frequency response in dB, with the cutoff dashed. More taps steepen the transition; the window decides how far the stopband sinks.

Two knobs, two effects:

  • More taps → a narrower transition band. The transition width is roughly proportional to \(1/N\).
  • The window sets the stopband floor, and more taps do not lower it. Plain truncation (the boxcar) leaves ripple at about −21 dB next to the edge however long the filter — the Gibbs overshoot of lesson 4, now in the frequency domain.
Peak stopband ripple below the passband for a 61-tap windowed-sinc low-pass. Hamming is firwin's default; Blackman goes deepest at the price of a wider transition.

Windowed Filter/ plots fifteen windows, and 1_fir_filter_impl.py uses the Kaiser window, which turns the trade-off into a formula: kaiserord(60, 5/50) asks for 60 dB of attenuation with a 5 Hz transition at a 100 Hz sampling rate and returns 74 taps with \(\beta = 5.65\).

Three filters on one signal

2_fir_win.py mixes 5, 50 and 250 Hz sines sampled at 2 kHz and separates them:

numtaps = 101
lowpass_coeff  = signal.firwin(numtaps, 7, nyq=1000)
highpass_coeff = signal.firwin(numtaps, 100, pass_zero=False, nyq=1000)
bandpass_coeff = signal.firwin(numtaps, [40, 100], pass_zero=False, nyq=1000)
low_pass_output = np.convolve(lowpass_coeff, sig_5hz_50hz_50hz)

Evaluating the three designs at the three frequencies:

Filter gain at 5 Hz at 50 Hz at 250 Hz
low-pass, 7 Hz 0.953 0.002 0.003
high-pass, 100 Hz 0.002 0.001 1.000
band-pass, 40–100 Hz 0.000 0.768 0.000

The low- and high-pass filters do their jobs. The band-pass filter passes the 50 Hz tone at only 77% of its amplitude: with 101 taps at 2 kHz the transition band is tens of hertz wide, and 50 Hz sits only 10 Hz inside the 40 Hz edge. Widening the band (say 30–100 Hz) or adding taps fixes it — a reminder to check a filter’s response at the frequencies you care about, not just its cutoffs.

Two maintenance notes. SciPy 1.11 already warns that the nyq= argument “will be removed in SciPy 1.12.0”; on current versions these lines fail, and fs=2000 is the spelling to use. And the commented-out band-reject block at the end repeats the band-pass call unchanged — a band-stop needs pass_zero=True with the two cutoffs.

Try it yourself. Rewrite the three firwin calls with fs=2000, then plot 20*np.log10(abs(h)) from signal.freqz(bandpass_coeff, fs=2000). Find the smallest numtaps that passes 50 Hz with at least 0.95 gain while keeping 5 Hz below −40 dB.

View source on GitHub →