Circular Buffer FIR Filter
This FIR filter implements a circular buffer. Your own coefficients in yourfiltercoeffs.h can be created using fdatool in MATLAB.
DIP1 down = Unfiltered
DIP1 up = Filtered
// These coefficients are used to filter
#include "yourfiltercoeffs.h"
// scale factor, S_h = 2^K.
#define K 15
int filterout = 0; // In fixed-point implementation, the accumulator is 32-bit int
short delay[LENGTH]; // Short is the 16-bit integer variable
interrupt void isr() // Interrupt function
{
short i;
int Sx = 1;
// Do the filtering as per fixfilt if DIP switch 1 up
if (get_DIP1() == 1) {
// Direct-Form FIR
delay[0] = Sx * get_sample(); // input for filter
filterout = hMax[0] * delay[0]; // Set up filter sum
// Notice, C-arrays go from 0..LENGTH-1
for (i = LENGTH-1; i > 0; i--){ // Get sum of products
filterout += hMax[i] * delay[i];
delay[i] = delay[i-1]; // Renew input array
}
filterout = (filterout>>K); //Move into the lower 16 bits, reverses scaling
//Sign extension carries the upper bit down if negative
send_output(filterout); // output for filter
} else { // If DIP switch 1 down, == 0, then just pass through signal.
send_output(get_sample());
}
return; // return from interrupt
}
void main()
{
short i;
for (i=0; i<= LENGTH-1; i++) {
delay[i] = 0; // init filter processing array
}
init_all(); // init all
while(1); // infinite loop
}