<< Chapter < Page | Chapter >> Page > |
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.
/* TIMER A */
TACCR0 = 900;
TACCTL1 = OUTMOD_4;
TACTL |= TASSEL_2+MC_1;
Given these timing settings, what is the sampling rate?
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?
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.
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.
Notification Switch
Would you like to follow the 'Microcontroller and embedded systems laboratory' conversation and receive update notifications?