# Classic IIR Filter Design

This example shows how to design classic IIR filters. The example initially focuses on the scenario where critical design parameter is the cutoff frequency at which the power of the filter decays to half (–3 dB) the nominal passband value.

The example then shows you how to replace a Butterworth design with a Chebyshev filter or an elliptic filter of the same order and obtain a steeper rolloff at the expense of some ripple in the passband and stopband of the filter. Finally, the example explores minimum-order filter designs.

### Lowpass Filters

Design an 8th order filter with a normalized cutoff frequency of 0.4𝜋. To begin, design a Butterworth filter which is maximally flat (no ripple in the passband or in the stopband).

```N = 8; F3dB = .4; d = fdesign.lowpass("N,F3dB",N,F3dB); Hbutter = design(d,"butter",SystemObject=true)```
```Hbutter = dsp.SOSFilter with properties: Structure: 'Direct form II' CoefficientSource: 'Property' Numerator: [4x3 double] Denominator: [4x3 double] HasScaleValues: true ScaleValues: [0.2914 0.2261 0.1929 0.1788 1] Use get to show all properties ```

A Chebyshev Type I filter has ripples in the passband and no ripples in the stopband. A Chebyshev Type I design allows you to control the passband ripples. Larger passband ripples result with a steeper rolloff.

Design a Chebyshev Type I filter with peak-to-peak ripples of 0.5 dB:

```Ap = .5; setspecs(d,"N,F3dB,Ap",N,F3dB,Ap); Hcheby1 = design(d,"cheby1",SystemObject=true); hfvt = fvtool(Hbutter,Hcheby1); legend(hfvt,"Butterworth","Chebyshev Type I");```

A Chebyshev Type II filter has ripples in the stopband, and no ripples in the passband. A Chebyshev Type II design allows you to control the stopband attenuation. A smaller stopband attenuation results with a steeper rolloff.

Design a Chebyshev Type II filter with a stopband attenuation of 80 dB:

```Ast = 80; setspecs(d,"N,F3dB,Ast",N,F3dB,Ast); Hcheby2 = design(d,"cheby2",SystemObject=true); hfvt = fvtool(Hbutter,Hcheby2); legend(hfvt,"Butterworth","Chebyshev Type II");```

Finally, an elliptic filter can provide a steeper rolloff compared to the previous designs by allowing ripples both in the stopband and the passband. To illustrate that, design an elliptic filter using the same passband and stopband characteristic as the previous steps.

```setspecs(d,"N,F3dB,Ap,Ast",N,F3dB,Ap,Ast); Hellip = design(d,"ellip",SystemObject=true); hfvt = fvtool(Hbutter,Hcheby1,Hcheby2,Hellip); legend(hfvt, ... "Butterworth","Chebyshev Type I","Chebyshev Type II","Elliptic");```

Zoom in on the passband to verify that all filters have the same –3dB frequency point and that only the Butterworth and the Chebyshev Type II designs have a perfectly flat passband.

### Phase Consideration

If phase response is a design concern, it is useful to remember that Butterworth and Chebyshev Type II introduce less distortion (their group delay is flatter). Verify that by comparing the group delay responses of the four filters designed in the previous steps.

`hfvt.Analysis = "grpdelay";`

### Minimum-Order Designs

Minimum order designs apply when the passband, stopband, and the magnitude of tolerable ripples are fully specified. In these cases, the cutoff frequency of 3dB is not of primary interest. The smallest filter order required to meet the design specification is automatically derived by the design algorithm.

Design a minimum-order IIR filter.

```Fp = .1; Fst = .3; Ap = 1; Ast = 60; setspecs(d,"Fp,Fst,Ap,Ast",Fp,Fst,Ap,Ast); Hbutter = design(d,"butter",SystemObject=true); Hcheby1 = design(d,"cheby1",SystemObject=true); Hcheby2 = design(d,"cheby2",SystemObject=true); Hellip = design(d,"ellip",SystemObject=true); hfvt = fvtool(Hbutter,Hcheby1,Hcheby2,Hellip,DesignMask="on"); legend(hfvt, ... "Butterworth","Chebyshev Type I","Chebyshev Type II","Elliptic");```

A 7th order filter is necessary to meet the specification in the Butterworth design, whereas a 5th order filter is sufficient with the two Chebyshev techniques. Elliptic design reduces the required filter order even further to 4.

`order(Hbutter)`
```ans = 7 ```
`order(Hcheby1)`
```ans = 5 ```
`order(Hcheby2)`
```ans = 5 ```
`order(Hellip)`
```ans = 4 ```

### Exactly Match the Passband or Stopband Specifications

In a minimum-order designs, the ideal order that meets a given specification may be a fractional number. Rounding up to the next integer might exceed the specifications due to the additional fractional order.

Use the `MatchExactly` option to constraint the design algorithm to exactly match the specifications in at least one of the two bands. By default, Chebyshev Type I designs match the passband and exceeds in the stopband. Butterworth and Chebyshev Type II match the stopband and exceed in the passband. Elliptic designs match the target attenuations in both bands, while the stopband edge frequency is exceeded.

```Hellipmin1 = design(d,"ellip",MatchExactly="passband",SystemObject=true); Hellipmin2 = design(d,"ellip",MatchExactly="stopband",SystemObject=true); hfvt = fvtool(Hellip, Hellipmin1, Hellipmin2,DesignMask="on"); legend(hfvt,"Matched passband and stopband", ... "Matched passband", "Matched stopband", ... Location="Northeast")```

Zoom in on the passband to compare the passband edges. The matched passband in both designs have an attenuation of exactly 1 dB at the passband edge frequency 0.1𝝅 rad/samples.

Verify that the resulting order of the filters did not change.

`order(Hellip)`
```ans = 4 ```
`order(Hellipmin1)`
```ans = 4 ```
`order(Hellipmin2)`
```ans = 4 ```

### Highpass, Bandpass, and Bandstop Filters

You can generalize the results in the previous steps to highpass, bandpass, and bandstop response types. For example, design three minimum order bandpass filters.

```d = fdesign.bandpass("Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2", ... .35,.45,.55,.65,60,1,60);```
```Hbutter = design(d,"butter",SystemObject=true); Hcheby1 = design(d,"cheby1",SystemObject=true); Hcheby2 = design(d,"cheby2",SystemObject=true); Hellip = design(d,"ellip",SystemObject=true); hfvt = fvtool(Hbutter,Hcheby1,Hcheby2,Hellip,DesignMask="on"); legend(hfvt,... "Butterworth","Chebyshev Type I","Chebyshev Type II","Elliptic",... Location="Northwest")```