Filters

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

The relationship of FIR and IIR filters can be seen clearly in a "linear constant-coefficient

difference equation", i.e.

a(1)*y(n) + a(2)*y(n-1) + ... + a(Na+1)*y(n-Na) =

b(1)*x(n) + b(2)*x(n-1) + ... + b(Nb+1)*x(n-Nb)

setting a weighted sum of outputs equal to a weighted sum of inputs. This is like the
equation that we gave earlier for the causal FIR filter, except that in addition to the
weighted sum of inputs, we also have a weighted sum of outputs.

If we want to think of this as a procedure for generating output samples, we need to


rearrange the equation to get an expression for the current output sample y(n),

y(n) = (1/a(1)) * ( b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb)


- a(2)*y(n-1) - ... - a(na+1)*y(n-na) )

Adopting the convention that a(1) = 1 (e.g. by scaling other a's and b's), we can get rid of
the 1/a(1) term:

y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(Nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(Na+1)*y(n-


na)

If all the a(n) other than a(1) are zero, this reduces to our old friend the causal FIR filter.

This is the general case of a (causal) LTI filter, and is implemented by the MATLAB
function "filter."

>> help filter

FILTER One-dimensional digital filter.


Y = FILTER(B,A,X) filters the data in vector X with the
filter described by vectors A and B to create the filtered
data Y. The filter is a "Direct Form II Transposed"
implementation of the standard difference equation:

a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb)


- a(2)*y(n-1) - ... - a(na+1)*y(n-na)

If a(1) is not equal to 1, FILTER normalizes the filter


coefficients by a(1).

When X is a matrix, FILTER operates on the columns of X. When X


is an N-D array, FILTER operates along the first non-singleton
dimension.

[Y,Zf] = FILTER(B,A,X,Zi) gives access to initial and final


conditions, Zi and Zf, of the delays. Zi is a vector of length
MAX(LENGTH(A),LENGTH(B))-1 or an array of such vectors, one for
each column of X.
FILTER(B,A,X,[],DIM) or FILTER(B,A,X,Zi,DIM) operates along the
dimension DIM.

See also FILTER2, FILTFILT (in the Signal Processing Toolbox).

Let's look at the case where the b coefficients other than b(1) are zero (instead of the FIR
case, where the a(n) are zero):

y(n) = b(1)*x(n) - a(2)*y(n-1) - ... - a(na+1)*y(n-na)

In this case, the current output sample y(n) is calculated as a weighted combination of the
current input sample x(n) and the previous output samples y(n-1), y(n-2), etc. To get an
idea of what happens with such filters, let's start with the case where:

na is 1 (a "first-order" filter, looking only the immediately previous


output sample),
b(1) is 1,
and a(2) is (say) .5.

That is, the current output sample is the sum of the current input sample and half the
previous output sample.

We'll take an input impulse through a few time steps, one at a time.

Step 1
MATLAB n: ? 1 2 3 4 5 6 7 8
__________________________________________________________________
Input: 1 0 0 0 0 0 0 0
Output: 0 0 0 0 0 0 0 0 0
__________________________________________________________________
|
Result: 1

Step 2
MATLAB n: ? 1 2 3 4 5 6 7 8
_________________________________________________________________
Input: 1 0 0 0 0 0 0 0
Output: 0 1 0 0 0 0 0 0 0
_________________________________________________________________
|
Result: .5

Step 3
MATLAB n: ? 1 2 3 4 5 6 7 8
________________________________________________________________
Input: 1 0 0 0 0 0 0 0
Output: 0 1 .5 0 0 0 0 0 0
________________________________________________________________
|
Result: .25

Step 4
MATLAB n: ? 1 2 3 4 5 6 7 8
________________________________________________________________
Input: 1 0 0 0 0 0 0 0
Output: 0 1 .5 .25 0 0 0 0 0
|
Result: .125

It should be clear at this point that we can easily write an expression for the nth output
sample value: it is just

.5^(n-1)

(If MATLAB counted from 0, this would be simply .5^n).

Since what we are calculating is the impulse response of the system, we have
demonstrated by example that the impulse response can indeed have infinitely many non-
zero samples.

To implement this trivial first-order filter in MATLAB, we could use "filter". The call
will look like this:

b(n) -- input coeff. a(n) -- output coeff. input signal


filter([1 0], [1 -.5], [1 0 0 0 0 0 0
0])

and the result is:

>> filter([1 0], [1 -.5], [1 0 0 0 0 0 0 0])


ans =
Columns 1 through 7
1.0000 0.5000 0.2500 0.1250 0.0625 0.0312 0.0156
Column 8
0.0078

Is this business really still linear?

We can look at this empirically:

r = rand(1,6)
s = rand(1,6)
filter([1 0],[1 -.5],r+s)
filter([1 0],[1 -.5],r) + filter([1 0],[1 -.5],s)

For a more general approach, consider the value of an output sample y(n).

It is x(n)+.5*y(n-1),
and y(n-1) in turn is
x(n-1)+.5*y(n-2),
and y(n-2) in turn is
x(n-2)+.5*y(n-3), and so on.

By successive substitution we could write this as


y(n)=x(n)+.5*(
x(n-1)+.5*(
x(n-2)+.5*(
x(n-3)+.5*( ...

or again as

y(n)=x(n)+.5*x(n-1)+.25*x(n-2)+.125*x(n-3)+...

This is just

y(n) = .5^0 * x(n) + .5^1 * x(n-1) + .5^2 * x(n-2) + .5^3 * x(n-3) + ...

or

This is just like our old friend the convolution-sum form of an FIR filter, with the impulse
response provided by the expression .5^k, and the length of the impulse response being
infinite. Thus the same arguments that we used to show that FIR filters were linear will
now apply here.

So far this may seem like a lot of fuss about not much. What is this whole line of
investigation good for?

We'll answer this question in stages, starting with an example.

It's not a big surprise that we can calculate a sampled exponential by recursive
multiplication. Let's look at a recursive filter that does something less obvious. This time
we'll make it a second-order filter, so that the call to "filter" will be of the form

input coeff. output coeff. input vector


X = filter( [1 0 0], [1 a2 a3], x )

Let's set the second output coefficient a2 to -2*cos(2*pi/40), and the third output
coefficient a3 to 1, and look at the impulse response.

>> x = zeros(1,64); x(1) = 1;


>> X = filter([1 0 0], [1 -2*cos(2*pi/40) 1], x);
>> plot(X,'o');
Not very useful as a filter, actually, but it does generate a sampled sine wave (from an
impulse) with three multiply-adds per sample! In order to understand how and why it
does this, and how recursive filters can be designed and analyzed in the more general
case, we need to step back and take a look at some other properties of complex numbers,
on the way to understanding the z transform.

You might also like