<< Chapter < Page Chapter >> Page >
In this lab, you will implement an FIR filter on an embedded microcontroller.

In this lab, you will learn how to implement an FIR filter. The discrete convolution equation that describes the behavior of a causal length-N filter is:

where x(n) represents the sampled input, h(n) represents the filter's impulse response (it's coefficients), and y(n) is the filtered output.

Part i: io setup

Adc and timers

In order to have a known sampling rate, the ADC must be interrupt driven. Set up your clock as usual, and let Timer B handle any filter clocks, if available. Set up the ADC to be interrupt driven and triggered by Timer A. Use the following code in order to set up Timer A: /* TIMER A */ TACCR0 = 900; TACCTL1 = OUTMOD_4; TACTL |= TASSEL_2+MC_1; Given these timing settings, what is the sampling rate?

Dma and dac

Configure the DMA to transfer your filtered output sample to DAC0. You can set the DMA trigger inside the ADC12 interrupt service routine after you have finished the filtering.

Circular buffer

Notice that FIR filter equation requires the N last input samples to compute the filtered output. In this case, you will need to store the last 21 samples including the most recent one. Implement a circular buffer to store all the required samples. Once you have reached the end of the buffer, reset your index back to zero.

Testing io setup

Verify that the project is working so far by sending the newest value stored in your buffer to the DAC via DMA. Also, using the memory watch window, make sure that input samples are stored in their correct spot in the buffer. You may have to pause your program every time a sample is taken to verify that the buffer filling up correctly.

Part ii: filter implementation

You will be implementing a 20-tap FIR filter. To obtain the filter's coefficients open up Matlab and type the following: B = remez(20,[0,.5,.55,1],[0,0,1,1]) Next, plot the filter's frequency response with the freqz command. What kind of filter is this, and given the ADC sampling frequency, what is the expected cutoff frequency?

Notice that the array of coefficients includes negative numbers. They will have to be converted to Q-15 (16 bit), two's complement format. You can calculate the new coefficients on your own, or you may include the following line in your project: signed short coeff[21]={0x04F5,0xF4D0,0xFA12,0x03AF,0x0295,0xF86D,0xFCD1,0x0D18,0x032C, 0xD768,0x3CC9,0xD768,0x032C,0x0D18,0xFCD1,0xF86D,0x0295,0x03AF,0xFA12,0xF4D0,0x04F5};

Inside your ADC12 interrupt service routine, compute the filtered output sample by taking the inner product of your input samples with the filter coefficients. The inner product is described mathematically in the equation above. Conceptually, your output can be described as output_sample = most_recent_input_sample*coeff[0] + second_most_recent_input_sample*coeff[1]+...+oldest_input_sample*coeff[20] . This is a simple multiply and accumulate operation.

In order to get real time operation, the MSP's hardware multiplier must be used. Be sure to use signed multiplication. How can you use some of the other multiplier functions to simplify your code? How is the output of the multiplier stored? Since the DAC only transmits 16 bits at a time, which multiplier register should be used as the output, and argue why this is a good scheme?

Part iii: testing

Hook up the function generator and the oscilloscope to the ADC and DAC of the board, respectively. Create a new coefficient array of length 21 consisting of a single one and the rest zeros. ([1,0,0,...,0]) What would you expect the output to be? Do you see that on the oscilloscope? Once this is working, try the original array of coefficients. Try changing the frequency on the function generator and make sure that filtering is taking place.

Now let’slook at the effects of the filter in the frequency domain. Set the oscilloscope to display the FFT of the input. The FFT, Fast Fourier Transform, will take a signal in the time domain and display it in the frequency domain. Look at the spectrum while using the [1,0,...,0] test coefficients and an arbitrary sine wave. Include a screenshot in your write up. Next, generate Gaussian White Noise using the function generator and connect it to the input of the board. White noise has power at all frequencies, so if we take the FFT of the output of the filter, we should get the frequency response of the filter. Does the actual frequency response look like the one generated by Matlab? Take a screenshot and submit it in the write up.

Part iv: extra credit

For extra credit, test your filter implementation with a new set of coefficients. Create a new set of filter coefficients that make up a new filter type. You may even increase the number of taps in the filter for more accurate results.

In order to generate a new set of coefficients, open up your favorite copy of matlab and use either the remez command. Newer versions of Matlab suggest you use FIRPM . If you are unsure how to use the command, type help remez to give you the syntax and usage. You may also try to use the Filter Design Toolbox for a more user friendly interface.

Once you have your coefficients, you must convert them to twos-complement Q-15 numbers. You may use twocomplement.m in order to do the conversion for you.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Microcontroller and embedded systems laboratory. OpenStax CNX. Feb 11, 2006 Download for free at http://cnx.org/content/col10215/1.29
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Microcontroller and embedded systems laboratory' conversation and receive update notifications?

Ask