/***************************************************************** * iir1fx.c - C program for first-order IIR filtering using * fixed-point C code *****************************************************************/ #include #include #include void main() { /*************************************************************** * Define variable arrays, define and clear variables ***************************************************************/ int xn = 0; /* x(n) from xn.dat file */ long yn = 0; /* float yn = 0.0; */ int alpha = 8192; /* alpha = 0.25*32768 */ int alpha1 = 24576; /* alpha1 = 1 - alpha = 0.75*32768 */ /*************************************************************** * Declare file pointers ***************************************************************/ FILE *xn_in; /* file pointer of x(n) */ FILE *yn_out; /* file pointer of y(n) */ xn_in = fopen("xn.dat","r"); /* open file for input x(n) */ yn_out = fopen("yn.dat","w"); /* open file for output y(n) */ /*************************************************************** * Start of main program ***************************************************************/ while( (fscanf(xn_in,"%d",&xn)) != EOF) { /* read in x(n) from data file and processing it */ /************************************************************* * IIR filtering: * y(n) = (1-alpha)*y(n-1)+alpha*x(n) *************************************************************/ /*yn = alpha1*yn+ alpha*(float)xn; */ yn = (((long)(int)alpha1*(long)(int)yn)>>15) +(((long)(int)alpha*(long)(int)xn)>>15); fprintf(yn_out,"%d\n",(int)(yn)); /* rounding y(n) to integer and write it to output file */ } printf("Finish"); fclose(xn_in); fclose(yn_out); /* close all opened files */ }