Important Disclaimer

The purpose of this blog is purely to serve as a compilation of good technical material for my students. No financial or other motives are involved. Most of the content in this blog has been reproduced from other sources. I have made every attempt to mention the source link at the beginning of each blog. All readers are requested to kindly acknowledge that source and not this blog, in case you find the post helpful. However, I have not been able to trace the source links for some of my older posts. I wish to emphasize that this is not intentional and any help in this regard would be appreciated.

Jan 15, 2011

Digital filters

post taken from : http://www.dsptutor.freeuk.com/dfilt4.htm
Kindly refer to the above site for more details and acknowledge the above site (not this blog). This post has been reproduced strictly for academic use.


Examples of simple digital filters

The following examples illustrate the essential features of digital filters.

1. UNITY GAIN FILTER: y(n) = x(n)

Each output value yn is exactly the same as the corresponding input value xn:

y(0) = x(0)
y(1) = x(1)
y(2) = x(2)
... etc

This is a trivial case in which the filter has no effect on the signal.

2. SIMPLE GAIN FILTER: y(n) = K.x(n) (K = constant)

This simply applies a gain factor K to each input value:

y(0) = K.x(0)
y(1) = K.x(1)
y(2) = K.x(2)
... etc
K > 1 makes the filter an amplifier, while 0 < K < 1 makes it an attenuator. K < 0 corresponds to an inverting amplifier. Example (1) above is the special case where K = 1.

3. PURE DELAY FILTER: y(n) = x(n-1)

The output value at time t = nh is simply the input at time t = (n-1)h, i.e. the signal is delayed by time h:
y(0) = x(-1)
y(1) = x(0)
y2 = x(1)
y(3) = x(2)
... etc
Note that as sampling is assumed to commence at t = 0, the input value x(-1) at t = -h is undefined. It is usual to take this (and any other values of x prior to t = 0) as zero.

4. TWO-TERM DIFFERENCE FILTER: y(n) = x(n) - x(n-1)

The output value at t = nh is equal to the difference between the current input xn and the previous input x(n-1):
y(0) = x(0) - x(-1)
y(1) = x(1) - x(0)
y(2) = x(2) - x(1)
y(3) = x(3) - x(2)
... etc

i.e. the output is the change in the input over the most recent sampling interval h. The effect of this filter is similar to that of an analog differentiator circuit.

5. TWO-TERM AVERAGE FILTER: y(n) = (x(n) + x(n-1)) / 2

The output is the average (arithmetic mean) of the current and previous input:
y0 = (x(0) + x(-1)) / 2
y1 = (x(1) + x(0)) / 2
y2 = (x(2) + x(1)) / 2
y3 = (x(3) + x(2)) / 2
... etc
This is a simple type of low pass filter as it tends to smooth out high-frequency variations in a signal. (We will look at more effective low pass filter designs later).

6. THREE-TERM AVERAGE FILTER: y(n) = (x(n) + x(n-1) + x(n-2)) / 3

This is similar to the previous example, with the average being taken of the current and two previous inputs:
y(0) = (x(0) + x(-1) + x(-2)) / 3
y(1) = (x(1) + x(0) + x(-1)) / 3
y(2) = (x(2) + x(1) + x(0)) / 3
y(3) = (x(3) + x(2) + x(1)) / 3
... etc
As before, x(-1) and x(-2) are taken to be zero.

7. CENTRAL DIFFERENCE FILTER: y(n) = (x(n) - x(n-2)) / 2

This is similar in its effect to example (4). The output is equal to half the change in the input signal over the previous two sampling intervals:
y(0) = (x(0) - x(-2)) / 2
y(1) = (x(1) - x(-1)) / 2
y(2) = (x(2) - x(0)) / 2
y(3) = (x(3) - x(1)) / 2
etc..

Order of a digital filter

