/***************************************************************** * ale_fixpt.c - Fixed-point C program for implementing ALE ****************************************************************** * System configuration: * * d(n) --------------------------- * | / | * d(n-1)|----------------| out(n) V * ---->| ALE (LMS-FIR) |-----> |+|-----> * |----------------| - | * /_______________________| * * * d(n) is the input data from data file "in_int.dat" *****************************************************************/ #include #include #include void main() { /*************************************************************** * Define variable arrays, define and clear variables ***************************************************************/ #define NL 16 /* length of FIR filter */ long yn = 0; /* y(n), output from filter */ int xn; int xnbuf[NL]; int w[NL]; int i,j,k; int mu = 338; /* 33(0.001); 338(0.01); 655(0.02) */ int mu_err; int dn = 0; int err; /*************************************************************** * Declare file pointers ***************************************************************/ FILE *xn_in; /* file pointer of x(n) */ FILE *yn_out; /* file pointer of y(n) */ FILE *w_out; /* file pointer of w(0) */ FILE *err_out; /* file pointer of e(n) */ xn_in = fopen("in_int.dat","r"); /* open file for input x(n) */ yn_out = fopen("out_int.dat","w");/* open file for output y(n)*/ w_out = fopen("w_int.dat","w"); /* coefficient file */ err_out = fopen("err_int.dat","w");/* error signal file */ /*************************************************************** * Start of main program ***************************************************************/ for(k=0;k0;i--) { xnbuf[i]=xnbuf[i-1]; /* refresh signal buffer */ } xnbuf[0]=dn; /* elayed version of dn */ dn = xn; /* delay by 1 */ yn = 0; for (j=0; j< NL; j++) /* FIR filtering */ { yn += ((long)(int)xnbuf[j] * (long)(int)w[j])>>15; } err = dn-yn; /* e(n) = d(n) - y(n) */ mu_err = (int)(((long)(int)mu*(long)(int)err)>>15); /* update filter coeffificient */ for (k=0; k< NL; k++) { w[k]=w[k]+(int)(((long)(int)mu_err*(long)(int)xnbuf[k])>>15); } fprintf(yn_out,"%d\n",(int)(yn)); fprintf(w_out,"%d\n",(int)w[0]); fprintf(err_out,"%d\n",(int)err); } printf("Finish"); /* complete filtering */ fcloseall(); /* close all opened files */ }