# Signal Processing/Filter Design
## Filter design

Designing a filter generally starts with the specification of its frequency response. From this, both a transfer function and a filter structure have to be chosen. Indeed, some filter structures such as chains of second order sections are adaptable to any filter function whilst the basic ladder filter structure with only inductances on the series branches and capacitances on the parallel branches merely implement all-pole lowpass transfer functions. Mapping the transfer function to the filter structure gives the element values of analog filters elements and the coefficients of digital filters.
From the ideal filter, further optimizations have to be done.
The lumped element or the coefficient values are ideal values. Lumped element values have to be mapped to the ones of existing components. Filter coefficients have to be rounded to values supported by the number representation of the filter circuit. For example, choosing a fixed-point or a floating point Digital Signal Processor (DSP) will result in coarser or finer coefficient values.
Then, the signal amplitudes inside the filter have to be considered. For active filters and digital filters, the internal signals have to be checked against saturation or overflow. For any filter, the signals should not become too small, because this would seriously affect the signal to noise ratio of the whole filter. So basically, the filter design process doesn't only analyse the transfer function from the input to the output, but also the transfer function from the input to the internal signals.

## Filter representations

A filter can be represented in many equivalent ways:
* obviously by its transfer function
* by the zeroes and poles of the transfer function, together with a gain factor
* by the state-space representation of the filter
Methods allow to switch from one representation to the other. The different representations cast a specific light on the filter's properties. The transfer function, plotted for $s=j\omega$ shows how frequencies are amplified or damped. The zero-pole plot gives a direct information about the system stability. The state-space representation gives more insight into the chosen filter structure and allows to analyse the amplitudes of some signals (the state variables) inside the filter.

### Transfer Function

The most common way to choose a transfer function for simple filters such as lowpass, highpass, bandpass or bandstop is to select a usual filter function: Butterworth, Chebyshev, Elliptic, Bessel…
Realizable transfer functions are in the form of the quotient of two polynomials.
    $H(s)={\frac {num(s)}{den(s)}}$
The filter order is the maximal power of $s$ (or of $z$ for sampled systems) in the two polynomials.
A 4th order transfer function is in the form:
    $H(s)={\frac {b_{4}s^{4}+b_{3}s^{3}+b_{2}s^{2}+b_{1}s+b_{0}}{a_{4}s^{4}+a_{3}s^{3}+a_{2}s^{2}+a_{1}s+a_{0}}}$
A 4th order all-pole transfer function is in the form:
    $H(s)={\frac {1}{a_{4}s^{4}+a_{3}s^{3}+a_{2}s^{2}+a_{1}s+a_{0}}}$
A 4th order FIR transfer function is in the form:
    $H(z)={\frac {b_{4}z^{4}+b_{3}z^{3}+b_{2}z^{2}+b_{1}z+b_{0}}{1}}=b_{4}z^{4}+b_{3}z^{3}+b_{2}z^{2}+b_{1}z+b_{0}$
Most filter types try to get as close as possible to a brick-wall shape, with different optimization goals in mind. For these, the higher the filter order, the closer the function comes to the brick-wall shape. Butterworth filters do this by having a maximally flat (ripple-free) transfer function. Bessel filters, on the other hand, aim to minimize group delay variation in the passband.
These transfer functions are defined for normalized lowpass filters. Filter transformation techniques allow to transform the lowpass function to lowpass with a desired cutoff frequency, highpass, bandpass or bandstop.
Also, the functions stand for time-continuous systems, understood with the help of the Laplace analysis. For sampled systems, such as switched-capacitor or digital filters, a further step is needed: the $s$-based transfer function has to be transformed in a $z$-based transfer function.

### Zeroes, Poles and Gain

The zeroes are the roots of the transfer function numerator. The poles are the roots of the transfer function denominator. The gain is the DC value of the transfer function: the value for $s=0$.
The transfer function of a system given by its zeroes $z_{i}$, poles $p_{i}$ and gain $k$ is:
    $H(s)=k\cdot {\frac {\prod _{i=1}^{M}(s-z_{i})}{\prod _{i=1}^{N}(s-p_{i})}}$
A stable LTI system has all its poles on the left side half-plane of $s$ (i.e., will have negative real parts).

### State Space Representation

The state space representation describes the filter as a set of differential equations:
    ${\dot {\mathbf {x} }}(t)=A\cdot \mathbf {x} (t)+B\cdot u(t)$
    $y(t)=C\cdot \mathbf {x} (t)+D\cdot u(t)$
Here, the input is $u(t)$, the internal filter signals are $\mathbf {x} (t)$ and the output is $y(t)$. The Laplace transform of these equations is given by:
    $s\cdot \mathbf {X} (s)=A\cdot \mathbf {X} (s)+B\cdot U(s)$
    $Y(s)=C\cdot \mathbf {X} (s)+D\cdot U(s)$
The transfer function is given by:
    $H(s)={\frac {Y(s)}{U(s)}}=C(s\mathbf {I} -A)^{-1}B+D$
The eigenvalues of $A$ are the poles of the system. The characteristic polynomial of $A$ is the denominator of the transfer function.

## Examples
### 4th Order Normalized Butterworth Filter

Contrarily to other filter functions given by polynomials, the Butterworth filter is best described by the location of its poles. It is an all-pole function: it has no zeroes.

#### Poles

