# Connexions

You are here: Home » Content » Lab 3: IIR Filtering

### Recently Viewed

This feature requires Javascript to be enabled.

### Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.

# Lab 3: IIR Filtering

Module by: Ricardo Radaelli-Sanchez. E-mail the author

Summary: In the last two labs, we have implemented filters using finite impulse response, or FIR, filters. In this lab, we will explore infinite impulse response, or IIR, filters.

Note: You are viewing an old version of this document. The latest version is available here.

## Introduction

In the last two labs, we have implemented filters using finite impulse response, or FIR, filters. In this lab, we will explore infinite impulse response, or IIR, filters.

Like FIR filters, IIR filters are LTI (linear time-invariant) systems that can be used to recreate a large range of different frequency responses. Compared to FIR filters, IIR filters have both advantages and disadvantages; on one hand, implementing an IIR filter with certain stopband-attenuation and transition band requirements typically requires far fewer filter taps than the corresponding FIR filter. This leadings to a significant reduction in the computational complexity required to achieve a given frequency response. However, the poles in the transfer function require that feedback be used to implement an IIR system. In addition to inducing nonlinear phase in the filter (delaying different frequency input signals by different amounts), the feedback can lead to complications implementing IIR filters on a fixed-point processor.

In this lab, you will exploring the advantages and disadvantages of IIR filters by implementing and examining a fourth-order IIR system on the TI C54x fixed-point DSP. The IIR filter should be implemented the cascade of two second-order direct-form II sections. The data flow for a direct-form II second-order, or bi-quad, section is shown below. Note that in Direct Form II, the states (delayed samples) are neither the input nor the output samples, but are instead the intermediate values wnwn.

## Pre-lab

The transfer function for the second-order section shown in Figure 1 is

Hz=G1+ b 1 z-1+ b 2 z-21+ a 1 z-1+ a 2 z-2 H z G 1 b 1 z -1 b 2 z -2 1 a 1 z -1 a 2 z -2
(1)
The freqz command in Matlab can accept an IIR filter as well as a simple FIR filter, and display its frequency response. For more information about this, Type "help freqz". Be sure to look at their definition of the transfer function.

### Exercise

Your first exercise in the prelab is to derive the above transfer function by first writing the difference equations for wnwn in terms of the input and past values (wn1wn1 and wn2wn2). Then write the difference equation for ynyn (also in terms of the past samples of wnwn). After deriving the appropriate difference equations, compute the corresponding ZZ-transforms and use the relation Hz=YzXz=YzWzWzXz H z Y z X z Y z W z W z X z to verify the IIR transfer function in equation Equation 1.

Next, design the coefficients for the two second-order sections you will be implementing. As with the prelab exercise for Lab 2, you are to write a Matlab script to compute the coefficients. Begin by designing the fourth-order filters and checking the response using the Matlab commands:

                      

[B,A] = ellip(4,.25,10,.25)
freqz(B,A)




#### note:

If you use the freqz command as shown above, both the magnitude (in decibels) and the phase of the response will be shown.

Next you must find the roots of the numerator, zeros, and denominator, poles, so that you can combine them into two second-order sections. The Matlab commands roots and poly will be useful for this task. Save the scripts you use to decompose your filter into second-order sections, as you will use them again for the matlab portion of the laboratory exercise.

Once you have obtained the coefficients for each of your two second-order sections you are now ready to choose a gain factor, GG. As part of your Matlab script, use freqz to compute the response WzXzWzXz with G=1G1 for either of the sets of second-order coefficients. (you can use the transfer function of WzXzWzXz designed earlier). Recall that on the DSP we cannot represent fractions larger than 1.0. If the maximum value of |WzXz| W z X z exceeds 1.0 then we have the problem of the possibility of overflow; that is, a bounded input could result in wn w n terms that are larger than one. You must therefore select the gain values (one for each section) such that the response from the input to the states, WzXzWzXz is always less than or equal to one in magnitude. In other words, set what value of GG to ensure that |WzXz|1 W z X z 1 .

### Testing

For this lab you will want to generate a 512-point test vector that is simply an impulse, δnδn. The output from your test vector simulation should then be the time-domain impulse response of your IIR filter.

## Matlab exercise

