Main Content

IIR Filter Design

This example compares classical Butterworth, Chebyshev, and elliptic designs; and explores Bessel, Yule-Walker, and generalized Butterworth filters.

IIR vs. FIR Filters

The primary advantage of IIR filters over FIR filters is that they typically meet a given set of specifications with a much lower filter order than a corresponding FIR filter. Although IIR filters have nonlinear phase, data processing within MATLAB® software is commonly performed “offline,” that is, the entire data sequence is available prior to filtering. This allows for a noncausal, zero-phase filtering approach (via the filtfilt function), which eliminates the nonlinear phase distortion of an IIR filter.

Classical IIR Filters

The classical IIR filters, Butterworth, Chebyshev Types I and II, elliptic, and Bessel, all approximate the ideal “brick wall” filter in different ways.

Signal Processing Toolbox™ provides functions to create all these types of classical IIR filters in both the analog and digital domains (except Bessel, for which only the analog case is supported), and in lowpass, highpass, bandpass, and bandstop configurations. For most filter types, you can also find the lowest filter order that fits a given filter specification in terms of passband and stopband attenuation, and transition widths.

Other IIR Filters

The direct filter design function yulewalk finds a filter with magnitude response approximating a specified frequency-response function. This is one way to create a multiband bandpass filter.

You can also use the parametric modeling or system identification functions to design IIR filters. These functions are discussed in Parametric Modeling.

The generalized Butterworth design function maxflat is discussed in the section Generalized Butterworth Filter Design.

IIR Filter Method Summary

The following table summarizes the various filter methods using Signal Processing Toolbox™ and lists the functions available to implement these methods.

Toolbox Filters Methods and Available Functions

Filter Method

Description

Filter Functions

Analog Prototyping

Using the poles and zeros of a classical lowpass prototype filter in the continuous (Laplace) domain, obtain a digital filter through frequency transformation and filter discretization.

Filter design:

besself, butter, cheby1, cheby2, ellip

Order estimation: buttord, cheb1ord, cheb2ord, ellipord

Lowpass analog prototyping: besselap, buttap, cheb1ap, cheb2ap, ellipap

Frequency transformation: lp2bp, lp2bs, lp2hp, lp2lp

Filter discretization: bilinear, impinvar

Direct Design

Design digital filter directly in the discrete time-domain by approximating a piecewise linear magnitude response.

yulewalk

Generalized Butterworth Design

Design lowpass Butterworth filters with more zeros than poles.

maxflat

Parametric Modeling

Find a digital filter that approximates a prescribed time or frequency domain response. (See System Identification Toolbox™ documentation for an extensive collection of parametric modeling tools.)

Time-domain modeling:

lpc, prony, stmcb

Frequency-domain modeling: invfreqs, invfreqz

Classical IIR Filter Design Using Analog Prototyping

The principal IIR digital filter design technique this toolbox provides is based on the conversion of classical lowpass analog filters to their digital equivalents. The following sections describe how to design filters and summarize the characteristics of the supported filter types. See Special Topics in IIR Filter Design for detailed steps on the filter design process.

Complete Classical IIR Filter Design

You can easily create a filter of any order with a lowpass, highpass, bandpass, or bandstop ftype configuration using the filter design functions.

Filter Design Functions

Filter Type

Design Function

Bessel (besself, analog only)

[b,a] = besself(n,Wn,ftype)

[z,p,k] = besself(n,Wn,ftype)

[A,B,C,D] = besself(n,Wn,ftype)

Butterworth (butter)

[b,a] = butter(n,Wn,ftype)

[z,p,k] = butter(n,Wn,ftype)

[A,B,C,D] = butter(n,Wn,ftype)

Chebyshev Type I (cheby1)

[b,a] = cheby1(n,Rp,Wn,ftype)

[z,p,k] = cheby1(n,Rp,Wn,ftype)

[A,B,C,D] = cheby1(n,Rp,Wn,ftype)

Chebyshev Type II (cheby2)

[b,a] = cheby2(n,Rs,Wn,ftype)

[z,p,k] = cheby2(n,Rs,Wn,ftype)

