/***************************************************************** * ale_floatpt.c - Floating-point C program of ALE ****************************************************************** * System configuration: * * d(n) --------------------------- * | / | * d(n-1)|----------------| out(n) V * ---->| ALE (LMS-FIR) |-----> |+|-----> e(n) * |----------------| - | * /_______________________| * ****************************************************************** * System simulation configuration: * * d(n) is the input data from data file "in.dat" * out(n) is the output data to data file "out.dat" * *****************************************************************/ #include #include #include void main() { /***************************************************************** * Define variable arrays, define and clear variables *****************************************************************/ #define NL 16 /* number of FIR filter's tap */ float yn = 0.0; /* y(n), output from the filter */ float xn; float xnbuf[NL]; float w[NL]; int i,j,k; float mu = 0.01; float mu_err; float dn = 0; float err; /***************************************************************** * Declare file pointers *****************************************************************/ FILE *xn_in; /* file pointer of x(n) */ FILE *yn_out; /* file pointer of y(n) */ FILE *w_out; FILE *err_out; xn_in = fopen("in.dat","r"); /* open file for input x(n) */ yn_out = fopen("out.dat","w"); /* open file for output y(n) */ w_out = fopen("w.dat","w"); /* open file for weight w[0] */ err_out = fopen("err.dat","w"); /* open file for error */ /*************************************************************** * Start of main program ***************************************************************/ for(k=0;k0;i--) { xnbuf[i]=xnbuf[i-1]; /* refresh signal buffer */ } xnbuf[0]=dn; /* delayed version of dn */ dn = xn; /* delayed by 1 */ /************************************************************* * FIR filtering: * y(n) = sum[x(n-i)*w(i)] for i = 0 to NL-1 *************************************************************/ yn = 0.0; for (j=0; j< NL; j++) { yn += xnbuf[j] * w[j]; /* FIR filtering */ } err = dn-yn; /* e(n) = d(n) - y(n) */ mu_err = mu*err; /* uen = mu * e(n) */ /* update filter coefficients */ for (k=0; k< NL; k++) { w[k]=w[k]+mu_err*xnbuf[k]; /* LMS update */ } fprintf(yn_out,"%f\n",yn); /* save output y(n) */ fprintf(err_out,"%f\n",err); /* save error signal e(n) */ fprintf(w_out, "%f\n",w[0]); /* save trajectory of w[0] */ } printf("Finish"); /* complete filtering */ fcloseall(); /* close all opened files */ }