Filter Specifications and the Design Workflow
"Put a low-pass filter on it" is not a specification. A vibration channel sampled at needs everything below kept within and a motor whine above pushed down by . Those four numbers decide whether the filter costs 15 multiplications per sample or 60, whether it delays the signal by about or nearly , and whether it distorts the shape of a pulse. This lesson turns a requirement into a tolerance mask, estimates the order each filter family needs, and lays out the workflow the next six lessons follow. What a filter is (FIR, IIR, moving average) is covered in Filters: Introduction; the analog versions are in First-Order Filters and Second-Order Filters.
The tolerance mask
A frequency-selective filter is specified by bands and tolerances, not by a single cutoff:
| Quantity | Symbol | Example | Meaning |
|---|---|---|---|
| sampling rate | all digital frequencies live in | ||
| passband edge | highest frequency that must be kept | ||
| stopband edge | lowest frequency that must be removed | ||
| passband ripple | allowed peak-to-peak gain variation in the passband | ||
| stopband attenuation | minimum attenuation everywhere in the stopband |
The band between and is the transition band: the filter may do anything there. Design methods often state the tolerances as linear deviations: the passband gain stays in and the stopband gain below , with
so is and is . Some tools define the ripple from down to only (gain to dB); say which convention you use in the specification.
Other requirements come with the mask: phase (must the waveform shape survive? then linear phase, so FIR or a delay-equalised IIR), delay (a control loop or a hearing aid tolerates only a few milliseconds), cost (multiplications per sample, memory, word length) and step response (overshoot matters for sensor data, not for audio).
How hard is a mask? Order estimates
The difficulty grows with the attenuation and shrinks with the normalised transition width . For a linear-phase FIR filter Kaiser's estimate of the length is
so halving the transition band doubles the length, while each extra costs only about 40 % more taps (from to ). For IIR filters the analog prototype orders (next section, Analog Prototypes) grow with the logarithm of the attenuation divided by the logarithm of the steepness (after pre-warping). SciPy has an estimator for each family:
import numpy as np
from scipy import signal
# Specification: fs = 8 kHz, passband 0-1 kHz with 1 dB ripple, stopband from 1.5 kHz with 60 dB attenuation
fs, fp, fst, Ap, As = 8000, 1000, 1500, 1.0, 60.0
dp = (10**(Ap / 20) - 1) / (10**(Ap / 20) + 1) # passband deviation from Ap = 20 log10((1+dp)/(1-dp))
ds = 10**(-As / 20) # stopband deviation
print(f"delta_p = {dp:.4f}, delta_s = {ds:.6f}, transition = {fst - fp} Hz = {(fst - fp) / fs:.4f} fs")
numtaps, beta = signal.kaiserord(As, (fst - fp) / (fs / 2))
print(f"FIR, Kaiser window : {numtaps} taps (beta = {beta:.3f})")
for name, fn in (("Butterworth", signal.buttord), ("Chebyshev I", signal.cheb1ord),
("Chebyshev II", signal.cheb2ord), ("elliptic", signal.ellipord)):
n, wn = fn(fp, fst, Ap, As, fs=fs)
print(f"IIR, {name:12s}: order {n:2d} -> {int(np.ceil(n / 2))} biquads, about {5 * int(np.ceil(n / 2))} multiplies/sample")delta_p = 0.0575, delta_s = 0.001000, transition = 500 Hz = 0.0625 fs
FIR, Kaiser window : 60 taps (beta = 5.653)
IIR, Butterworth : order 16 -> 8 biquads, about 40 multiplies/sample
IIR, Chebyshev I : order 8 -> 4 biquads, about 20 multiplies/sample
IIR, Chebyshev II: order 8 -> 4 biquads, about 20 multiplies/sample
IIR, elliptic : order 6 -> 3 biquads, about 15 multiplies/sample
FIR or IIR?
| FIR | IIR | |
|---|---|---|
| phase | exactly linear if the taps are symmetric: delay samples | non-linear, worst near the band edge |
| stability | always stable | poles must stay inside the unit circle, also after rounding the coefficients |
| cost for a sharp mask | high (tens to hundreds of taps) | low (a few biquads) |
| fixed point | robust, no feedback, no limit cycles | needs care: scaling, limit cycles |
| design | window, equiripple, frequency sampling | analog prototype + bilinear transform |
| typical use | audio crossovers with phase constraints, communications pulse shaping, decimation | sensor smoothing, audio equalisers, anything replacing an analog filter cheaply |
Worked example: choosing a filter for the vibration channel
Given: the mask above: , , , , ; no phase requirement, but the delay should stay below .
Find: the cost and delay of a Kaiser-window FIR and of the cheapest IIR.
Solve: Kaiser's formula with : , and kaiserord returns 60 taps. A symmetric FIR needs about 30 multiplications per sample (pairs of equal taps share one multiplier) and delays every frequency by samples . The elliptic filter needs order 6: three biquads, about 15 multiplications per sample, and a group delay that is small at low frequencies ( at DC) but peaks near the band edge ( just below ; the Frequency Response lesson shows how to compute it). A Butterworth would need order 16: the maximally flat family pays for its smoothness with order.
Check: design both and test them against the mask on a dense frequency grid. Never trust an order estimate without this check.
import numpy as np
from scipy import signal
fs, fp, fst, Ap, As = 8000, 1000, 1500, 1.0, 60.0
def check(name, sos=None, b=None):
f = np.linspace(0, fs / 2, 4001)
if sos is not None:
_, H = signal.sosfreqz(sos, worN=f, fs=fs)
else:
_, H = signal.freqz(b, worN=f, fs=fs)
g = 20 * np.log10(np.maximum(np.abs(H), 1e-12))
pb, sb = g[f <= fp], g[f >= fst]
ripple = pb.max() - pb.min()
atten = -sb.max()
ok = ripple <= Ap + 1e-3 and atten >= As - 1e-3
print(f"{name:22s} passband ripple {ripple:5.3f} dB, stopband attenuation {atten:5.1f} dB -> {'meets' if ok else 'FAILS'} the mask")
n, wn = signal.ellipord(fp, fst, Ap, As, fs=fs)
check(f"elliptic, order {n}", sos=signal.ellip(n, Ap, As, wn, output='sos', fs=fs))
check(f"elliptic, order {n - 1}", sos=signal.ellip(n - 1, Ap, As, wn, output='sos', fs=fs))
numtaps, beta = signal.kaiserord(As, (fst - fp) / (fs / 2))
check(f"FIR Kaiser, {numtaps} taps", b=signal.firwin(numtaps, (fp + fst) / 2, window=('kaiser', beta), fs=fs))elliptic, order 6 passband ripple 1.000 dB, stopband attenuation 60.0 dB -> meets the mask
elliptic, order 5 passband ripple 1.000 dB, stopband attenuation 52.5 dB -> FAILS the mask
FIR Kaiser, 60 taps passband ripple 0.016 dB, stopband attenuation 60.4 dB -> meets the mask
The order-5 elliptic filter misses the stopband by more than , so order 6 is the minimum. The FIR meets the mask with a passband ripple far smaller than allowed: the window method gives (nearly) the same deviation in both bands, so the stopband forces , i.e. about . An equiripple design can trade that wasted passband accuracy for fewer taps.
The design workflow
- Write the mask from the application: band edges in hertz, ripple and attenuation in dB, phase, delay and cost limits. Check .
- Choose the family with the table above and estimate the order.
- Design (window, equiripple, or prototype + bilinear transform), as second-order sections for IIR.
- Verify the frequency response against the mask on a dense grid, plus the group delay and the step or impulse response.
- Quantise the coefficients to the target word length and verify again (Biquads and SOS).
- Simulate the implementation with real signals, then measure the built system.
Guided lab: measuring a filter against its mask
Hand calculation. For the elliptic filter of the example, the gain should lie between and up to and below from on; at (inside the transition band) anything goes.
Simulation. Run the check program; then tighten the stopband edge to and see which orders change.
What you would measure. Run the filter on a microcontroller or audio interface sampling at , feed sines from a function generator ( amplitude, inside the converter's input range) and read input and output amplitudes on a scope or with the FFT. Expect the passband readings within about of the design (generator flatness, ADC and DAC gain). Stopband readings near are only for a input, close to the noise floor of a 12-bit converter; measure them with an FFT and averaging, and expect them to be limited by noise, not by the filter.
Hazards and safe setup. Low-voltage signals only: generator and USB-powered board. Set the generator offset and amplitude with its output off and check them on a scope so the ADC pin never sees less than or more than its supply.
Pitfalls
- Specifying only "cutoff ": a cutoff is a point, not a mask, and says nothing about attenuation.
- Mixing up the band edges in hertz with normalised frequency (SciPy's
fs=argument avoids this; without it, means Nyquist, not ). - Trusting an order estimate without checking the designed response.
- Forgetting the delay: 60 taps at is , too much inside a fast control loop.
- Placing the stopband edge above : the digital filter cannot see it.
Practice
- Convert and to and .
- Estimate the Kaiser FIR length for and a transition of at .
- A linear-phase FIR has 101 taps at . What is its delay?
Answer
- ; .
- ; taps: sharp transitions at high sampling rates are expensive, which is one reason for multirate designs.
- samples .
Test yourself
dB corresponds to a stopband gain of at most
- 0.06
- 0.01
- 0.001
Halving the transition width of a Kaiser-window FIR roughly
- leaves the length unchanged
- doubles the length
- halves the length
- adds 6 dB of attenuation
For the same mask, which IIR family needs the lowest order?
- Butterworth
- Chebyshev I
- Chebyshev II
- elliptic
Why does the window-method FIR in the example have only 0.02 dB passband ripple?
- the window is too long
- the window method gives about the same deviation in both bands, set by the stopband
- the elliptic filter helps it
- rounding of the coefficients