[A,B,C,D] = cheby2(n,Rs,Wn,ftype)

Elliptic (ellip)

[b,a] = ellip(n,Rp,Rs,Wn,ftype)

[z,p,k] = ellip(n,Rp,Rs,Wn,ftype)

[A,B,C,D] = ellip(n,Rp,Rs,Wn,ftype)

By default, each of these functions returns a lowpass filter; you need to specify only the cutoff frequency that you want, Wn, in normalized units such that the Nyquist frequency is 1 Hz). For a highpass filter, append "high" to the function's parameter list. For a bandpass or bandstop filter, specify Wn as a two-element vector containing the passband edge frequencies. Append "stop" for the bandstop configuration.

Here are some example digital filters:

[bB,aB] = butter(5,0.4);                  % Lowpass Butterworth
[bC1,aC1] = cheby1(4,1,[0.4 0.7]);        % Bandpass Chebyshev Type I
[bC2,aC2] = cheby2(6,60,0.8,"high");      % Highpass Chebyshev Type II
[bE,aE] = ellip(3,1,60,[0.4 0.7],"stop"); % Bandstop elliptic

To design an analog filter, perhaps for simulation, use a trailing "s" and specify cutoff frequencies in rad/s:

[bBA,aBA] = butter(5,0.4,"s");    % Analog Butterworth filter

All filter design functions return a filter in the transfer function, zero-pole-gain, or state-space linear system model representation, depending on how many output arguments are present. In general, you should avoid using the transfer function form because numerical problems caused by round-off errors can occur. Instead, use the zero-pole-gain form which you can convert to a second-order section (SOS) form using zp2sos and then use the SOS form to analyze or implement your filter.

All classical IIR lowpass filters are ill-conditioned for extremely low cutoff frequencies. Therefore, instead of designing a lowpass IIR filter with a very narrow passband, it can be better to design a wider passband and decimate the input signal.

Designing IIR Filters to Frequency Domain Specifications

Signal Processing Toolbox™ provides order selection functions that calculate the minimum filter order that meets a given set of requirements.

Filter Type

Order Estimation Function

Butterworth

[n,Wn] = buttord(Wp,Ws,Rp,Rs)

Chebyshev Type I

[n,Wn] = cheb1ord(Wp,Ws,Rp,Rs)

Chebyshev Type II

[n,Wn] = cheb2ord(Wp,Ws,Rp,Rs)

Elliptic

[n,Wn] = ellipord(Wp,Ws,Rp,Rs)

These order estimation functions are useful in conjunction with the filter design functions. Suppose you want a bandpass filter with a passband from 1000 to 2000 Hz, stopbands starting 500 Hz away on either side, a 10 kHz sampling frequency, at most 1 dB of passband ripple, and at least 60 dB of stopband attenuation. You can meet these specifications by using the butter function as follows.

[n,Wn] = buttord([1000 2000]/5000,[500 2500]/5000,1,60)
n = 
12
Wn = 1×2

    0.1951    0.4080

[bB2,aB2] = butter(n,Wn);

An elliptic filter that meets the same requirements is given by

[n,Wn] = ellipord([1000 2000]/5000,[500 2500]/5000,1,60)
n = 
5
Wn = 1×2

    0.2000    0.4000

[bE2,aE2] = ellip(n,1,60,Wn);

These functions also work with the other standard band configurations, as well as for analog filters.

Comparison of Classical IIR Filter Types

Signal Processing Toolbox™ provides five different types of classical IIR filters, each optimal in some way. This section shows the basic analog prototype form for each and summarizes major characteristics.

Butterworth Filter

The Butterworth filter provides the best Taylor series approximation to the ideal lowpass filter response at analog frequencies Ω=0 and Ω=; for any order N, the magnitude squared response has 2N1 zero derivatives at these locations (maximally flat at Ω=0 and Ω=). Response is monotonic overall, decreasing smoothly from Ω=0 to Ω=. |H(jΩ)|=1/2 at Ω=1.

n = 5;
[z,p,k] = buttap(n);
[bBp,aBp] = zp2tf(z,p,k);
[hBp,wBp] = freqs(bBp,aBp,4096);
semilogx(wBp,abs(hBp))
grid on
xlabel("Frequency (rad/s)")
ylabel("Magnitude")