The order of a digital filter can be defined as the number of previous inputs (stored in the processor's memory) used to calculate the current output.

This is illustrated by the filters given as examples in the previous section.

Example (1): y(n) = x(n)
This is a zero order filter, since the current output yn depends only on the current input xn and not on any previous inputs.

Example (2): y(n) = Kx(n)
The order of this filter is again zero, since no previous outputs are required to give the current output value.

Example (3): y(n) = x(n-1)
This is a first order filter, as one previous input (x(n-1)) is required to calculate y(n). (Note that this filter is classed as first-order because it uses one previous input, even though the current input is not used).

Example (4): y(n) = x(n) - x(n-1)
This is again a first order filter, since one previous input value is required to give the current output.

Example (5): y(n) = (x(n) + x(n-1)) / 2
The order of this filter is again equal to 1 since it uses just one previous input value.

Example (6): y(n) = (x(n) + x(n-1) + x(n-2)) / 3
To compute the current output y(n), two previous inputs (x(n-1) and x(n-2)) are needed; this is therefore a second-order filter.

Example (7): y(n) = (x(n) - x(n-2)) / 2
The filter order is again 2, since the processor must store two previous inputs in order to compute the current output. This is unaffected by the absence of an explicit x(n-1) term in the filter expression.

The order of a digital filter may be any positive integer. A zero-order filter (such as those in examples (1) and (2) above) is possible, but somewhat trivial, since it does not really filter the input signal in the accepted sense.

Digital filter coefficients

All of the digital filter examples given in the previous section can be written in the following general forms:
Zero order: y(n) = a0.x(n)
First order: y(n) = a0.x(n) + a1.x(n-1)
Second order: y(n) = a0.x(n) + a1.x(n-1) + a2.x(n-2)

Similar expressions can be developed for filters of any order.


The constants a0, a1, a2, ... appearing in these expressions are called the filter coefficients. The values of these coefficients determine the characteristics of a particular filter.

The table below gives the values of the coefficients of each of the filters given as examples in the previous section.

Example Order a0 a1 a2
1 0 1 - -
2 0 K - -
3 1 0 1 -
4 1 1 -1 -
5 1 1/2 1/2 -
6 2 1/3 1/3 1/3
7 2 1/2 0 -1/2

Recursive and non-recursive filters

For all the examples of digital filters discussed so far, the current output (yn) is calculated solely from the current and previous input values (x(n), x(n-1), x(n-2), ...). This type of filter is said to be non-recursive.

A recursive filter is one which in addition to input values also uses previous output values. These, like the previous input values, are stored in the processor's memory.

The word recursive literally means "running back", and refers to the fact that previously-calculated output values go back into the calculation of the latest output. The expression for a recursive filter therefore contains not only terms involving the input values (x(n), x(n-1), x(n-2), ...) but also terms in y(n-1), y(n-2), ...

From this explanation, it might seem as though recursive filters require more calculations to be performed, since there are previous output terms in the filter expression as well as input terms. In fact, the reverse is usually the case. To achieve a given frequency response characteristic using a recursive filter generally requires a much lower order filter, and therefore fewer terms to be evaluated by the processor, than the equivalent non-recursive filter. This will be demonstrated later.

Note: FIR and IIR filters

Some people prefer an alternative terminology in which a non-recursive filter is known as an FIR (or Finite Impulse Response) filter, and a recursive filter as an IIR (or Infinite Impulse Response) filter. These terms refer to the differing "impulse responses" of the two types of filter

The impulse response of a digital filter is the output sequence from the filter when a unit impulse is applied at its input. (A unit impulse is a very simple input sequence consisting of a single value of 1 at time t = 0, followed by zeros at all subsequent sampling instants). An FIR filter is one whose impulse response is of finite duration. An IIR filter is one whose impulse response (theoretically) continues for ever, because the recursive (previous output) terms feed back energy into the filter input and keep it going. The term IIR is not very accurate, because the actual impulse responses of nearly all IIR filters reduce virtually to zero in a finite time. Nevertheless, these two terms are widely used.

Example of a recursive filter

A simple example of a recursive digital filter is given by

y(n) = x(n) + y(n-1)

In other words, this filter determines the current output (y (n)) by adding the current input (x(n)) to the previous output (y(n-1)).

Thus:

y(0) = x(0) + y(-1)
y(1) = x(1) + y(0)
y(2) = x(2) + y(1)
y(3) = x(3) + y(2)
... etc

Note that y(-1) (like x(-1)) is undefined, and is usually taken to be zero.

Let us consider the effect of this filter in more detail. If in each of the above expressions we substitute for yn-1 the value given by the previous expression, we get the following:

y(0) = x(0) + y(-1) = x(0)(since y(-1) does'nt exist, it has been taken as 0)
y(1) = x(1) + y(0) = x(1) + x(0)
y(2) = x(2) + y(1) = x(2) + x(1) + x(0)
y(3) = x(3) + y(2) = x(3) + x(2) + x(1) + x(0)
... etc

Thus we can see that y(n), the output at t = nh, is equal to the sum of the current input xn and all the previous inputs. This filter therefore sums or integrates the input values, and so has a similar effect to an analog integrator circuit.

This example demonstrates an important and useful feature of recursive filters: the economy with which the output values are calculated, as compared with the equivalent non-recursive filter. In this example, each output is determined simply by adding two numbers together.

For instance, to calculate the output at time t = 10h, the recursive filter uses the expression

y(10) = x(10) + y(9)

To achieve the same effect with a non-recursive filter (i.e. without using previous output values stored in memory) would entail using the expression

y(10) = x(10) + x(9) + x(8) + x(7) + x(6) + x(5) + x(4) + x(3) + x(2) + x(1) + x(0)

This would necessitate many more addition operations, as well as the storage of many more values in memory.

Order of a recursive (IIR) digital filter

The order of a digital filter was defined earlier as the number of previous inputs which have to be stored in order to generate a given output. This definition is appropriate for non-recursive (FIR) filters, which use only the current and previous inputs to compute the current output. In the case of recursive filters, the definition can be extended as follows:

The order of a recursive filter is the largest number of previous input or output values required to compute the current output.

This definition can be regarded as being quite general: it applies both to FIR and IIR filters.

For example, the recursive filter discussed above, given by the expression

y(n) = x(n) + y(n-1)

is classed as being of first order, because it uses one previous output value (y(n-1)), even though no previous inputs are required.

In practice, recursive filters usually require the same number of previous inputs and outputs. Thus, a first-order recursive filter generally requires one previous input (x(n-1)) and one previous output (y(n-1)), while a second-order recursive filter makes use of two previous inputs (x(n-1) and x(n-2)) and two previous outputs (yn-1 and y(n-2)); and so on, for higher orders.

Note that a recursive (IIR) filter must, by definition, be of at least first order; a zero-order recursive filter is an impossibility. (Why?)

Self-assessment question

State the order of each of the following recursive filters:

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


Answeers

1. Order = 1
2. Order = 3
3. Order = 2


Coefficients of recursive (IIR) digital filters

From the above discussion, we can see that a recursive filter is basically like a non-recursive filter, with the addition of extra terms involving previous outputs (y(n-1), y(n-2) etc.).

A first-order recursive filter can be written in the general form

y(n) = (a0.x(n) + a1.xn-1 - b1.y(n-1)) / b0

Note the minus sign in front of the "recursive" term b1yn-1, and the factor (1/b0) applied to all the coefficients. The reason for expressing the filter in this way is that it allows us to rewrite the expression in the following symmetrical form:

b0.y(n) + b1.y(n-1) = a0.x(n) + a1.x(n-1)

In the case of a second-order filter, the general form is

y(n) = (a0.x(n) + a1.x(n-1) + a2.x(n-2) - b1.y(n-1) - b2.y(n-2)) / b0

An alternative "symmetrical" form of this expression is

b0.y(n) + b1.y(n-1) + b2.y(n-2) = a0.x(n) + a1.x(n-1) + a2.x(n-2)

Note the convention that the coefficients of the inputs (the x's) are denoted by a's, while the coefficients of the outputs (the y's) are denoted by b's

Self-assessment question

Identify the values of the filter coefficients for the first-order recursive filter
y(n) = x(n) + y(n-1)
discussed earlier.
Repeat this for each of the filters in previous self assessment questions.

Answers
a0 = 1 a1 = 0
b0 = 1 b1= -1

Answer For filters listed in previous self assessment:

1.
a0 = 2 a1 = -1
b0 = 1 b1 = -1
2.
a0 = 0 a1 = 1 a2 = 0 a3 = -1
b0 = 1 b1 = 2 b2 = 0 b3 = 0
3.
a0 = 1 a1 = 2 a2 = 1
b0 = 1 b1 = 2 b2 = -1

The transfer function of a digital filter

In the last section, we used two different ways of expressing the action of a digital filter: a form giving the output yn directly, and a "symmetrical" form with all the output terms (y's) on one side and all the input terms (x's) on the other.

In this section, we introduce what is called the transfer function of a digital filter. This is obtained from the symmetrical form of the filter expression, and it allows us to describe a filter by means of a convenient, compact expression. Tthe transfer function of a filter can be used to determine many of the characteristics of the filter, such as its frequency response.
The unit delay operator

First of all, we must introduce the unit delay operator, denoted by the symbol

(z-1) (this is actually z raised to the power of -1)

When applied to a sequence of digital values, this operator gives the previous value in the sequence. It therefore in effect introduces a delay of one sampling interval.

Applying the operator (z-1) to an input value (say x(n)) gives the previous input (x(n-1)):

(z-1).x(n) = x(n-1)

Suppose we have an input sequence

x(0) = 5
x(1) = -2
x(2) = 0
x(3) = 7
x(4) = 10

Then

(z-1).x(1) = x(0) = 5
(z-1).x(2) = x(1) = -2
(z-1).x(3) = x(2) = 0

and so on. Note that (z-1).x(0) would be x(-1) which is unknown (and usually taken to be zero, as we have already seen).

Similarly, applying the (z-1) operator to an output gives the previous output:

(z-1).y(n) = y(n-1)

Applying the delay operator (z-1) twice produces a delay of two sampling intervals:

(z-1).((z-1).x(n)) = (z-1).x(n-1) = x(n-2)

We adopt the (fairly logical) convention

(z-1).(z-1) = (z-2)

i.e. the operator z-2 represents a delay of two sampling intervals:

(z-2).x(n) = x(n-2)

This notation can be extended to delays of three or more sampling intervals, the appropriate power of (z-1) being used.

Let us now use this notation in the description of a recursive digital filter. Consider, for example, a general second-order filter, given in its symmetrical form by the expression

b0.y(n) + b1.y(n-1) + b2.y(n-2) = a0.x(n) + a1.x(n-1) + a2.x(n-2)

We will make use of the following identities:

y(n-1) = (z-1).yn

y(n-2) = (z-2).yn

x(n-1) = (z-1).x(n)

x(n-2) = (z-2) x(n)

Substituting these expressions into the digital filter gives

(b0 + b1.(z-1) + b2.(z-2)).y(n) = (a0 + a1.(z-1) + a2.(z-2)).x(n)

Rearranging this to give a direct relationship between the output and input for the filter, we get

y(n)/x(n) = (a0 + a1.(z-1) + a2.(z-2)) / (b0 + b1.(z-1) + b2.(z-2))

This is the general form of the transfer function for a second-order recursive (IIR) filter.

For a first-order filter, the terms in (z-2) are omitted. For filters of order higher than 2, further terms involving higher powers ofz-1 are added to both the numerator and denominator of the transfer function.

A non-recursive (FIR) filter has a simpler transfer function which does not contain any denominator terms. The coefficient b0 is regarded as being equal to 1, and all the other b coefficients are zero. The transfer function of a second-order FIR filter can therefore be expressed in the general form

y(n) / x(n) = a0 + a1z-1 + a2z-2

Note: keep in mind that z-1, z-2 etc are to be read as 'z raised to the power of -1' etc. and not 'z minus 1'.


Transfer function examples


1. The three-term average filter, defined by the expression

y(n) = 1/3 (x(n) + x(n-1) + xn-2)

can be written using the (z-1) operator notation as

y(n) = 1/3 (x(n) + (z-1).x(n) + (z-2).x(n))

= 1/3 (1 + (z-1) + (z-2)) x(n)

The transfer function for the filter is therefore

y(n) / x(n) = 1/3 (1 + (z-1) + (z-2))


2.The general form of the transfer function for a first-order recursive filter can be written

y(n) / x(n) = (a0 + a1.(z-1)) / (b0 + b1.(z-1))

Consider, for example, the simple first-order recursive filter

y(n) = x(n) + y(n-1)

which we discussed earlier. To derive the transfer function for this filter, we rewrite the filter expression using the z-1 operator:

(1 - (z-1)) y(n) = x(n)

Rearranging gives the filter transfer function as

y(n) / x(n) = 1 / (1 - (z-1))

3.As a further example, consider the second-order IIR filter

y(n) = x(n) + 2x(n-1) + x(n-2) - 2y(n-1) + y(n-2)

Collecting output terms on the left and input terms on the right to give the "symmetrical" form of the filter expression, we get

y(n) + 2y(n-1) - y(n-2) = x(n) + 2x(n-1) + x(n-2)

Expressing this in terms of the z-1 operator gives

(1 + 2.(z-1) - (z-2)) y(n) = (1 + 2.(z-1) + (z-2)) x(n)

and so the transfer function is

y(n)/x(n) = (1 + 2.(z-1) + (z-2)) / (1 + 2.(z-1) - (z-2))


Q. Derive the transfer functions of each of the filters in first self assessment.
Answers:
1. y(n) / x(n) = (2 - (z-1)) / (1 - (z-1))
2. y(n) / x(n) = ((z-1) - (z-3)) / (1 + 2(z-1))
3. y(n) / x(n) = (1 + 2(z-1) + (z-2)) / (1 + 2(z-1) - (z-2))


Tutorial question

A digital filter is described by the expression

y(n) = 2x(n) - x(n-1) + 0.8y(n-1)

1. State whether the filter is recursive or non-recursive. Justify your answer.
2. State the order of the filter.
3. Derive the filter transfer function.
4. The following sequence of input values is applied to the filter.

x(0) = 5
x(1) = 16
x(2) = 8
x(3) = -3
x(4) = 0
x(5) = 2

Determine the output sequence for the filter, from y(0) to y(5).

Answers:

1. Recursive: it contains a previous output term, y(n-1)
2. Order = 1
3. y(n) / x(n) = (2 - (z-1)) / (1 - 0.8(z-1))
4. y(0) = 10
y(1) = 35
y(2) = 28
y(3) = 8.4
y(4) = 9.72
y(5) = 11.776

No comments: