Equiripple and Frequency-Sampling FIR Design; Linear Phase
The window-method filter of the previous lesson met the vibration-channel mask with 60 taps, but it wasted accuracy: its passband ripple was when was allowed, and its stopband lobes fell from near the edge to below further out, where was enough. An optimal design spreads the error evenly over the bands, exactly up to the limits, and buys a shorter filter with the slack. This lesson covers the Parks–McClellan (Remez exchange) algorithm, the frequency-sampling method for arbitrary shapes, and the four types of linear-phase FIR filters that both methods produce.
The minimax idea
Parks and McClellan (1972) posed the design as a minimax problem: choose the taps to minimise the largest weighted error
where is the desired gain (1 in the passband, 0 in the stopband), is the filter's real amplitude response (its frequency response without the linear-phase term) and is a weight. The solution is equiripple: by the alternation theorem, the optimal error touches its maximum, with alternating sign, at least times ( is the number of free cosine coefficients, about ). The weight sets the ratio of the deviations: with in the passband and in the stopband, the stopband error is held times smaller. The transition band carries no weight at all ("don't care").
For equiripple filters, Kaiser's length estimate becomes
and, unlike the window formula, it rewards a loose passband: the geometric mean enters, not the smaller of the two.
Worked example: the shortest filter for the mask
Given: ; passband to with ; stopband to with .
Find: the stopband weight, an estimate of the length, the shortest equiripple filter that meets the mask, and the saving against the Kaiser window.
Solve: , , weight . Estimate: , , . Estimates are only a starting point, so search upward and measure each design:
import numpy as np
from scipy import signal
fs, fp, fst, Ap, As = 8000, 1000, 1500, 1.0, 60.0
dp = (10**(Ap / 20) - 1) / (10**(Ap / 20) + 1)
ds = 10**(-As / 20)
weight = [1, dp / ds] # weight the stopband error dp/ds times more heavily
print(f"delta_p = {dp:.4f}, delta_s = {ds:.4f}, stopband weight = {dp / ds:.1f}")
def measure(h):
f = np.linspace(0, fs / 2, 8001)
_, H = signal.freqz(h, worN=f, fs=fs)
g = 20 * np.log10(np.maximum(np.abs(H), 1e-12))
return g[f <= fp].max() - g[f <= fp].min(), -g[f >= fst].max()
for numtaps in range(30, 61):
h = signal.remez(numtaps, [0, fp, fst, fs / 2], [1, 0], weight=weight, fs=fs)
rip, att = measure(h)
if rip <= Ap and att >= As:
break
print(f"Parks-McClellan: {numtaps} taps, ripple {rip:.3f} dB, attenuation {att:.1f} dB, delay {(numtaps - 1) / 2} samples")
rip, att = measure(signal.remez(numtaps - 1, [0, fp, fst, fs / 2], [1, 0], weight=weight, fs=fs))
print(f"one tap fewer : {numtaps - 1} taps, ripple {rip:.3f} dB, attenuation {att:.1f} dB")
nk, beta = signal.kaiserord(As, (fst - fp) / (fs / 2))
print(f"Kaiser window : {nk} taps for the same mask -> equiripple saves {100 * (1 - numtaps / nk):.0f} %")delta_p = 0.0575, delta_s = 0.0010, stopband weight = 57.5
Parks-McClellan: 36 taps, ripple 0.826 dB, attenuation 61.6 dB, delay 17.5 samples
one tap fewer : 35 taps, ripple 1.012 dB, attenuation 59.8 dB
Kaiser window : 60 taps for the same mask -> equiripple saves 40 %
Check: 36 taps meet the mask (ripple , attenuation ) while 35 just fail both; the formula's 33 was a little optimistic, which is typical. The filter is 40 % shorter than the Kaiser design and its delay drops from to samples (). The figure shows the price: every stopband ripple peaks at the same level, , instead of decaying, so energy far above the band is attenuated less than with a window design. That matters if a large interferer sits far out in the stopband, and is irrelevant otherwise.
remez takes the band edges and desired values per band ([0, fp, fst, fs/2], [1, 0]), optional weight per band, and type='differentiator' or 'hilbert' for those special designs. For long filters (hundreds of taps) or very narrow bands it may fail to converge; then reduce the grid density, widen the transition, or use firls (least squares) or a multirate design.
The four linear-phase types
A real FIR filter has exactly linear phase when its taps are symmetric, , or antisymmetric, . Then (times for antisymmetric taps): every frequency is delayed by the same samples, the group delay, and the waveform keeps its shape. Pairing with forces zeros at (DC) or () for some types:
| Type | Length | Symmetry | Forced zero | Suitable for |
|---|---|---|---|---|
| I | odd | symmetric | none | any filter |
| II | even | symmetric | low-pass, band-pass (not high-pass) | |
| III | odd | antisymmetric | and | differentiator-like band-pass, Hilbert transformer |
| IV | even | antisymmetric | differentiator, Hilbert transformer, high-pass |
import numpy as np
from scipy import signal
# The four linear-phase FIR types: gain at DC (z = 1) and at fs/2 (z = -1) for random taps
rng = np.random.default_rng(3)
for name, N, sym in (("I odd N, symmetric", 9, 1), ("II even N, symmetric", 10, 1),
("III odd N, antisymmetric", 9, -1), ("IV even N, antisymmetric", 10, -1)):
a = rng.standard_normal(N)
h = (a + sym * a[::-1]) / 2 # force the symmetry
H0 = abs(np.sum(h)) # H(z = 1)
Hpi = abs(np.sum(h * (-1.0) ** np.arange(N))) # H(z = -1)
print(f"type {name:26s} |H(0)| = {H0:5.3f} |H(fs/2)| = {Hpi:5.3f}")
# Frequency sampling (firwin2): a low-pass with a +6 dB lift toward 3 kHz, fs = 8 kHz
fs = 8000
freq = [0, 1000, 3000, 3300, 4000]
gain = [1, 1, 2, 0, 0]
h = signal.firwin2(101, freq, gain, fs=fs)
fk = [500, 1000, 2000, 3000, 3600, 3900]
_, H = signal.freqz(h, worN=fk, fs=fs)
target = np.interp(fk, freq, gain)
for f, v, tg in zip(fk, H, target):
tdb = f"{20 * np.log10(tg):6.2f} dB" if tg > 0 else " stop "
print(f" {f:4d} Hz: target {tdb}, got {round(20 * np.log10(abs(v)), 2) + 0.0:7.2f} dB")type I odd N, symmetric |H(0)| = 4.450 |H(fs/2)| = 2.692
type II even N, symmetric |H(0)| = 2.002 |H(fs/2)| = 0.000
type III odd N, antisymmetric |H(0)| = 0.000 |H(fs/2)| = 0.000
type IV even N, antisymmetric |H(0)| = 0.000 |H(fs/2)| = 5.966
500 Hz: target 0.00 dB, got 0.00 dB
1000 Hz: target 0.00 dB, got 0.09 dB
2000 Hz: target 3.52 dB, got 3.52 dB
3000 Hz: target 6.02 dB, got 5.32 dB
3600 Hz: target stop , got -77.72 dB
3900 Hz: target stop , got -85.20 dB
The first four lines confirm the table with random taps: whatever the values, the symmetry alone forces the zeros. Linear phase also means the zeros come in groups: if is a zero, so is (and the conjugates), so zeros off the unit circle occur in quadruples. That is why a linear-phase filter cannot be minimum-phase, and why its delay is half its length.
Frequency sampling for arbitrary shapes
When the desired response is not a set of flat bands (an equaliser curve, a sensor correction, a sinc-droop compensator in Reconstruction and DACs), specify the gain at a list of frequencies and let firwin2 interpolate it on a dense grid, take the inverse FFT and apply a window. The second half of the program asks for a flat response up to , a linear rise to at , then a fast fall to zero at . It follows the target closely where the shape is smooth (the at is exact) and rounds off the sharp peak at ( instead of ): a corner in frequency needs a long impulse response, just as a brick-wall edge does. More taps sharpen the corner; a smoother target needs fewer.
Guided lab: equiripple in the measurement
Hand calculation. The 36-tap equiripple filter should hold every stopband frequency between and below , with every ripple peak at that level; the 60-tap Kaiser filter gives at , but its first side lobe just above (near ) peaks at , and further out its lobes fall below .
Simulation. Print the gains at , and for both filters with freqz. Then set the stopband weight to 1 and see the two bands' ripples become equal in linear terms, so the passband ripple shrinks and the stopband attenuation drops below .
What you would measure. On a USB-powered board sampling at , sweep a sine from the generator through the stopband and read the output with an averaged scope FFT. Expect a nearly constant output peaking at about for the equiripple filter (), compared with falling values for the Kaiser filter. Readings this small are at the converter's noise level: quote them with an uncertainty of several dB, and check the noise floor with the input shorted.
Hazards and safe setup. Low-voltage signals only (generator, USB board); keep the input within the converter range, set the generator with its output disabled and check it on a scope first.
Pitfalls
- Setting equal weights and then wondering why the stopband misses: the weight must be .
- Trusting the length estimate: search and measure.
- Designing a high-pass with an even length (type II has a zero at ).
- Expecting more attenuation far into the stopband from an equiripple design: it is flat at the specified level.
- Specifying a discontinuous target to
firwin2and blaming the method for the rounded corner.
Practice
- Estimate the equiripple length for , , .
- A 64-tap linear-phase FIR runs at . What is its group delay?
- Which linear-phase type would you choose for a digital differentiator, and why?
Answer
- , ; , about 52 taps.
- samples .
- Type IV (even length, antisymmetric): it has the zero at DC a differentiator needs and no forced zero at , where a differentiator's gain is largest. Type III would force a zero there.
Test yourself
An equiripple (Parks–McClellan) filter minimises
- the total squared error
- the largest weighted error over the bands
- the number of zeros
- the group delay
To hold the stopband deviation 57.5 times smaller than the passband deviation, set the stopband weight to
- 1/57.5
- 1
- 57.5
An even-length symmetric FIR always has a zero at
- DC
- nowhere
For the example mask, the shortest equiripple filter had
- 16 taps
- 36 taps
- 60 taps
- 6 taps