Figure contains an axes object. The axes object with xlabel Frequency (rad/s), ylabel Magnitude contains an object of type line.

Chebyshev Type I Filter

The Chebyshev Type I filter minimizes the absolute difference between the ideal and actual frequency response over the entire passband by incorporating an equal ripple of Rp dB in the passband. Stopband response is maximally flat. The transition from passband to stopband is more rapid than for the Butterworth filter. |H(jΩ)|=10-Rp/20 at Ω=1.

n = 5;
Rp = 0.5;
[z,p,k] = cheb1ap(n,Rp);
[bC1p,aC1p] = zp2tf(z,p,k);
[hC1p,wC1p] = freqs(bC1p,aC1p,4096);
semilogx(wC1p,abs(hC1p))
grid on
xlabel("Frequency (rad/s)")
ylabel("Magnitude")

Figure contains an axes object. The axes object with xlabel Frequency (rad/s), ylabel Magnitude contains an object of type line.

Chebyshev Type II Filter

The Chebyshev Type II filter minimizes the absolute difference between the ideal and actual frequency response over the entire stopband by incorporating an equal ripple of Rs dB in the stopband. Passband response is maximally flat.

The stopband does not approach zero as quickly as the type I filter (and does not approach zero at all for even-valued filter order n). The absence of ripple in the passband, however, is often an important advantage. |H(jΩ)|=10-Rs/20 at Ω=1.

n = 5;
Rs = 20;
[z,p,k] = cheb2ap(n,Rs);
[bC2p,aC2p] = zp2tf(z,p,k);
[hC2p,wC2p] = freqs(bC2p,aC2p,4096);
semilogx(wC2p,abs(hC2p))
grid on
xlabel("Frequency (rad/s)")
ylabel("Magnitude")

Figure contains an axes object. The axes object with xlabel Frequency (rad/s), ylabel Magnitude contains an object of type line.

Elliptic Filter

Elliptic filters are equiripple in both the passband and stopband. They generally meet filter requirements with the lowest order of any supported filter type. Given a filter order n, passband ripple Rp in decibels, and stopband ripple Rs in decibels, elliptic filters minimize transition width. |H(jΩ)|=10-Rp/20 at Ω=1.

n = 5;
Rp = 0.5;
Rs = 20;
[z,p,k] = ellipap(n,Rp,Rs);
[bEp,aEp] = zp2tf(z,p,k);
[hEp,wEp] = freqs(bEp,aEp,4096);
semilogx(wEp,abs(hEp))
grid on
xlabel("Frequency (rad/s)")
ylabel("Magnitude")

Figure contains an axes object. The axes object with xlabel Frequency (rad/s), ylabel Magnitude contains an object of type line.

Bessel Filter

Analog Bessel lowpass filters have maximally flat group delay at zero frequency and retain nearly constant group delay across the entire passband. Filtered signals therefore maintain their waveshapes in the passband frequency range. When an analog Bessel lowpass filter is converted to a digital one through frequency mapping, it no longer has this maximally flat property. Signal Processing Toolbox™ supports only the analog case for the complete Bessel filter design function.

Bessel filters generally require a higher filter order than other filters for satisfactory stopband attenuation. |H(jΩ)|<1/2 at Ω=1 and decreases as filter order n increases.

n = 5;
[z,p,k] = besselap(n);
[bBsp,aBsp] = zp2tf(z,p,k);
[hBsp,wBsp] = freqs(bBsp,aBsp,4096);
semilogx(wBsp,abs(hBsp))
grid on
xlabel("Frequency (rad/s)")
ylabel("Magnitude")

Figure contains an axes object. The axes object with xlabel Frequency (rad/s), ylabel Magnitude contains an object of type line.

NOTE: The lowpass filters shown above were created with the analog prototype functions besselap, buttap, cheb1ap, cheb2ap, and ellipap. These functions find the zeros, poles, and gain of an nth-order analog filter of the appropriate type with a cutoff frequency of 1 rad/s. The complete filter design functions (besself, butter, cheby1, cheby2, and ellip) call the prototyping functions as a first step in the design process. See Special Topics in IIR Filter Design for details.