One important issue that must be considered when IIR filters are used on a fixed-point processor is that the filter coefficients that are actually used are quantized from the "exact" (high-precision floating point) values computed by Matlab. Since, unlike with FIR filters, this quantization can actually have significant impacts in the response of the filter, it is worth examining the effects of quantizing the filter coefficients on the actual filter response.

By default, Matlab uses 64-bit floating point numbers in all of its computation. These floating point numbers can typically represent 15-16 digits of precision, far more than the DSP can represent internally. For this reason, when creating filters in Matlab, we can generally regard its precision as "infinite," because it is high enough for any reasonable task. 1 The DSP, on the other hand, operates using 16-bit fixed-point numbers in the range of -1.0 to 1.02-15 1.0 2 -15 . This gives the DSP only 4 to 5 digits of precision and only if the input is properly scaled to occupy the full range from -1 to 1.

For this section of the lab, you are to look at a notch filter generated using the butter command: [B,A] = butter(2,[0.07 0.10],'stop').

### Quantizing coefficients in Matlab

It is not difficult to use Matlab to quantize the filter coefficients to the 16-bit precision used on the DSP. To do this, first take each vector of filter coefficients (that is, the A and B vectors) and divide by the smallest power of two such that the resulting absolute value of the largest filter coefficient is less than or equal to one. 2 Next, quantize the resulting vector to 16 bits of precision by first multiplying them by 215=32768 2 15 32768 , rounding (use round), and then dividing the resulting vectors back down by 32768. Then multiply the resulting numbers, which will be in the range of -1 to 1, back by the power of two that you divided out.

### Effects of quantization

Explore the effects of quantization by quantizing the filter coefficients for the notch filter. Use the freqz command to compare the response of the unquantized filter with two quantized versions: first, quantize the entire fourth-order filter at once, and second, quantize the second-order ("bi-quad") sections seperately and recombine the resulting quantized sections using the conv function. Compare the response of the unquantized filter and the two quantized versions. Which one is "better?" Why do we always implement IIR filters using second-order sections instead of implementing fourth (or higher) order filters directly?

Be sure to print out graphs showing the differences between the filter responses of the unquantized notch filter, the notch filter quantized as a single fourth-order section, and the notch filter quantized as two second-order sections. Also save the Matlab code you use to generate these graphs, and be prepared to reproduce and explain the graphs as part of your quiz. Make sure that in your comparisons, you rescale the resulting filters to ensure that the response is unity (one) at frequencies far outside the notch.

## Implementation

On the DSP, you will implement the elliptic low-pass filter (that you designed using the ellip command) from the prelab. You should not try to implement the notch filter you designed for the Matlab exercise above, as it will not work correctly when implemented using Direct Form II. (Be prepared to tell us why during your quiz!)

To implement the fourth-order filter, start with a single set of second-order coefficients and implement a single second-order section. Write pseudo-code for a single second-order filter on paper and review your code with a TA. Once your single second-order IIR is working properly you can then proceed to code the entire fourth-order filter.

### Large coefficients

You may have noticed that some of the coefficients you have computed for the second-order sections are larger than 1.0 in magnitude. For any stable second-order IIR section, the "0" and "2" coefficients (a0a0 and a2a2 for example) will always be less than or equal to 1.0. However, the "1" coefficient can be as large as 2.0 (in magnitude). To overcome this problem you will have to divide the a1a1 and b1b1 coefficient by two prior to saving them for inclusion into your DSP code. Then, in your implementation, you will have to repeat the multiplication (and addition) twice to correct for using half the coefficient value.

### Repeating code

Rather than write the code for each second-order section, you are encouraged to first write one section, then cycle through the second-order section twice using the repeat structure below. Because the IIR code will have to run inside the block I/O loop and this loop uses the block repeat counter (BRC), you must use another looping structure to avoid corrupting the BRC.

#### Note:

You will have to make sure that your code uses different coefficients and states during the second cycle of the repeat loop.

                      

stm     (num_stages-1),AR1

start_stage

; IIR code goes here

banz    start_stage,*AR1-




### Gain

It may be necessary to add gain to the output of the system. To do this, simply shift the output left (which can be done using the ld opcode with its optional "shift" parameter) before saving the output to memory.

## Footnotes

1. However, some IIR filters are not necessarily "reasonable!"
2. This is an easy but fairly reasonable approximation of how numbers outside the range of -1 to 1 are actually handled on the DSP.

## Content actions

### Give feedback:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks