# Connexions

You are here: Home » Content » FIR Filtering

### Recently Viewed

This feature requires Javascript to be enabled.

# FIR Filtering

Module by: Hyeokho Choi. E-mail the author

Summary: This module is an overview of the design and implementation of FIR filtering.

## Lab Objective

In this lab, you will learn the design and implementation of FIR filtering. The discrete convolution equation that describes the behavior of a causal length- N N FIR filter is:

y n= k =0N1 h k x nk y n k 0 N 1 h k x n k
(1)
where x n x n represents the sampled input, h n h n represents the filter’s impulse response, and y n y n output.

## Digital Filter Basics

You can skip reading this section if you are already familiar with digital filtering.

All of you should be already familiar with analog filters, which takes analog input signal and output analog signal. Digital filters, with the sampling device and digital-to-analog converter, can do the same job as analog filters. Analog filters are specified by its impulse response h t h t and the output signal y t y t for input signal x t x t is given by the convolution integral
y t=0t x tu h ud u y t u 0 t x t u h u
(2)
Similarly, digital filters are specified by its impulse response h n h n and the output sequence y n y n for the input sequence (samples of input analog signal) x n x n by the discrete convolution
y n= k =0N1 h k x nk y n k 0 N 1 h k x n k
(3)
The filter design problem is to design the impulse response h n h n so that the digital filter, together with the sampler and D/A converter, performs the same processing as the analog filters. There are many possible ways to pick h n h n to implement filtering. The details of the techniques will be covered in ELEC431.

In this lab, we will not be concerned with the details of filter design techniques. Rather, you only learn how to use several MATLAB commands that design the filters for you once you specify desired frequency response of the filter. The real-time implementation will be simply implementing the discrete-time convolution equation with the impulse response (filter coefficients) obtained from MATLAB. If you want to learn more details, please refer to any introductory digital signal processing textbook.

## MATLAB FIR Filter Design

### Exercise 1: Design FIR filters using MATLAB

Design FIR filters and compare them using MATLAB by the following steps (assume 48kHz sampling rate in all your designs):

1. Study how to use the MATLAB commands fir1, fir2, remez, filter, and freqz.
2. Design an order-40 low-pass filter having cut-off frequency 10kHz by the windowing method using the fir1 command.
3. Design an order-40 low~pass filter having transition band l0kHz-llkHz using the remez command
4. Compare the above two filters by showing the frequency response using the freqz command. How are the filters different?
5. Design an order-40 high-pass filter having transition band 9kHz-l0kHz by frequency sampling method using the fir2 command.
6. Design an order-40 high-pass filter having transition band 9kHz-l0kHz using the remez command.
7. Compare the above two filters by showing the frequency response using the freqz command. How are the filters different?
8. Design an order-80 band-pass filter having pass band 4kHz-8kHz using the remez command. Specify the transition bands as 3kHz-4kHz and 8kHz-9kHz.
In your lab report, state the filter design MATLAB command for each of the above filters. Also list the designed filter coefficients with a plot of the filter frequency response.

## Implementation

In this lab, you implement the FIR filtering in assemb1y language by modifying the dot product code you wrote in (Reference).

### Storing Filter Coefficients

Rather than defining the filter coefficients in your main assembly program file, it is usually more convenient to store them in a separate file. By defining the coefficients in a separate assembly (for example, coeff.asm) file, you can load the coefficients at a desired memory location at the run time, although it is not essential for the current simple FIR filtering lab.

The assembly file containing the filter coefficients can be written as follows:


1          .def	   _coef
2          .sect   “coeffs”
3
4  _coef:
5          .short  0ff9bh
6          .short  0ff06h
7          .short  0feffh
8          .short  0ff93h
9          .short  070h
10         .short  0117h
11         .short  0120h
12         .short  07bh

Each coefficient must be converted to the Q-15 format and defined by each .short assembly directive. For your convenience, I wrote a short MATLAB script save_coef.m that converts the filter coefficients stored as a MATLAB vector to Q-15 format and then writes to a file exactly in the above format. The section coeffs should be defined in the link command file so that the coefficients are to be loaded at the correct memory location.

You can simply include the coeff.asm using the .include directive at the beginning of your main assembly program.

#### Exercise 2: Coefficient File

Make coefficient files for each of the filters you designed in Exercise 1.

### Real-Time Implementation

Now you are ready to implement FIR filter algorithm.

#### Exercise 3: FIR Implementation

1. Write an assembly routine that implements the FIR filter by modifying the inner product program you have written in (Reference).
2. Combine the FIR filtering function with the interrupt-based codec input-output code you wrote in (Reference). Your code should perform FIR filtering on the input samples and output the filtered result to the codec. Both the left and right channels are processed by the sample filter. To write the designed filter vector saved as a MATLAB vector as a .asm file, use the provided save_coef.m function. First implement the length-40 lowpass filter with 10kHz passband designed using the remez.
3. Connect the soundcard signal generator to the codec microphone jack to input 5kHz sine waves. Connect the oscilloscope to observe both the codec input and output.
4. Input white noise (from PC sound card) to the DSK input and observe the spectrum of the output. Can you see the filter frequency response?
5. While changing the sine wave frequency between 5kHz and 15kHz as the filter input, observe the change in the output. Can you plot a transfer function (magnitude only) of the filter?
6. Repeat the above steps using other filters you designed using MATLAB.

## Testing

You can count the number of CPU cycles necessary to execute the FIR filtering function to see the maximum possible sampling frequency the filter can handle.

### Exercise 4: Testing Your FIR Filter

1. Count the number of cycles of the FIR filtering routine.
2. What is the maximum possible sample rate your FIR routine can possibly handle?
3. What are some of the things you can do to reduce the cycle count of your FIR filtering function?
4. Optimize your FIR filtering assembly code as much as possible and compare the clock cycles before and after optimization.

### Circular Buffer Implementation

As you might already have noticed, a lot of cycles are wasted in FIR filtering while maintaining the buffer to see if you reached the end of buffer and update the address pointers properly. To avoid this unnecessary buffer maintenance, the TI DSP processors have a special addressing mode, called circular addressing. Using circular addressing, you can define a block of memory as a circular buffer. As you increase (or decrease) the pointer register pointing to the buffer index beyond the buffer limit, it automatically points to the other end of the buffer, implementing a circle of data array. Instead of moving the data samples themselves, you can move the pointer which specifies the beginning of the buffer, as each new sample is processed. You don’t need to check if you reached the end of buffer because the address pointer returns to the beginning of the buffer immediately after reaching the end.

Of the 32 registers on the C6x, 8 of them can perform circular addressing. These registers are A4 through A7 and B4 through B7. Since circular addressing is not default, each of these registers must be specified as circular using the AMR (Address Mode Register) register. The lower 16 bits of the AMR are used to select the mode for each of the 8 registers. The upper 10 bits (6 are reserved) are used to set the length of the circular buffer. Buffer size is determined by 2N+1 2 N 1 bytes, where N N is the value appearing in the block size fields of the AMR register. The top address of the buffer needs to be aligned with proper physical memory block address using the .align assembler directive. See the explanation on the .align directive in (Reference).

#### Exercise 5: Circular Buffer

1. Read the TMS320C62x/C67x CPU and Instruction Set Reference Guide to learn how to define circular buffers.
2. Modify your FIR filtering assembly code to use circular buffering.
3. Count the clock cycles for each FIR filter computation.
4. Optimize your code as much as possible to reduce the clock cycles.

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

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