/***************************************************************** * iir1.c - C program for first-order IIR filtering ****************************************************************** * System configuration: * * * x(n) |----------------| y(n) * ---->| Lowpass filter |-----> * |----------------| * ****************************************************************** * System simulation configuration: * * x(n) is the input data from data file "xn.dat" * y(n) is the output data to data file "yn.dat" * *****************************************************************/ #include #include #include void main() { /***************************************************************** * Define variable arrays, define and clear variables *****************************************************************/ int xn = 0; /* x(n) from xn.dat file */ float yn = 0.0; /* y(n), output from IIR filter */ float alpha = 0.25; /* alpha = 0.25 */ float alpha1 = 0.75; /* alpha1 = 1 - alpha = 0.75 */ /***************************************************************** * 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; fprintf(yn_out,"%d\n",(int)(yn+0.5)); /* rounding y(n) to integer and write it to output file */ } printf("Finish"); fclose(yn_out); fclose(xn_in); /* close all opened files */ }