//IIRinverse.C  Inverse IIR Filter
 
#include "bp2000.cof"       	     //BP @ 2 kHz coefficient file
short dly[stages][2] = {0};        //delay samples per stage
short out_type = 1;		     //type of output for slider
short a0, a1, a2, b1, b2;          //coefficients  

interrupt void c_int11()	     //ISR
{
 short i, input, input1;
 int un1, yn1, un2, input2, yn2;
 
 input1 = input_sample();    	     //input to 1st stage
 input = input1;         	     //original input	
 for(i = 0; i < stages; i++) 	     //repeat for each stage
  {
   a1 = ((a[i][1]*dly[i][0])>>15); //a1*u(n-1)
   a2 = ((a[i][2]*dly[i][1])>>15); //a2*u(n-2)          
   b1 = ((b[i][0]*dly[i][0])>>15); //b1*u(n-1)
   b2 = ((b[i][1]*dly[i][1])>>15); //b2*u(n-2)
   un1 = input1 - b1 - b2;
   a0=((a[i][0]*un1)>>15);
 	
   yn1 = a0 + a1 + a2;             //stage output        
   input1 = yn1; 			     //intermediate out->in next stage
   dly[i][1] = dly[i][0];          //update delays u(n-2) = u(n-1)
   dly[i][0] = un1;                //update delays u(n-1) = u(n)
  }
 input2 = yn1;                     //out forward=in reverse filter
 
 for(i = stages; i > 0; i--)       //for inverse IIR filter
  {
   a1 = ((a[i][1]*dly[i][0])>>15); //a1u(n-1)
   a2 = ((a[i][2]*dly[i][1])>>15); //a2u(n-2)
   b1 = ((b[i][0]*dly[i][0])>>15); //b1u(n-1)
   b2 = ((b[i][1]*dly[i][1])>>15); //b2u(n-2)
   un2 = input2 - a1 - a2;         
   yn2 = (un2 + b1 + b2);          
   input2 = (yn2<<15)/a[i][0];     //intermediate out->in next stage
  }
 if(out_type == 1)  		     //if slider in position 1
    output_sample(input);          //original input signal
 if(out_type == 2)            
    output_sample(yn1);            //output of forward filter
 if(out_type == 3)           
    output_sample(yn2 >>6);        //output of inverse filter
 return;				     //return from ISR
}

void main() 
{
 comm_intr();                      //init DSK, codec, McBSP
 while(1);			           //infinite loop	 		
}