Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

DSP: filter design, FIR and IIR filters, adaptive filters

A filter changes the frequency content of a signal: it keeps a band and removes the rest, removes one interfering tone, differentiates, or matches a known waveform. Digital filters come in two kinds. FIR filters compute each output from a finite window of inputs, by convolution. They are always stable and can have exactly linear phase, but need many taps. IIR filters also feed back past outputs, which buys sharp responses from a handful of coefficients at the cost of nonlinear phase and numerical fragility. Adaptive filters adjust their own taps as they run. This chapter covers how to design all three, and how to run them efficiently and accurately. It is the first of the book’s DSP chapters; spectral estimation, windows and multirate processing follow.

The central questions

  • FIR or IIR? Linear phase, guaranteed stability and easy adaptivity favour FIR; sharpness at low cost favours IIR.
  • How sharp, and at what order? A specification (passband ripple, stopband attenuation, transition width) fixes the order of an IIR family or the length of an FIR filter. The order-selection functions and Kaiser’s formulas compute it.
  • Which approximation? Butterworth, Chebyshev, elliptic and Bessel trade flatness, sharpness and phase for IIR filters. Windows, frequency sampling and equiripple design do the same for FIR filters.
  • How to compute it? Direct convolution for short FIR filters and FFT methods for long ones. For IIR filters, structures that tolerate rounding.

Background: frequency responses and transfer functions

The DSP chapters use a small vocabulary that is worth collecting in one place. Readers who have taken a signals course can skip this section.

Signals and frequencies. A digital signal is a sequence of samples , taken times per second (the sampling rate). A sinusoid of frequency hertz advances by radians from one sample to the next; is the frequency in radians per sample. The highest frequency a sampled signal can represent is the Nyquist frequency , or , at which the samples alternate in sign. It is convenient to work with the complex exponential , which carries a cosine and a sine together. (Engineers write for , and so do these chapters.)

Linear time-invariant filters. A digital filter computes each output sample as a weighted sum of current and past inputs and, possibly, past outputs:

If there are no feedback terms the filter is FIR, and its impulse response (the output when the input is a single 1 followed by zeros) is just the list of coefficients , called taps. With feedback the impulse response goes on for ever: the filter is IIR.

The frequency response. Feed such a filter the complex exponential . A delay of samples multiplies it by , so every term of the sum is a multiple of the input, and the output is the same exponential multiplied by a complex number,

A filter does not change the frequency of a sinusoid; it changes only its amplitude, by the factor (the gain), and its phase, by . as a function of is the frequency response. Since any signal is a sum of sinusoids, describes completely what the filter does. A low-pass filter has at low frequencies (its passband) and at high ones (its stopband), with a transition band between.

Decibels. Gains are quoted on a logarithmic scale, decibels (dB). A gain of in amplitude is dB, is dB, and dB is a factor of in amplitude, or one half in power.

The transfer function, poles and zeros. Writing for , the frequency response is the value on the unit circle of a ratio of two polynomials in ,

the transfer function. Here stands for “delay by one sample”. The roots of the numerator are the zeros of the filter and the roots of the denominator its poles; with an overall gain factor, they determine it completely. They also give a geometric picture of the response: as moves round the unit circle, the gain dips where the point passes near a zero and peaks where it passes near a pole.

Stability. Each pole contributes a term proportional to to the impulse response. If the term decays; if it grows without bound. A filter is stable exactly when all its poles lie inside the unit circle. FIR filters have no poles (other than at the origin) and are always stable.

Analogue filters. A circuit is described in the same way, with continuous time. Its response to is , where the transfer function is a ratio of polynomials in the complex variable , and the frequency response is its value on the imaginary axis . A pole contributes to the impulse response, so an analogue filter is stable when all its poles lie in the left half-plane, .

Sampling and aliasing

Where do the samples come from, and what is lost by taking them? Sample a continuous signal every seconds. A cosine of frequency then gives . Now add any whole multiple to the frequency:

because is a whole number of turns. The samples cannot tell from (nor from , since cosine is even). This is aliasing: a 7 Hz cosine sampled 10 times a second produces , exactly the samples of a 3 Hz cosine. In general a frequency is seen as the distance from to the nearest multiple of , a number always between and . The signal has been folded about multiples of .

The same fact in the frequency domain: sampling replicates the spectrum. If is the spectrum of , the sampled signal has the spectrum , copies of centred at every multiple of the sampling rate. (The derivation is the Poisson summation formula; the picture is what matters.) The copies overlap, and information is destroyed, unless occupies a band narrower than . That gives the sampling theorem (Nyquist, Shannon, Whittaker, Kotelnikov): if contains no frequencies at or above , it is determined by its samples, and

Sampling above twice the highest frequency present loses nothing, and no processing afterwards can undo the damage if it was done below. Much of the practice in this and the following chapters is an application of these three sentences:

  • Anti-alias filter before decimating. Keeping every -th sample divides the sampling rate by , so anything above the new Nyquist frequency folds into the band. The low-pass filter must come first (resampling).
  • Upsampling creates images. Inserting zeros compresses the spectrum and leaves extra copies, which a low-pass filter must remove.
  • Non-band-limited waveforms alias when generated. A square wave has harmonics without end, so any sampled version has some folded back.
  • The DFT sees one period. The spectrum computed from a finite record is a sampled version of the spectrum of a periodic extension, and windows control the leakage that results.
  • Space can be sampled too. An antenna array samples the wavefront at the element positions. A plane wave arriving at angle from broadside has a spatial frequency along the array, which lies in . Elements spaced apart sample space at rate and can distinguish spatial frequencies only within a band wide, so unambiguous coverage of all angles needs , that is . Wider spacing makes distinct directions look alike: these are the grating lobes of beamforming, the spatial version of aliasing.

