Page 357 - Programming Microcontrollers in C
P. 357
342 Chapter 6 Large Microcontrollers
In this expression, it seems possible that the x subscript can have a
negative value. Actually, such a case is not possible because a nega
tive subscript implies that data is used before it is available. Positive
subscripts correspond to time that has already passed. Values of x
will be zero for negative subscripts. If the kth sample corresponds to
“now,” increasing values of i will get older samples of x. This op
eration can lead to a little problem in creating the code for the
convolution. The looping construct within the assembly program
above selects the different values of i in the above equation. The
coefficients h are placed in memory in successive order so that an
i
increase in the value of i will select the correct next coefficient.
However, if the data values x were placed in memory as one would
naturally expect, the newest value of data would be at the current
array index, and old values of the data would be at lesser index val
ues. This arrangement will not work correctly. The data must be placed
in the array backwards in order to get the convolution to work. Older
data must be at higher indices than the current data sample. Also,
when filling the array initially, the program should start at the top of
the array rather than the bottom. The routine listed below will store
the data properly in the array.
int data[64];
int handle_data(char new_data)
{
static int i=63;
if(i<0)
i=63;
data[i—]=new_data;
return i+1;
}
Listing 6-12: Convenient Data Storage For DSP Use
This routine is integrated into the code shown in Listing 6-11
and used to test the circular convolution. This resultant program is
shown in Listing 6-13. In this case, the variables data and coef
are moved outside of the function main() to make them global.