TExcelTAD
Home › Signals, Systems & Digital Filters for Engineers › Equiripple and Frequency-Sampling FIR Design; Linear Phase

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 0.02 dB0.02\ \text{dB} when 1 dB1\ \text{dB} was allowed, and its stopband lobes fell from −60.4 dB-60.4\ \text{dB} near the edge to below −70 dB-70\ \text{dB} further out, where −60 dB-60\ \text{dB} 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 NN taps to minimise the largest weighted error

max⁡f∈bands  W(f) ∣Hd(f)−A(f)∣\max_{f \in \text{bands}} \; W(f)\,\bigl\lvert H_d(f) - A(f) \bigr\rvert

where HdH_d is the desired gain (1 in the passband, 0 in the stopband), A(f)A(f) is the filter's real amplitude response (its frequency response without the linear-phase term) and W(f)W(f) is a weight. The solution is equiripple: by the alternation theorem, the optimal error touches its maximum, with alternating sign, at least L+1L + 1 times (LL is the number of free cosine coefficients, about N/2N/2). The weight sets the ratio of the deviations: with W=1W = 1 in the passband and W=δp/δsW = \delta_p/\delta_s in the stopband, the stopband error is held δp/δs\delta_p/\delta_s times smaller. The transition band carries no weight at all ("don't care").

0 1 kHz 1.5 2 3 4 kHz 0 -20 -40 -60 -80 -100 gain (dB) equiripple, 36 taps Kaiser window, 60 taps frequency (Hz), fs = 8 kHz; dashed red line = the -60 dB requirement

For equiripple filters, Kaiser's length estimate becomes

N≈−20log⁡10δpδs−1314.6 Δf/fs+1N \approx \frac{-20\log_{10}\sqrt{\delta_p \delta_s} - 13}{14.6\,\Delta f/f_s} + 1

and, unlike the window formula, it rewards a loose passband: the geometric mean δpδs\sqrt{\delta_p\delta_s} enters, not the smaller of the two.

Worked example: the shortest filter for the mask

Given: fs=8 kHzf_s = 8\ \text{kHz}; passband 00 to 1 kHz1\ \text{kHz} with Ap=1 dBA_p = 1\ \text{dB}; stopband 1.51.5 to 4 kHz4\ \text{kHz} with As=60 dBA_s = 60\ \text{dB}.

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: δp=0.0575\delta_p = 0.0575, δs=0.001\delta_s = 0.001, weight =57.5= 57.5. Estimate: δpδs=0.00758\sqrt{\delta_p\delta_s} = 0.00758, −20log⁡100.00758=42.4 dB-20\log_{10} 0.00758 = 42.4\ \text{dB}, N≈(42.4−13)/(14.6×0.0625)+1=33.2N \approx (42.4 - 13)/(14.6 \times 0.0625) + 1 = 33.2. Estimates are only a starting point, so search upward and measure each design:

python · try it yourself
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 0.83 dB0.83\ \text{dB}, attenuation 61.6 dB61.6\ \text{dB}) 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 29.529.5 to 17.517.5 samples (2.19 ms2.19\ \text{ms}). The figure shows the price: every stopband ripple peaks at the same level, −61.6 dB-61.6\ \text{dB}, 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, h[n]=h[N−1−n]h[n] = h[N - 1 - n], or antisymmetric, h[n]=−h[N−1−n]h[n] = -h[N - 1 - n]. Then H(ejω)=A(ω) e−jω(N−1)/2H(e^{j\omega}) = A(\omega)\,e^{-j\omega (N - 1)/2} (times jj for antisymmetric taps): every frequency is delayed by the same (N−1)/2(N-1)/2 samples, the group delay, and the waveform keeps its shape. Pairing nn with N−1−nN - 1 - n forces zeros at z=1z = 1 (DC) or z=−1z = -1 (fs/2f_s/2) for some types:

