I have the following loop in part of my code, which is supposed to traverse an array backwards. When the index reaches 0, it resets itself and the loop starts over from the top. I've been staring at it so long I am probably just missing the obvious, so I'm hoping someone with fresh eyes can spot why this is happening.
The offending loop is in bold, I include the rest for the sake of completeness but the first loop appears to be doing its job (or at least, it's not obviously not doing it's job, I'll know for sure when I can test the whole thing). If anyone is interested, this is the main part of a recursive digital low-pass filter.
Code:
void filter_signal(double *signal, double *tempfiltered, double *filtered, double *dcof, int *ccof, double scale, uint64_t order, uint64_t length)
{
uint64_t i,p,index;
i = 0;
p = 0;
for (i=0; i<order; i++) //pad the output signal
{
tempfiltered[i] = signal[0];
}
for (i=0; i<length; i++)
{
tempfiltered[i] = ccof[0]*scale*signal[i];
for (p=1; p<=order; p++)
{
if (i-p < 0)
{
index = 0;
}
else
{
index = i-p;
}
tempfiltered[i] += ccof[p]*scale*signal[index] - dcof[p]*tempfiltered[index];
}
}
for (i=length-1; i>length-order-1; i--)
{
filtered[i] = tempfiltered[length-1];
}
for (i=0; i<length; i++)
{
printf("length is %"PRIu64" and i is %"PRIu64" and we are indexing position %"PRIu64"\n",length, i, length - i);
fflush(stdout);
filtered[length-i-1] = ccof[0]*scale*tempfiltered[length-i-1];
for (p=1; p<=order; p++)
{
if (length-1-i+p > length-1)
{
index = length-1;
}
else
{
index = i+p;
}
filtered[length-i-1] += ccof[p]*scale*tempfiltered[index] - dcof[p]*filtered[index];
}
}
}