Testing against SciPy

Each design function mirrors a SciPy function. For 45 cases, covering every IIR family and band type, order selection, window and equiripple FIR design, notch, peak and comb filters, frequency responses, filtering and convolution, the library’s results agree with SciPy 1.17.1:

Table 14.1. Filter design and filtering routines against SciPy.

SciPy function and caselargest difference ÷ largest coefficient
butter 4 lowpass 0.23.7e-16
butter 3 highpass 0.39.6e-17
butter 4 bandpass 0.2-0.58.5e-16
butter 2 bandstop 0.2-0.42.0e-16
cheby1 5 1 dB lowpass 0.31.9e-16
cheby1 4 0.5 dB bandpass 0.2-0.55.5e-16
cheby2 5 40 dB lowpass 0.32.4e-16
cheby2 4 30 dB highpass 0.41.5e-16
ellip 5 1/50 dB lowpass 0.31.6e-14
ellip 4 0.5/40 dB bandpass 0.2-0.56.1e-15
ellip 3 1/60 dB bandstop 0.2-0.65.7e-13
bessel 4 lowpass 0.2 (phase)5.0e-16
bessel 4 lowpass 0.2 (delay)8.2e-16
bessel 4 lowpass 0.2 (mag)7.4e-16
buttord lowpassorder 12 (SciPy 12), 0.0e0
buttord bandpassorder 11 (SciPy 11), 2.5e-18
buttord bandstoporder 11 (SciPy 11), 1.2e-7
cheb1ord lowpassorder 6 (SciPy 6), 0.0e0
cheb1ord bandpassorder 6 (SciPy 6), 0.0e0
cheb1ord bandstoporder 6 (SciPy 6), 2.2e-7
cheb2ord lowpassorder 6 (SciPy 6), 0.0e0
cheb2ord bandpassorder 6 (SciPy 6), 0.0e0
cheb2ord bandstoporder 6 (SciPy 6), 2.6e-7
ellipord lowpassorder 4 (SciPy 4), 0.0e0
ellipord bandpassorder 4 (SciPy 4), 0.0e0
ellipord bandstoporder 4 (SciPy 4), 3.2e-7
firwin 51 lowpass 0.3 hamming0.0e0
firwin 51 bandpass 0.2-0.51.8e-16
firwin 61 highpass 0.4 kaiser 65.6e-16
firwin2 511.4e-16
remez 51 lowpass7.1e-13
remez 41 bandpass weighted6.1e-15
remez 31 hilbert1.9e-13
remez 30 differentiator7.0e-13
iirnotch 0.25 Q 300.0e0
iirpeak 0.25 Q 300.0e0
iircomb 20 Hz Q 30 fs 200 notch0.0e0
bilinear 1/(s+1) fs 20.0e0
freqz butter 4, 8 points2.0e-15
freqs Butterworth prototype1.1e-16
lfilter ellip 4 on 64 samples9.0e-14
convolve full1.6e-16
convolve same1.6e-16
convolve valid0.0e0
correlate full1.6e-16
zpk2sos butter 8 lowpass 0.151.1e-16
zpk2sos ellip 5 1/50 dB lowpass 0.33.1e-13
zpk2sos cheby1 4 0.5 dB bandpass 0.2-0.52.8e-16
sosfilt_zi butter 8 lowpass 0.158.2e-16
sosfilt butter 8 lowpass 0.153.6e-15
sosfiltfilt butter 8 lowpass 0.155.5e-15
lfilter_zi butter 4 lowpass 0.23.8e-15
filtfilt butter 4 lowpass 0.2 (odd)6.3e-15
filtfilt butter 4 lowpass 0.2 (even)5.2e-15
filtfilt butter 4 lowpass 0.2 (constant, 20)5.6e-15

The band-stop critical frequencies differ by about because SciPy’s bounded minimiser stops within of the optimal edge, where the library’s search converges fully.

In the library

The crate is lib_jsl_dsp (API reference). Analogue designs are under continuous::filter; digital filters under discrete::filters (fir, iir, adaptive); convolution and correlation directly under discrete. Frequencies follow SciPy: digital edges in the units of fs, which defaults to 2 so that edges are fractions of Nyquist.

You needUseEntry
To apply an FIR filter, in one piece or as a stream; to find a delayconvolve, fft_convolve, OverlapAddFir, FftFir, cross_correlate, fft_correlation_peakConvolution and correlation
An analogue low-pass prototype or a band transformationbuttap, cheb1ap, cheb2ap, ellipap, besselap, lp2*_zpk, freqsAnalogue prototypes
A digital IIR filter meeting a specificationbuttord/butter, cheb1ord/cheby1, cheb2ord/cheby2, ellipord/ellip, bessel, freqzDigital IIR design
To run an IIR filter; to remove one tone or a harmonic seriesDiscreteLinearFilter, BiquadFilter, iirnotch, iirpeak, iircombIIR structures
A high-order IIR filter that stays stable; filtering without phase delayzpk2sos, tf2sos, sosfilt, sosfilt_zi, lfilter, lfilter_zi, filtfilt, sosfiltfiltSecond-order sections and zero-phase filtering
A linear-phase FIR filterfirwin, firwin2, remez, kaiser_beta, generate_derivative_tapsFIR design
A filter that learnsLmsFilter, RlsFilterAdaptive filters