Type Length Symmetry Forced zero Suitable for
I odd symmetric none any filter
II even symmetric z=−1z = -1 low-pass, band-pass (not high-pass)
III odd antisymmetric z=1z = 1 and z=−1z = -1 differentiator-like band-pass, Hilbert transformer
IV even antisymmetric z=1z = 1 differentiator, Hilbert transformer, high-pass
python · try it yourself
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 z0z_0 is a zero, so is 1/z01/z_0 (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 1 kHz1\ \text{kHz}, a linear rise to +6 dB+6\ \text{dB} at 3 kHz3\ \text{kHz}, then a fast fall to zero at 3.3 kHz3.3\ \text{kHz}. It follows the target closely where the shape is smooth (the 3.52 dB3.52\ \text{dB} at 2 kHz2\ \text{kHz} is exact) and rounds off the sharp peak at 3 kHz3\ \text{kHz} (5.325.32 instead of 6.02 dB6.02\ \text{dB}): 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 1.51.5 and 4 kHz4\ \text{kHz} below −61.6 dB-61.6\ \text{dB}, with every ripple peak at that level; the 60-tap Kaiser filter gives −66.6 dB-66.6\ \text{dB} at 1.5 kHz1.5\ \text{kHz}, but its first side lobe just above (near 1.53 kHz1.53\ \text{kHz}) peaks at −60.4 dB-60.4\ \text{dB}, and further out its lobes fall below −70 dB-70\ \text{dB}.

Simulation. Print the gains at 1.51.5, 2.52.5 and 3.5 kHz3.5\ \text{kHz} 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 60 dB60\ \text{dB}.

What you would measure. On a USB-powered board sampling at 8 kSa/s8\ \text{kSa/s}, sweep a 0.5 V0.5\ \text{V} sine from the generator through the stopband and read the output with an averaged scope FFT. Expect a nearly constant output peaking at about 0.4 mV0.4\ \text{mV} for the equiripple filter (−61.6 dB-61.6\ \text{dB}), 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 δp/δs\delta_p/\delta_s.
  • Trusting the length estimate: search and measure.
  • Designing a high-pass with an even length (type II has a zero at fs/2f_s/2).
  • Expecting more attenuation far into the stopband from an equiripple design: it is flat at the specified level.
  • Specifying a discontinuous target to firwin2 and blaming the method for the rounded corner.

Practice

  1. Estimate the equiripple length for δp=0.01\delta_p = 0.01, δs=0.001\delta_s = 0.001, Δf/fs=0.05\Delta f/f_s = 0.05.
  2. A 64-tap linear-phase FIR runs at 48 kHz48\ \text{kHz}. What is its group delay?
  3. Which linear-phase type would you choose for a digital differentiator, and why?
Answer
  1. 10−5=0.00316\sqrt{10^{-5}} = 0.00316, −20log⁡10=50 dB-20\log_{10} = 50\ \text{dB}; N≈37/(14.6×0.05)+1=51.7N \approx 37/(14.6 \times 0.05) + 1 = 51.7, about 52 taps.
  2. (64−1)/2=31.5(64 - 1)/2 = 31.5 samples =0.656 ms= 0.656\ \text{ms}.
  3. Type IV (even length, antisymmetric): it has the zero at DC a differentiator needs and no forced zero at fs/2f_s/2, where a differentiator's gain is largest. Type III would force a zero there.

Test yourself

An equiripple (Parks–McClellan) filter minimises

  1. the total squared error
  2. the largest weighted error over the bands
  3. the number of zeros
  4. the group delay

To hold the stopband deviation 57.5 times smaller than the passband deviation, set the stopband weight to

  1. 1/57.5
  2. 1
  3. 57.5
  4. 57.5257.5^2

An even-length symmetric FIR always has a zero at

  1. DC
  2. fs/4f_s/4
  3. fs/2f_s/2
  4. nowhere

For the example mask, the shortest equiripple filter had

  1. 16 taps
  2. 36 taps
  3. 60 taps
  4. 6 taps