<< Chapter < Page | Chapter >> Page > |
This module explains how to implement an FIR filter in the Direct-Form structure. It explains how data is stored as samples are received and how to use a circular buffer in the algorithm.
The difference equation for an N -order FIR filter is given by:
For each new input sample, x ( n ), the filter produces an output y ( n ). To calculate the new output sample, the input must be convolved with the FIR filter impulse response, h ( n ). Notice that the summation requires the input samples starting with the most recent sample, x ( n ) through the N -1 most recent samples, through x ( n - N -1). In a DSP system, these previous samples must be saved in an array in memory. Usually there will be one new sample received, x ( n ), and any previous samples that were received must be saved in memory.
Since the algorithm only needs the previous N -1 samples to be saved there must be a way for the algorithm to update the array when a new sample is input. This can be accomplished in two ways. The first way is to shift all the samples in the array by copying them to the shifted location and then storing the new value at the beginning of the array. This is a first-in-first-out (FIFO) buffer. If this method is used then the FIR algorithm is given by
Figure 1 shows a diagram of the implementation of the FIR filter using the array shifting method. In the diagram the "*" indicates multiplication. The new value is shifted into the x (0) spot and all the others are shifted to the right.
The following pseudo-code shows how to implement the FIR filter using the FIFO type buffer. In the code the following definitions are used:
N
- number of coefficients
h(n)
- filter coefficients, n = 0...N-1
x(n)
- stored input data, n = 0...N-1
input_sample
- variable that contains the newest input sample
// Shift in the new sample. Note the data is shifted starting at the end.
for (i=N-2; i>=0; i--){
x[i+1]= x[i];}
x[0]= input_sample
// Filter the dataynew = 0;
for(i=0;i<N;i++){
ynew = ynew + h(i)*x(i);}
In the example above, the data in the FIFO buffer is shifted every time a new sample is received. This could lead to inefficient code. It is better to do processing on blocks of data. For instance, you could use a DMA to copy a block of data from one place to another and free up the processor from doing the work.
One solution could be to make a FIFO buffer that is twice as large as the data that is needed to store. When this is done, shifting of the data can be done after a block of data is received.
The following figures show the data buffer as data is being received and stored. Figure 2 shows the buffer when the first new value is received. Of course the buffer would be zeroed before processing begins. The shaded area shows the part of the buffer that is used in the algorithm.
Notification Switch
Would you like to follow the 'Dsp lab with ti c6x dsp and c6713 dsk' conversation and receive update notifications?