Main Content

Filter Implementation

Convolution and Filtering

The mathematical foundation of filtering is convolution. For a finite impulse response (FIR) filter, the outputy(k)of a filtering operation is the convolution of the input signalx(k)with the impulse responseh(k):

y ( k ) = l = h ( l ) x ( k l ) .

If the input signal is also of finite length, you can implement the filtering operation using the MATLAB®conv函数。例如,过滤five-sample跑dom vector with a third-order averaging filter, you can storex(k)in a vectorx,h(k)in a vectorh, and convolve the two:

x = randn(5,1); h = [1 1 1 1]/4;% A third-order filter has length 4y = conv(h,x)
y = -0.3375 0.4213 0.6026 0.5868 1.1030 0.3443 0.1629 0.1787
The length ofy是一个我ess than the sum of the lengths ofxandh.

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 outputy,Y(z), is the product of the transfer function andX(z), the Z-transform of the inputx:

Y ( z ) = H ( z ) X ( z ) = ( h ( 1 ) + h ( 2 ) z 1 + + h ( n + 1 ) z n ) X ( z ) .

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

Note

The filter coefficient indices run from 1 to (n+ 1), rather than from 0 ton. 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 ( z ) = H ( z ) X ( z ) = b ( 1 ) + b ( 2 ) z 1 + ... + b ( n + 1 ) z n a ( 1 ) + a ( 2 ) z 1 + ... + a ( m + 1 ) z m X ( z ) ,

whereb(i) anda(i) are the filter coefficients. In this case, the order of the filter is the maximum ofnandm. IIR filters withn= 0 are also called all-pole, recursive, or autoregressive (AR) filters. IIR filters with bothnandmgreater 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 thefilterFunction

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 thata(1) = 1, move the denominator to the left side, and take the inverse Z-transform to obtain

y ( k ) + a ( 2 ) y ( k 1 ) + + a ( m + 1 ) y ( k m ) = b ( 1 ) x ( k ) + b ( 2 ) x ( k 1 ) + + b ( n + 1 ) x ( k n ) .

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

y ( k ) = b ( 1 ) x ( k ) + b ( 2 ) x ( k 1 ) + + b ( n + 1 ) x ( k n ) a ( 2 ) y ( k 1 ) a ( m + 1 ) y ( k m ) ,

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

y ( 1 ) = b ( 1 ) x ( 1 ) y ( 2 ) = b ( 1 ) x ( 2 ) + b ( 2 ) x ( 1 ) a ( 2 ) y ( 1 ) y ( 3 ) = b ( 1 ) x ( 3 ) + b ( 2 ) x ( 2 ) + b ( 3 ) x ( 1 ) a ( 2 ) y ( 2 ) a ( 3 ) y ( 1 ) y ( n ) = b ( 1 ) x ( n ) + + b ( n ) x ( 1 ) a ( 2 ) y ( n 1 ) a ( n ) y ( 1 ) .

To implement this filtering operation, you can use the MATLABfilter函数。filterstores the coefficients in two row vectors, one for the numerator and one for the denominator. For example, to solve the difference equation

y ( n ) 0.9 y ( n 1 ) = x ( n ) Y ( z ) = 1 1 0.9 z 1 X ( z ) = H ( z ) X ( z ) ,

you can use

b = 1; a = [1 -0.9]; y = filter(b,a,x);
filtergives you as many output samples as there are input samples, that is, the length ofyis the same as the length ofx. If the first element ofais not 1, thenfilterdivides the coefficients bya(1) before implementing the difference equation.

See Also

Apps

Functions