DSPRelated.com
Code

Dual DIP-switched IIR Filter

Ron April 1, 2011 Coded in C for the TI C67x

Allows two IIR filters to be switched between in real time with DIP1 on the C6X.

DIP1 down = High Pass. DIP1 up = Low Pass.

"IIRLPF.h" and "IIRHPF.h"can be created using fdatool in MATLAB or use your own.

//iirFilterSwitch.c  

// Include the filter coefficients and corresponding variables
#include "IIRLPF.h"
#include "IIRHPF.h"

// The intermediate values in the Direct Form II filter
float delay_w1[MWSPT_NSEC1][3];		
float delay_w2[MWSPT_NSEC2][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
{	
  short i; 			// i loops through the MWSPT_NSEC number of sections

  //  Use the LPF 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 = NUM1[0][0] * get_sample();

	for (i=1; i<MWSPT_NSEC1; i++) { // Loop through all the sections
	
		// Get the new delay_w1[0];
		delay_w1[i][0] = sectionOut - DEN1[i][1]*delay_w1[i][1] - DEN1[i][2]*delay_w1[i][2];

		// Get the output of this section		
		sectionOut = NUM1[i][0]*delay_w1[i][0] + NUM1[i][1]*delay_w1[i][1] + NUM1[i][2]*delay_w1[i][2];
	
		// Delay the w's for the next interrupt
		delay_w1[i][2] = delay_w1[i][1];
		delay_w1[i][1] = delay_w1[i][0];

	}

	// 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 use HPF

	sectionOut = NUM2[0][0] * get_sample();

	for (i=1; i<MWSPT_NSEC2; i++) { // Loop through all the sections
	
		// Get the new delay_w2[0];
		delay_w2[i][0] = sectionOut - DEN2[i][1]*delay_w2[i][1] - DEN2[i][2]*delay_w2[i][2];

		// Get the output of this section		
		sectionOut = NUM2[i][0]*delay_w2[i][0] + NUM2[i][1]*delay_w2[i][1] + NUM2[i][2]*delay_w2[i][2];
	
		// Delay the w's for the next interrupt
		delay_w2[i][2] = delay_w2[i][1];
		delay_w2[i][1] = delay_w2[i][0];

	}

	// Apply the gain, convert to short and send out
	send_output((short)(2 * sectionOut));
	// Gain of 2 chosen heuristically for speech from PC

  }

 	return;							// return from interrupt
}

void main()
{
	short i,j;

 	for (i=0; i<MWSPT_NSEC1; i++)
 		for (j=0; j<3; j++) 
			delay_w1[i][j] = 0;      // init intermediate array

 	for (i=0; i<MWSPT_NSEC2; i++)
 		for (j=0; j<3; j++) 
			delay_w2[i][j] = 0;      // init intermediate array
			
	init_all();                  	// init all
	while(1);    			// infinite loop
}