# Signal Processing/Digital Filters
## Digital Filters

Digital filters are by essence sampled systems. The input and output signals are represented by samples with equal time distance.

## FIR Filters

Finite Impulse Response (FIR) filters are characterized by a time response depending only on a given number of the last samples of the input signal. In other terms: once the input signal has fallen to zero, the filter output will do the same after a given number of sampling periods.
The output $y(k)$ is given by a linear combination of the last input samples $x(k-i)$.
       $y(k)=\sum _{i=0}^{N-1}b(i)\cdot x(k-i)$
The coefficients $b(i)$ give the weight for the combination. They also correspond to the coefficients of the numerator of the z-domain filter transfer function.
The following figure shows an FIR filter of order $N$:
For linear phase filters, the coefficient values are symmetric around the middle one and the delay line can be folded back around this middle point in order to reduce the number of multiplications.
The transfer function of FIR filters only pocesses a numerator. This corresponds to an all-zero filter.
FIR filters typically require high orders, in the magnitude of several hundreds. Thus the choice of this kind of filters will need a great amount of hardware or CPU. Despite of this, one reason to choose a FIR filter implementation is the ability to achieve a linear phase response, which can be a requirement in some cases. Nevertheless, the filter designer has the possibility to choose IIR filters with a good phase linearity in the passband, such as Bessel filters, or to design an allpass filter to correct the phase response of a standard IIR filter.

### Average Filters

As its name states, an average filter (or Simple Moving Average filter, SMA filter) calculates the average of the $N$ last samples of a signal
       $y(k)={\frac {1}{N}}\cdot \sum _{i=0}^{N-1}x(k-i)$
It is the simplest form of an FIR filter, with all coefficients being equal.
The transfer function of an average filter is given by:
       ${\begin{aligned}H(z)&={\frac {1}{N}}\cdot \sum _{i=0}^{N-1}z^{-i}\\&={\frac {1}{N}}\cdot \left({\frac {1-z^{-N}}{1-z^{-1}}}\right)\end{aligned}}$
The transfer function of an average filter has $N$ equally spaced zeroes along the frequency axis. However, the zero at DC is masked by the pole of the filter. Hence, there is a larger lobe a DC which accounts for the filter passband.

#### Weighted moving average

Weighted moving average filters add a weight to the samples in order to give more importance to some of them
       $y(k)={\frac {\sum _{i=0}^{N-1}w(k-i)\cdot x(k-i)}{\sum _{i=0}^{N-1}w(k-i)}}$
The weights may vary linearly or exponentially.

#### Recursive implementation

The recursive implementation of a moving average filter bases on the fact that the output for 2 subsequent output samples is made out of the sum of the same set of input samples except for the first and the last one.
       $y(k)=y(k-1)+{\frac {1}{N}}\cdot [x(k)-x(k-(N-1))]$

#### Cascaded Integrator-Comb (CIC) implementation

A Cascaded integrator-comb filter (CIC) is a special technique for implementing average filters placed in series. The series placement of the average filters enhances the first lobe at DC compared to all other lobes.
A CIC filter implements the transfer function of $N$ average filters, each calculating the average of $RM$ samples. Its transfer function is thus given by:
       $H(z)=\left({\frac {1-z^{-R\cdot M}}{1-z^{-1}}}\right)^{N}$
CIC filters are used for decimating the number of samples of a signal by a factor of $R$ or, in others terms, to resample a signal at a lower frequency, throwing away $R-1$ samples out of $R$. The factor $M$ indicates how much of the first lobe is used by the signal. The number of average filter stages, $N$, indicates how well other frequency bands are damped, at the expense of a less flat transfer function around DC.
The CIC structure allows to implement the whole system with only adders and registers, not using any multipliers which are greedy in terms of hardware.
Downsampling by a factor of $R$ allows to increase the signal resolution by $\log _{2}(R)$ bits.

## IIR Filters
### Canonical filters

Canonical filters implement a filter transfer function with a number of delay elements equal to the filter order, one multiplier per numerator coefficient, one multiplier per denominator coefficient and a series of adders. Similarily to active filters canonical structures, this kind of circuits showed to be very sensitive to element values: a small change in a coefficients had a large effect on the transfer function.
Here too, the design of active filters has shifted from canonical filters to other structures such as chains of second order sections or leapfrog 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 most known filter biquad structures are
* direct-form 1
* direct-form 2
* direct-form 1 transposed
* direct-form 2 transposed
The direct-form 2 transposed of the following figure is especially interesting in terms of required hardware as well as signal and coefficient quantization.

### Digital Leapfrog Filters
#### Filter Structure

Digital leapfrog filters base on the simulation of analog active leapfrog filters. The incentive for this choice is to inherit from the excellent passband sensitivity properties of the original ladder circuit.
The following 4th order all-pole lowpass leapfrog filter
can be implemented as a digital circuit by replacing the analog integrators with accumulators.
Replacing the analog integrators with accumulators corresponds to simplify the Z-transform to $z=1+sT$, which are the two first terms of the Taylor series of $z=exp(sT)$. This approximation is good enough for filters where the sampling frequency is much higher than the signal bandwidth.

#### Transfer Function

The state space representation of the preceeding filter 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 Octave or Matlab allow to plot the filter's frequency response or to examine its zeroes and poles.
In the digital leapfrog filter, 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 (also a factor of two).
A special case is the Buterworth 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.

### Autoregressive Filters (AR)

Autoregressive (AR) models are process models in the form:
    $u(n)=\sum _{m=1}^{M}a^{*}(m)u(n-m)+x(n)$
Where u(n) is the output of the model, x(n) is the input of the model, and u(n - m) are previous samples of the model output value. These filters are called "autoregressive" because output values are calculated based on regressions of the previous output values. AR processes can be represented by an all-pole filter.

### ARMA Filters

Autoregressive Moving-Average (ARMA) filters are combinations of AR and MA filters. The output of the filter is given as a linear combination of both the weighted input and weighted output samples:
    $y(n)=\sum _{m=0}^{M}a^{*}(m)x(n-m)+\sum _{k=1}^{K}b^{*}(k)y(n-k)$
ARMA processes can be considered as a digital IIR filter, with both poles and zeros.

## Yule-Walker Equations

AR filters are preferred in many instances because they can be analyzed using the Yule-Walker equations. MA and ARMA processes, on the other hand, can be analyzed by complicated nonlinear equations which are difficult to study and model.
If we have an AR process with tap-weight coefficients a (a vector of a(n), a(n - 1), ...) an input of x(n), and an output of y(n), we can use the yule-walker equations. We say that σ$_x$$^2$ is the variance of the input signal. We treat the input data signal as a random signal, even if it is a deterministic signal, because we do not know what the value will be until we receive it. We can express the Yule-Walker equations as:
[Yule-Walker Equations]
    $\mathbf {R} \mathbf {a} =\mathbf {r}$
Where R is the cross-correlation matrix of the process output
    $\mathbf {R} =E[y(n)y^{H}(n)]$
And r is the autocorrelation matrix of the process output:
    $\mathbf {r} =E[y(n)y^{*}(n)]$

### Variance

We can show that:
    $r(0)=\sigma _{y}^{2}$
We can express the input signal variance as:
    $\sigma _{x}^{2}=\sum _{k=0}^{M}a(k)r(k)$
Or, expanding and substituting in for r(0), we can relate the output variance of the process to the input variance:
    $\sigma _{x}^{2}=\sigma _{y}^{2}+\sum _{k=1}^{M}a(k)r(k)$