The normalized Butterworth filter has equally spaced poles on the left half plane part of the unit circle. For a 4th order filter, their values are:
    ${\begin{cases}p_{1},p_{1}^{*}=e^{\pm i(\pi /2+\pi /8)}\\p_{2},p_{2}^{*}=e^{\pm i(\pi /2+3\pi /8)}\end{cases}}or{\begin{cases}p_{1},p_{1}^{*}=-0.383\pm 0.924\cdot i\\p_{2},p_{2}^{*}=-0.924\pm 0.383\cdot i\end{cases}}$

#### Transfer Function

The transfer function is thus:
    $H(s)={\frac {1}{(s-p_{1})\cdot (s-p_{1}^{*})\cdot (s-p_{2})\cdot (s-p_{2}^{*})}}$
    $H(s)={\frac {1}{(s^{2}-2\cdot re(p_{1})\cdot s+1)\cdot (s^{2}-2\cdot re(p_{2})\cdot s+1)}}$
    $H(s)={\frac {1}{(s^{2}+0.765s+1)\cdot (s^{2}+1.848s+1)}}$
    $H(s)={\frac {1}{s^{4}+2.61s^{3}+3.41s^{2}+2.61s+1}}$

#### State Space Representation

A possible state space representation is found from the transfer function:
    $Y(s)=U(s){\frac {1}{s^{4}+2.61s^{3}+3.41s^{2}+2.61s+1}}$
    $s^{4}\cdot Y(s)+2.61s^{3}\cdot Y(s)+3.41s^{2}\cdot Y(s)+2.61s\cdot Y(s)+Y(s)=U(s)$
    $s\cdot s^{3}\cdot Y(s)=-Y(s)-2.61s\cdot Y(s)-3.41s^{2}\cdot Y(s)-2.61s^{3}\cdot Y(s)+U(s)$
Writing:
    ${\begin{cases}X_{1}(s)=Y(s)\\X_{2}(s)=s\cdot Y(s)=s\cdot X_{1}(s)\\X_{3}(s)=s^{2}\cdot Y(s)=s\cdot X_{2}(s)\\X_{4}(s)=s^{3}\cdot Y(s)=s\cdot X_{3}(s)\\\end{cases}}$
gives:
    $s\cdot X_{4}(s)=-X_{1}(s)-2.61\cdot X_{2}(s)-3.41\cdot X_{3}(s)-2.61\cdot X_{4}(s)+U(s)$
This can be rewritten in matrix form as:
    ${\begin{matrix}s\cdot {\begin{bmatrix}X_{1}\\X_{2}\\X_{3}\\X_{4}\end{bmatrix}}&=&{\begin{bmatrix}0&1&0&0\\0&0&1&0\\0&0&0&1\\-1&-2.61&-3.41&-2.61\end{bmatrix}}{\begin{bmatrix}X_{1}\\X_{2}\\X_{3}\\X_{4}\end{bmatrix}}&+&{\begin{bmatrix}0\\0\\0\\1\end{bmatrix}}U\\Y&=&{\begin{bmatrix}1&0&0&0\end{bmatrix}}{\begin{bmatrix}X_{1}\\X_{2}\\X_{3}\\X_{4}\end{bmatrix}}&+&{\begin{bmatrix}0\end{bmatrix}}U\end{matrix}}$

#### Octave Code

The following Octave code provides the same results and plots the amplitude response of the filter:

### Analog Lowpass Filter Design
#### Specification

A reduced version of CCITT G712 input filter specification, giving only the lowpass part, is shown in the plot on the side.
The passband goes up to 3 kHz and allows a maximal ripple of 0.125 dB. The stopband requires an attenuation of 14 dB at 4 kHz and an attenuation of 32 dB above 4.6 kHz.

#### Filter order

A first step is to evaluate the order needed for usual filter functions. As there is no specification on the phase, there is no need to consider a Bessel filter.
The following Octave code:
gives us the results:
The Butterworth filter needs roughly twice as much hardware as the Chebyshev type 1. The Chebyshev type 2 and the elliptic filter have zeroes which the Chebyshev type 1 filter doesn't, as it is an all-pole function. So they too will need more hardware than the Chebyshev type 1.
Chebyshev type 1 will be the candidate chosen for this filter.

#### Transfer function

Octave does the frequency transforms automatically. The amplitude response is obtained with:
This gives the following plot:
As one might have noticed, the filter order has been pushed up to 7 and the cutoff frequency has been pushed a little higher in order to balance the distance to the specification borders on the passband and the stopband side.

#### Filter coefficients

Obviously, the filter coefficients depend on the chosen filter structure.
For a chain of second order sections, the higher order transfer function has to be split into the product of second order transfer functions:
This gives:
The "1, 0, 0" beginning of the SOS matrix lines shows that they are all-pole functions.
The last SOS matrix line with values 0 at the third and at the last positions shows that it corresponds to a first order section. Indeed, the 7th order function is made out of 3 second order sections and one first order section.
The imaginary part of the gain is to be ignored: it is certainly an error of fixed point calculus. The real part of the gain is equal to the product of the last (non-zero) elements of the SOS matrix lines.
From these coefficients, one can find the analog element values of state variable filters or Sallen-Key circuits which both are of all-pole kind. The coefficients can also used almost as such in the canonical biquads, but with $b_{1}=0$ and $b_{2}=0$, which simplifies the circuits.