/***************************************************************** * iir_floatpt.c- C program for IIR filtering, cascade of three * direct-form II sections in floating-point * Section 7.7.2 ****************************************************************** * System configuration: * * in(n) |----------------| out(n) * ---->| Bandpass filter|-----> * |----------------| * ****************************************************************** * System simulation configuration: * * in(n) is the input data from data file "in.dat" * out(n) is the output data to data file "out.dat" * *****************************************************************/ #include #include #include #include "iircascade_sp.h" void main() { /*************************************************************** * Define variable arrays, define and clear variables ***************************************************************/ const int section = 3; const int tap = 3; float out = 0.0; /* y(n), output from IIR filter */ float xn; float delay[9] = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}; float gain = 0.004204816817972; int j; /*************************************************************** * Declare file pointers ***************************************************************/ FILE *xn_in; /* file pointer of x(n) */ FILE *yn_out; /* file pointer of y(n) */ xn_in = fopen("in.dat","r"); /* open file for input x(n) */ yn_out = fopen("out.dat","w"); /* open file for output y(n) */ /*************************************************************** * Start of main program ***************************************************************/ while ((fscanf(xn_in,"%f",&xn)) != EOF) { /* read in x(n) from data file and processing it */ out=xn*gain; /************************************************************* * IIR filtering: (feedback follow by feedforward) and repeat * for three sections. *************************************************************/ for (j=0; j< section; j++) { /* feedback section */ out=out-(delay[1+(j*section)]*(real32_T)DEN[j][1]); delay[(j*section)]=out-(delay[2+(j* section)]*(real32_T)DEN[j][2]); /* feedforward section */ out=delay[(j* section)]+(delay[1+(j* section)]*(real32_T)NUM[j][1]); out=out+(delay[2+(j*section)]*(real32_T)NUM[j][2]); /* refreshes signal buffer */ delay[2+(j*section)] = delay[1+(j*section)]; delay[1+(j*section)] = delay[(j*section)]; } fprintf(yn_out,"%f\n",out); } printf("Finish"); /* complete filtering */ fcloseall(); /* close all opened files */ }