IIR FIlter and add tone
Filters and then corrupts the signal with a overlaid tone. Your own coefficients "IIRLPF.h" can be made using fdatool in MATLAB.
//iirFilter.c
// Include the filter coefficients and corresponding variables
#include "IIRLPF.h"
#define A 1 //The amplitude of added wave
#define FREQ 1000 //The frequency of the sine wave in Hz
// The intermediate values in the Direct Form II filter
float delay_w[MWSPT_NSEC][3];
// delay_w[i][j] <=> w_i(n-j), j=0,1,2
// i is the section, j the delay
float sectionOut; // yk[n] <=> sectionout
interrupt void isr() //Interrupt function t=125us, f = 8kHz
{
short i; // i loops through the MWSPT_NSEC number of sections
int period = 8000 / FREQ;
float rad = FREQ * 2 * pi;
int j = 0;
// Do the filtering if DIP switch 1 up
if (get_DIP1() == 1) {
// In the first section, we read in the x-value, apply the first stage gain
sectionOut = NUM[0][0] * get_sample();
for (i=1; i<MWSPT_NSEC; i++) { // Loop through all the sections
// Get the new delay_w[0];
delay_w[i][0] = sectionOut - DEN[i][1]*delay_w[i][1] - DEN[i][2]*delay_w[i][2];
// Get the output of this section
sectionOut = NUM[i][0]*delay_w[i][0] + NUM[i][1]*delay_w[i][1] + NUM[i][2]*delay_w[i][2];
// Delay the w's for the next interrupt
delay_w[i][2] = delay_w[i][1];
delay_w[i][1] = delay_w[i][0];
}
//Add a tone to sectionOut
sectionOut = sectionOut + A*sin(rad*j); //Add a sine wave of freq rad to sectionOut
j = (j + 1) % period; //Increment the sine wave counter
// Apply the gain, convert to short and send out
send_output((short)(2 * sectionOut));
// Gain of 2 chosen heuristically for speech from PC
} else { // If DIP switch 1 down, == 0, then just pass through signal.
send_output(get_sample());
}
return; // return from interrupt
}
void main()
{
short i,j;
for (i=0; i<MWSPT_NSEC; i++)
for (j=0; j<3; j++)
delay_w[i][j] = 0; // init intermediate array
init_all(); // init all
while(1); // infinite loop
}