# Digital Signal Processing/IIR Filter Design

IIR filters are typically designed basing on continuous-time filter functions. Once the transfer function has been chosen, different filter structures allow to implement the filter, be it in hardware or in software.

# Transfer function

The classical design technique is to start from a well known lowpass filter function and to transform it to a highpass, a bandpass or a bandstop function if required. Classical transfer functions are:
* Butterworth
* Chebyshev
* Elliptic or Cauer
The general form of the transfer function is given by the ratio of 2 polynoms:
    $H(z)={\frac {B(z)}{A(z)}}={\frac {b_{0}+b_{1}z^{-1}+b_{2}z^{-2}+\cdots +b_{M}z^{-M}}{1+a_{1}z^{-1}+a_{2}z^{-2}+\cdots +a_{N}z^{-N}}}$

# Filter structures

The most straightforward digital filter implementation would be done by implementing its difference equation which is given by:
    $y\left[k\right]=b_{0}x[k]+b_{1}x[k-1]+\cdots +b_{M}x[k-M]-a_{1}y[k-1]-a_{2}y[k-2]-\cdots -a_{N}y[k-N]$
The following circuit shows a possible implementation of the difference equation for a 4th order filter:
Nevertheless, this form is not used in practice. Indeed, the first who tried to implement a filter in that way have found out that this circuit is very sensitive to the coefficient values: a small change in one coefficient can have dramatic effects on the shape of the filter's transfer function. So rounding the coefficients to integer or fixed-point values resulted into a design nightmare. Because of this, engineers turned over to one of the following approaches:
* simulating analog filter structures which had shown not to suffer of this acute sensitivity
* breaking the total transfer function into a chain of second order sections, with an additional first order section for even-order filters

## All-Pole Filters

All-pole filters are limited to lowpass devices. The fact that they do not have zeros makes their structure much simpler. Indeed, their transfer function is written as:
       $H(z)={\frac {B(z)}{A(z)}}={\frac {1}{1+a_{1}z^{-1}+a_{2}z^{-2}+\cdots +a_{N-2}z^{-N-2}+a_{N-1}z^{-N-1}+a_{N}z^{-N}}}={\frac {1}{z^{N}+a_{1}z^{N-1}+a_{2}z^{N-2}+\cdots +a_{N-2}z^{-2}+a_{N-1}z+a_{N}}}$
Some filter structures based on the simulation of analog filters are used for the implementation of all-pole filters.
Butterworth and Chebyshev Type I functions are of all-pole kind.

### Chains of Integrators

A Chain of Integrators with Feedback (CIF) allows a straightforward implementation of an all-pole transfer function.
This circuit, with $U_{1}$ to $U_{4}$ being the multiplier ouputs, can be described by the following equations:
       ${\begin{cases}st_{1}\cdot U_{1}=U_{in}-U_{out}\\st_{2}\cdot U_{2}=U_{1}-U_{out}\\st_{3}\cdot U_{3}=U_{2}-U_{out}\\st_{4}\cdot U_{4}=U_{3}-U_{out}\\U_{out}=U_{4}\end{cases}}$
which can be iteratively solved as:
       $st_{4}\cdot U_{out}=U_{3}-U_{out}$
       $st_{3}st_{4}\cdot U_{out}=U_{2}-U_{out}-st_{3}\cdot U_{out}$
       $st_{2}st_{3}st_{4}\cdot U_{out}=U_{1}-U_{out}-st_{2}\cdot U_{out}-st_{2}st_{3}\cdot U_{out}$
       $st_{1}st_{2}st_{3}st_{4}\cdot U_{out}=U_{in}-U_{out}-st_{1}U_{out}-st_{1}st_{2}\cdot U_{out}-st_{1}st_{2}st_{3}\cdot U_{out}$
       $(s^{4}t_{1}t_{2}t_{3}t_{4}+s^{3}t_{1}t_{2}t_{3}+s^{2}t_{1}t_{2}+st_{1}+1)\cdot U_{out}=U_{in}$
       ${\frac {U_{out}}{U_{in}}}={\frac {1}{s^{4}t_{1}t_{2}t_{3}t_{4}+s^{3}t_{1}t_{2}t_{3}+s^{2}t_{1}t_{2}+st_{1}+1}}$
From this, the time constants can be iteratively retrieved from the transfer function's coefficients.
The digital structure corresponding to the analog CIF is given in the following figure:
If the sampling rate is much higher than the time constants, the filter coefficients can be obtained directly from the CIF in the analog domain. Else, they can be found from the sampled system transfer function:
       ${\frac {y}{x}}={\frac {1}{(z-1)^{4}c_{4}c_{3}c_{2}c_{1}+(z-1)^{3}c_{3}c_{2}c_{1}+(z-1)^{2}c_{2}c_{1}+(z-1)c_{1}+1}}$
with $c_{i}={\frac {t_{i}}{T}}$.

### Leapfrog Filters

A leapfrog filter simulates the functionality of an analog ladder filter and provides a robust implementation of an all-pole transfer function. It can be shown that a large variation of coefficient values only leads to small variations of the transfer function, especially in the passband.
Taking the example of the following 4th order all-pole lowpass ladder filter,
one can write the following equations:
       ${\begin{cases}sC_{1}\cdot U_{1}={\frac {1}{R}}\cdot (U_{in}-U_{1})-I_{2}\\sL_{2}\cdot I_{2}=U_{1}-U_{3}\\sC_{3}\cdot U_{3}=I_{2}-I_{4}\\sL_{4}\cdot I_{4}=U_{3}-R\cdot I_{4}\\U_{out}=R\cdot I_{4}\end{cases}}$