Direct IIR Filter Design

This toolbox uses the term direct methods to describe techniques for IIR design that find a filter based on specifications in the discrete domain. Unlike the analog prototyping method, direct design methods are not constrained to the standard lowpass, highpass, bandpass, or bandstop configurations. Rather, these functions design filters with an arbitrary, perhaps multiband, frequency response. This section discusses the yulewalk function, which is intended specifically for filter design; Parametric Modeling discusses other methods that may also be considered direct, such as Prony's method, Linear Prediction, the Steiglitz-McBride method, and inverse frequency design.

The yulewalk function designs recursive IIR digital filters by fitting a specified frequency response. yulewalk's name reflects its method for finding the filter's denominator coefficients: it finds the inverse FFT of the ideal specified magnitude-squared response and solves the modified Yule-Walker equations using the resulting autocorrelation function samples. The statement [b,a] = yulewalk(n,f,m) returns row vectors b and a containing the n+1 numerator and denominator coefficients of the nth-order IIR filter whose frequency-magnitude characteristics approximate those given in vectors f and m. f is a vector of frequency points ranging from 0 to 1, where 1 represents the Nyquist frequency. m is a vector containing the specified magnitude response at the points in f. f and m can describe any piecewise linear shape magnitude response, including a multiband response. The FIR counterpart of this function is fir2, which also designs a filter based on an arbitrary piecewise linear magnitude response. See FIR Filter Design for details.

Note that yulewalk does not accept phase information, and no statements are made about the optimality of the resulting filter.

Design a multiband filter with yulewalk and plot the specified and actual frequency response:

m = [0   0   1   1   0   0   1   1   0 0];
f = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 1];
[bY,aY] = yulewalk(10,f,m);
freqz(bY,aY,128)

Figure contains 2 axes objects. Axes object 1 with title Phase, xlabel Normalized Frequency (\times\pi rad/sample), ylabel Phase (degrees) contains an object of type line. Axes object 2 with title Magnitude, xlabel Normalized Frequency (\times\pi rad/sample), ylabel Magnitude (dB) contains an object of type line.

Generalized Butterworth Filter Design

The maxflat function enables you to design generalized Butterworth filters, that is, Butterworth filters with differing numbers of zeros and poles. This is desirable in some implementations where poles are more expensive computationally than zeros. maxflat is just like the butter function, except that it you can specify two orders (one for the numerator and one for the denominator) instead of just one. These filters are maximally flat. This means that the resulting filter is optimal for any numerator and denominator orders, with the maximum number of derivatives at 0 and the Nyquist frequency ω = π both set to 0.

For example, when the two orders are the same, maxflat is the same as butter:

[bM0,aM0] = maxflat(3,3,0.25)
bM0 = 1×4

    0.0317    0.0951    0.0951    0.0317

aM0 = 1×4

    1.0000   -1.4590    0.9104   -0.1978

[bB0,aB0] = butter(3,0.25)
bB0 = 1×4

    0.0317    0.0951    0.0951    0.0317

aB0 = 1×4

    1.0000   -1.4590    0.9104   -0.1978

However, maxflat is more versatile because it allows you to design a filter with more zeros than poles:

[bM1,aM1] = maxflat(3,1,0.25)
bM1 = 1×4

    0.0950    0.2849    0.2849    0.0950

aM1 = 1×2

    1.0000   -0.2402

The third input to maxflat is the half-power frequency, a frequency between 0 and 1 with a magnitude response of 1/√2.

You can also design linear phase filters that have the maximally flat property using the "sym" option:

bM2 = maxflat(4,"sym",0.3)
bM2 = 1×5

    0.0331    0.2500    0.4337    0.2500    0.0331

Comparing the filter responses:

filterAnalyzer(bM0,aM0,bM1,aM1,bM2,1, ...
    FilterNames=["SameOrder" "DiffOrder" "Symmetric"])

For complete details of the maxflat algorithm, see Selesnick and Burrus [3].