## Filter Implementation

### Convolution and Filtering

The mathematical foundation of filtering is convolution. For a finite impulse response (FIR) filter, the output y(k) of a filtering operation is the convolution of the input signal x(k) with the impulse response h(k):

$y\left(k\right)=\sum _{l=-\infty }^{\infty }h\left(l\right)\text{\hspace{0.17em}}x\left(k-l\right).$

If the input signal is also of finite length, you can implement the filtering operation using the MATLAB® conv function. For example, to filter a five-sample random vector with a third-order averaging filter, you can store x(k) in a vector x, h(k) in a vector h, and convolve the two:

x = randn(5,1);
h = [1 1 1 1]/4;   % A third-order filter has length 4
y = conv(h,x)
y =
-0.3375
0.4213
0.6026
0.5868
1.1030
0.3443
0.1629
0.1787
The length of y is one less than the sum of the lengths of x and h.

### Filters and Transfer Functions

The transfer function of a filter is the Z-transform of its impulse response. For an FIR filter, the Z-transform of the output y, Y(z), is the product of the transfer function and X(z), the Z-transform of the input x:

$Y\left(z\right)=H\left(z\right)X\left(z\right)=\left(h\left(1\right)+h\left(2\right){z}^{-1}+\cdots +h\left(n+1\right){z}^{-n}\right)X\left(z\right).$

The polynomial coefficients h(1), h(2), …, h(n + 1) correspond to the coefficients of the impulse response of an nth-order filter.

Note

The filter coefficient indices run from 1 to (n + 1), rather than from 0 to n. This reflects the standard indexing scheme used for MATLAB vectors.

FIR filters are also called all-zero, nonrecursive, or moving-average (MA) filters.

For an infinite impulse response (IIR) filter, the transfer function is not a polynomial, but a rational function. The Z-transforms of the input and output signals are related by

$Y\left(z\right)=H\left(z\right)X\left(z\right)=\frac{b\left(1\right)+b\left(2\right){z}^{-1}+...+b\left(n+1\right){z}^{-n}}{a\left(1\right)+a\left(2\right){z}^{-1}+...+a\left(m+1\right){z}^{-m}}X\left(z\right),$

where b(i) and a(i) are the filter coefficients. In this case, the order of the filter is the maximum of n and m. IIR filters with n = 0 are also called all-pole, recursive, or autoregressive (AR) filters. IIR filters with both n and m greater than zero are also called pole-zero, recursive, or autoregressive moving-average (ARMA) filters. The acronyms AR, MA, and ARMA are usually applied to filters associated with filtered stochastic processes.

### Filtering with the filter Function

For IIR filters, the filtering operation is described not by a simple convolution, but by a difference equation that can be found from the transfer-function relation. Assume that a(1) = 1, move the denominator to the left side, and take the inverse Z-transform to obtain

$y\left(k\right)+a\left(2\right)\text{\hspace{0.17em}}y\left(k-1\right)+\dots +a\left(m+1\right)\text{\hspace{0.17em}}y\left(k-m\right)=b\left(1\right)\text{\hspace{0.17em}}x\left(k\right)+b\left(2\right)\text{\hspace{0.17em}}x\left(k-1\right)+\cdots +b\left(n+1\right)\text{\hspace{0.17em}}x\left(k-n\right).$

In terms of current and past inputs, and past outputs, y(k) is

$y\left(k\right)=b\left(1\right)\text{\hspace{0.17em}}x\left(k\right)+b\left(2\right)\text{\hspace{0.17em}}x\left(k-1\right)+\cdots +b\left(n+1\right)\text{\hspace{0.17em}}x\left(k-n\right)-a\left(2\right)\text{\hspace{0.17em}}y\left(k-1\right)-\cdots -a\left(m+1\right)\text{\hspace{0.17em}}y\left(k-m\right),$

which is the standard time-domain representation of a digital filter. Starting with y(1) and assuming a causal system with zero initial conditions, the representation is equivalent to

$\begin{array}{l}y\left(1\right)=b\left(1\right)\text{\hspace{0.17em}}x\left(1\right)\\ y\left(2\right)=b\left(1\right)\text{\hspace{0.17em}}x\left(2\right)+b\left(2\right)\text{\hspace{0.17em}}x\left(1\right)-a\left(2\right)\text{\hspace{0.17em}}y\left(1\right)\\ y\left(3\right)=b\left(1\right)\text{\hspace{0.17em}}x\left(3\right)+b\left(2\right)\text{\hspace{0.17em}}x\left(2\right)+b\left(3\right)\text{\hspace{0.17em}}x\left(1\right)-a\left(2\right)\text{\hspace{0.17em}}y\left(2\right)-a\left(3\right)\text{\hspace{0.17em}}y\left(1\right)\\ \text{ }\text{ }⋮\text{ }\text{ }\text{ }\\ y\left(n\right)=b\left(1\right)\text{\hspace{0.17em}}x\left(n\right)+\cdots +b\left(n\right)\text{\hspace{0.17em}}x\left(1\right)-a\left(2\right)\text{\hspace{0.17em}}y\left(n-1\right)-\cdots -a\left(n\right)\text{\hspace{0.17em}}y\left(1\right).\end{array}$

To implement this filtering operation, you can use the MATLAB filter function. filter stores the coefficients in two row vectors, one for the numerator and one for the denominator. For example, to solve the difference equation

$y\left(n\right)-0.9y\left(n-1\right)=x\left(n\right)\text{ }⇒\text{ }Y\left(z\right)=\frac{1}{1-0.9\text{\hspace{0.17em}}{z}^{-1}}X\left(z\right)=H\left(z\right)\text{\hspace{0.17em}}X\left(z\right),$

you can use

b = 1;
a = [1 -0.9];
y = filter(b,a,x);
filter gives you as many output samples as there are input samples, that is, the length of y is the same as the length of x. If the first element of a is not 1, then filter divides the coefficients by a(1) before implementing the difference equation.