Rewriting these equations in order to have only voltages on the left side gives:
       ${\begin{cases}U_{1}={\frac {1}{sRC_{1}}}(U_{in}-U_{1}-RI_{2})\\RI_{2}={\frac {R}{sL_{2}}}(U_{1}-U_{3})\\U_{3}={\frac {1}{sRC_{3}}}(RI_{2}-RI_{4})\\RI_{4}={\frac {R}{sL_{4}}}(U_{3}-RI_{4})\\U_{out}=RI_{4}\end{cases}}$
This new set of equations shows that the filter can be implemented with a chain of 4 integrators with:
* state variables having the form of $w_{i}=U_{i}$ or $w_{i}=RI_{i}$
* time constants having the form of $t_{i}=RC_{i}$ or $t_{i}={\frac {L_{i}}{R}}$
The resulting circuit is an integrator chain where each integrator output is brought back to the preceeding integrator input. Hence the name leapfrog.
This analog device can be implemented as a digital circuit by replacing the analog integrators with accumulators:
The filter coefficients can be derived from tables providing the $R$, $C$ and $L$ values for the selected filter functions and from the sampling period $T$. An interesting point is that the relative values of the coefficients set the shape of the transfer function (Butterworth, Chebyshev, …), whereas their amplitudes set the cutoff frequency. Dividing all coefficients by a factor of two shifts the cutoff frequency down by one octave, which corresponds to a factor of two.
Let us note that the replacement of the analog integrators with accumulators corresponds is a very primitive design technique. In terms of signal processing, this corresponds to simplify the Z-transform to $z=1+sT$, which are the two first terms of the Taylor series of $z=exp(sT)$. Nevertheless, this approximation is good enough for filters where the sampling frequency is much higher than the signal bandwidth.
The state space representation of the preceeding filtre can be written as:
    ${\begin{array}{ccrcccccc}z\cdot {\begin{bmatrix}w_{1}\\w_{2}\\w_{3}\\w_{4}\end{bmatrix}}&=&{\begin{bmatrix}1-{\frac {T}{t_{1}}}&-{\frac {T}{t_{1}}}&0&0\\{\frac {T}{t_{2}}}&1&-{\frac {T}{t_{2}}}&0\\0&{\frac {T}{t_{3}}}&1&-{\frac {T}{t_{3}}}\\0&0&{\frac {T}{t_{4}}}&1-{\frac {T}{t_{4}}}\end{bmatrix}}&\cdot &{\begin{bmatrix}w_{1}\\w_{2}\\w_{3}\\w_{4}\end{bmatrix}}&+&{\begin{bmatrix}{\frac {T}{t_{1}}}\\0\\0\\0\end{bmatrix}}&\cdot &U_{in}\\U_{out}&=&{\begin{bmatrix}0&0&0&1\end{bmatrix}}&\cdot &{\begin{bmatrix}w_{1}\\w_{2}\\w_{3}\\w_{4}\end{bmatrix}}&+&{\begin{bmatrix}0\end{bmatrix}}&\cdot &U_{in}\end{array}}$
From this equation set, one can write the A, B, C, D matrices as:
    $A={\begin{bmatrix}1-{\frac {T}{t_{1}}}&-{\frac {T}{t_{1}}}&0&0\\{\frac {T}{t_{2}}}&1&-{\frac {T}{t_{2}}}&0\\0&{\frac {T}{t_{3}}}&1&-{\frac {T}{t_{3}}}\\0&0&{\frac {T}{t_{4}}}&1-{\frac {T}{t_{4}}}\end{bmatrix}}$
    $B={\begin{bmatrix}{\frac {T}{t_{1}}}\\0\\0\\0\end{bmatrix}}$
    $C={\begin{bmatrix}0&0&0&1\end{bmatrix}}$
    $D={\begin{bmatrix}0\end{bmatrix}}$
From this representation, signal processing tools such as SciPy, Octave or Matlab allow to plot the filter's frequency response or to examine its zeroes and poles.
A special case is the Butterworth 3rd order filter which has time constants with relative values of 1, 1/2 and 1. Due to that, this filter can be implemented in hardware without any multiplier, but using shifts instead.

## Filters with poles and zeroes

Filters with poles and zeroes are best implemented in the form of a chain of biquadratic cells or by the parallel connection of two all-pass filters.

### Chain of Second Order Sections

A second order section, often referred as biquad, implements a second order transfer function. The transfer function of a filter can be split into a product of transfer functions each associated to a pair of poles and possibly a pair of zeroes. If the transfer function's order is odd, then a first order section has to be added to the chain. This section is associated to the real pole and to the real zero if there is one.
The second order section's transfer function is given by:
    $H(z)={\frac {B(z)}{A(z)}}={\frac {b_{0}+b_{1}z^{-1}+b_{2}z^{-2}}{1+a_{1}z^{-1}+a_{2}z^{-2}}}$
The most known biquad structures are shown next.

#### Direct-form 1 biquad

The following figure shows a direct form 1 biquad:

#### Direct-form 1 transposed biquad

The following figure shows a direct form 1 transposed biquad:

#### Direct-form 2 biquad

The following figure shows a direct form 2 biquad:

#### Direct-form 2 transposed biquad

The following figure shows a direct form 2 transposed biquad:
Obviously, the corresponding first order sections are done by removing the multipliers for coefficients $a_{2}$ and $b_{2}$, together with the associated delay element(s).

### Parallel connection of allpass filters

Allpass filters are used to modify the phase of a signal's frequency components without altering their amplitude. They can be used to compensate for other undesired phase shifts, as for example in the design of close-to-linear phase IIR filters.
Additionally, the parallel connection of two allpass filters allows to implement any kind of transfer function. Indeed, at frequencies where the two branches show the same phase shift, the output will follow the input, but at the frequencies where the relative phase shift is 180°, the output will be zero. An example of this functionality is given in the family of Wave Digital Filters (WDFs).