FIR Filter Design
A finite impulse response (FIR) filter computes each output as a weighted sum of the last inputs. FIR filters are always stable, can have exactly linear phase (no waveform distortion), and are designed directly from a desired frequency response. The classic method, the windowed sinc, takes the ideal impulse response, truncates it, and tapers it with a window.
The windowed-sinc method
For a low-pass with cutoff at sampling rate and an odd number of taps :
- Ideal response: , centered at .
- Multiply by a window of length .
- Normalize so (unit gain at DC).
The window's sidelobes set the stopband attenuation, and its main-lobe width sets the transition width.
import numpy as np
fs, fc, M = 1000.0, 100.0, 61
n = np.arange(M)
ideal = 2 * fc / fs * np.sinc(2 * fc / fs * (n - (M - 1) / 2))
windows = {"rectangular": np.ones(M), "hamming": np.hamming(M), "blackman": np.blackman(M)}
for name, w in windows.items():
h = ideal * w
h /= h.sum()
H = np.abs(np.fft.rfft(h, 8192))
f = np.fft.rfftfreq(8192, 1 / fs)
worst = [round(float(20 * np.log10(H[f > edge].max())), 1) for edge in (125, 150, 200)]
print(f"{name:12s} worst attenuation above 125, 150, 200 Hz (dB): {worst}")rectangular worst attenuation above 125, 150, 200 Hz (dB): [-27.1, -30.9, -37.9]
hamming worst attenuation above 125, 150, 200 Hz (dB): [-41.1, -61.1, -62.1]
blackman worst attenuation above 125, 150, 200 Hz (dB): [-26.3, -75.5, -83.4]
The rectangular window (plain truncation) stalls between and dB. Hamming reaches about dB from 150 Hz on. Blackman goes deepest, about dB above 150 Hz and dB above 200 Hz, but its wider transition means that just above the cutoff, at 125 Hz, it is still only at dB.
Linear phase
A symmetric impulse response, , has frequency response with real : every frequency is delayed by the same samples. Waveforms keep their shape, which matters for ECG, audio transients and data communication.
import numpy as np
M = 31
n = np.arange(M)
h = 0.2 * np.sinc(0.2 * (n - 15)) * np.hamming(M)
h /= h.sum()
print("symmetric:", np.allclose(h, h[::-1]))
for w in (0.05, 0.1, 0.2):
H = np.sum(h * np.exp(-1j * w * n))
print(f"omega {w}: phase / omega = {np.angle(H) / w:.4f} samples of delay (expected -{(M - 1) / 2})")symmetric: True
omega 0.05: phase / omega = -15.0000 samples of delay (expected -15.0)
omega 0.1: phase / omega = -15.0000 samples of delay (expected -15.0)
omega 0.2: phase / omega = -15.0000 samples of delay (expected -15.0)
Designing other types
- High-pass: spectral inversion, .
- Band-pass: difference of two low-passes, or a low-pass modulated by .
- Optimal designs: the Parks-McClellan (Remez) algorithm minimizes the maximum error, giving equiripple filters that need fewer taps than windowed sincs for the same spec.
In software
scipy.signal.firwin(numtaps, cutoff, window=..., fs=...)implements the windowed-sinc method;remezandfirlsimplement optimal designs;freqzchecks the result.- A rule of thumb for the length with a Hamming window: taps for a transition width .
Exercises
Exercise 1: Estimate the length
You need a Hamming-windowed low-pass with a 50 Hz transition band at Hz. About how many taps?
Answer
About 530.
Solution
; round to an odd number such as 529. Sharp transitions at high sampling rates need long FIR filters, which is where FFT convolution or IIR designs help.
Exercise 2: Delay of a linear-phase FIR
What is the delay of a 101-tap linear-phase FIR at 48 kHz?
Answer
50 samples, about 1.04 ms.
Solution
samples, ms.
Exercise 3: A high-pass by inversion (code)
Build a 61-tap high-pass with cutoff 100 Hz at Hz by spectral inversion of the Hamming low-pass, and print its gain at 0, 50, 200 and 400 Hz.
Answer
About 0 at 0 and 50 Hz, about 1 at 200 and 400 Hz.
Solution
import numpy as np
fs, fc, M = 1000.0, 100.0, 61
n = np.arange(M)
lp = 2 * fc / fs * np.sinc(2 * fc / fs * (n - 30)) * np.hamming(M)
lp /= lp.sum()
hp = -lp
hp[30] += 1 # delta at the center minus the low-pass
for f in (0, 50, 200, 400):
H = np.sum(hp * np.exp(-2j * np.pi * f / fs * n))
print(f, round(abs(H), 4))0 0.0
50 0.0039
200 0.9992
400 0.9997
Test yourself
Why are FIR filters always stable?
- they use feedback
- their impulse response is finite, so it is absolutely summable
- they are causal
- they have linear phase
A symmetric FIR impulse response gives…
- minimum phase
- linear phase (constant delay)
- zero gain at DC
- an unstable filter
In the windowed-sinc method, the window mainly controls…
- the cutoff frequency
- stopband attenuation and transition width
- the sampling rate
- the filter's delay only