I have been coding a very rudimentary LMS algorithm for Echo cancellation but I am stuck since the code is not working. I have kept the code to be very simple and yet I am not able to identify where the code is going wrong. Please help me to identify the error in the code. FILE *ref; and FILE *des; contains the same data and ideally when the algorithm converges,there should not be any voice in the FILE *final file (this is what I expect since there is only echo and no other side voice is assumed),but still I am getting the same voice data as that of the *ref and *des file...there is no echo suppression. ************************************************************* #include <stdio.h> #include <math.h> #include <stdlib.h> #define beta 10 //convergence rate #define N 40//order of filter #define NS 40 //number of samples #define Fs 16000 //sampling frequency short int buffer[NS]; short int buffer1[NS]; short int ec_buffer[NS]; int main() { long I, T; short int D, Y, E; short int temp = 0; short int W[N+1] = {0,}; //Impulse Response of Adaptive FIR filter short int X[N+1] = {0,}; //Input Signal of Adaptive FIR filter int result; FILE *ref; FILE *des; FILE *final; //FILE *coeff; //File with Echo des = fopen ("des.raw","rb"); if (des==NULL) { printf ("fopen des failedn"); fclose (des); } //File without echo ref = fopen ("ref.raw","rb"); if (ref==NULL) { printf ("fopen ref failedn"); fclose (ref); } //Ouput file - E = (des.Raw - ref.raw) final = fopen ("final","ab+"); if (ref==NULL) { printf ("fopen final failedn"); fclose (final); } /*coeff = fopen ("coeff.txt","w+"); if (ref==NULL) { printf ("fopen coeff failedn"); fclose (final); }*/ while( !feof( ref ) ) { result = fread(buffer,sizeof(short int),NS,ref); result = fread(buffer1,sizeof(short int),NS,des); for (T = 0; T < NS; T++) //start adaptive algorithm { Y = 0; //filter’output set to zero X[0] = buffer[T]; D = buffer1[T]; for (I = 0; I < N; I++) { //Y = (Y + (W[I] * X[I])); Y = Y + (int)(((long)(W[I] * (X[I])))>>15); //calculate filter output }//Loop End E = D-Y; //calculate error signal temp = (int)(((long)(beta*E))>>15); //temp = beta*E; for (I = N; I > 0; I--) { W[I+1] = (W[I] + (int)((long)(X[I]*temp))>>15); //update filter coefficients //W[I+1] = (W[I] + (X[I]*temp)); //if (I != 0) //(X[I] = X[I-1]); } for(I=N;I>0;I--) { if (I != 0) (X[I] = X[I-1]); } ec_buffer[T] = E; } result = fwrite(ec_buffer,sizeof(short int),NS,final); } result = fclose(final); fclose (ref); fclose (des); return 0; } --------------------------------------- Posted through http://www.DSPRelated.com
LMS algorithm in fixed point arithmetic
Started by ●April 28, 2015
Reply by ●April 28, 20152015-04-28
>I have been coding a very rudimentary LMS algorithm for Echo >cancellation >but I am stuck since the code is not working. > >I have kept the code to be very simple and yet I am not able to identify >where the code is going wrong. > >Please help me to identify the error in the code. > >FILE *ref; and FILE *des; contains the same data and ideally when the >algorithm converges,there should not be any voice in the FILE *final >file >(this is what I expect since there is only echo and no other side voice >is >assumed),but still I am getting the same voice data as that of the *ref >and *des file...there is no echo suppression. > >************************************************************* >#include <stdio.h> >#include <math.h> >#include <stdlib.h> > >#define beta 10 //convergence rate >#define N 40//order of filter >#define NS 40 //number of samples >#define Fs 16000 //sampling frequency > > >short int buffer[NS]; >short int buffer1[NS]; >short int ec_buffer[NS]; > > > >int main() >{ > long I, T; > short int D, Y, E; > short int temp = 0; > short int W[N+1] = {0,}; //Impulse Response of Adaptive FIR filter > short int X[N+1] = {0,}; //Input Signal of Adaptive FIR filter > int result; > FILE *ref; > FILE *des; > FILE *final; > //FILE *coeff; > > //File with Echo > des = fopen ("des.raw","rb"); > if (des==NULL) > { > printf ("fopen des failedn"); > fclose (des); > } > //File without echo > ref = fopen ("ref.raw","rb"); > if (ref==NULL) > { > printf ("fopen ref failedn"); > fclose (ref); > } > //Ouput file - E = (des.Raw - ref.raw) > final = fopen ("final","ab+"); > if (ref==NULL) > { > printf ("fopen final failedn"); > fclose (final); > } > /*coeff = fopen ("coeff.txt","w+"); > if (ref==NULL) > { > printf ("fopen coeff failedn"); > fclose (final); > }*/ > while( !feof( ref ) ) > { > result = fread(buffer,sizeof(short int),NS,ref); > result = fread(buffer1,sizeof(short int),NS,des); > > > for (T = 0; T < NS; T++) //start adaptive algorithm > { > > Y = 0; //filter’output set to zero > X[0] = buffer[T]; > > D = buffer1[T]; > > for (I = 0; I < N; I++) > { > //Y = (Y + (W[I] * X[I])); > > Y = Y + (int)(((long)(W[I] * (X[I])))>>15); //calculate filter >output > }//Loop End > > E = D-Y; //calculate error signal > > temp = (int)(((long)(beta*E))>>15); > //temp = beta*E; > for (I = N; I > 0; I--) > { > W[I+1] = (W[I] + (int)((long)(X[I]*temp))>>15); //update filter >coefficients > //W[I+1] = (W[I] + (X[I]*temp)); > //if (I != 0) > //(X[I] = X[I-1]); > } > for(I=N;I>0;I--) > { > if (I != 0) > (X[I] = X[I-1]); > } > ec_buffer[T] = E; > > } > result = fwrite(ec_buffer,sizeof(short int),NS,final); > > } > result = fclose(final); > fclose (ref); > fclose (des); > return 0; >} > > >--------------------------------------- >Posted through http://www.DSPRelated.comReally, what's your question? "Hey find my errors in my design" is not a question, thats doing all the work for you......... --------------------------------------- Posted through http://www.DSPRelated.com
Reply by ●April 28, 20152015-04-28
On Tuesday, April 28, 2015 at 7:42:25 AM UTC-5, pscall4ram wrote:> I have been coding a very rudimentary LMS algorithm for Echo cancellation > but I am stuck since the code is not working. > > I have kept the code to be very simple and yet I am not able to identify > where the code is going wrong. > > Please help me to identify the error in the code. > > FILE *ref; and FILE *des; contains the same data and ideally when the > algorithm converges,there should not be any voice in the FILE *final file > (this is what I expect since there is only echo and no other side voice is > assumed),but still I am getting the same voice data as that of the *ref > and *des file...there is no echo suppression. > > ************************************************************* > #include <stdio.h> > #include <math.h> > #include <stdlib.h> > > #define beta 10 //convergence rate > #define N 40//order of filter > #define NS 40 //number of samples > #define Fs 16000 //sampling frequency > > > short int buffer[NS]; > short int buffer1[NS]; > short int ec_buffer[NS]; > > > > int main() > { > long I, T; > short int D, Y, E; > short int temp = 0; > short int W[N+1] = {0,}; //Impulse Response of Adaptive FIR filter > short int X[N+1] = {0,}; //Input Signal of Adaptive FIR filter > int result; > FILE *ref; > FILE *des; > FILE *final; > //FILE *coeff; > > //File with Echo > des = fopen ("des.raw","rb"); > if (des==NULL) > { > printf ("fopen des failedn"); > fclose (des); > } > //File without echo > ref = fopen ("ref.raw","rb"); > if (ref==NULL) > { > printf ("fopen ref failedn"); > fclose (ref); > } > //Ouput file - E = (des.Raw - ref.raw) > final = fopen ("final","ab+"); > if (ref==NULL) > { > printf ("fopen final failedn"); > fclose (final); > } > /*coeff = fopen ("coeff.txt","w+"); > if (ref==NULL) > { > printf ("fopen coeff failedn"); > fclose (final); > }*/ > while( !feof( ref ) ) > { > result = fread(buffer,sizeof(short int),NS,ref); > result = fread(buffer1,sizeof(short int),NS,des); > > > for (T = 0; T < NS; T++) //start adaptive algorithm > { > > Y = 0; //filter'output set to zero > X[0] = buffer[T]; > > D = buffer1[T]; > > for (I = 0; I < N; I++) > { > //Y = (Y + (W[I] * X[I])); > > Y = Y + (int)(((long)(W[I] * (X[I])))>>15); //calculate filter output > }//Loop End > > E = D-Y; //calculate error signal > > temp = (int)(((long)(beta*E))>>15); > //temp = beta*E; > for (I = N; I > 0; I--) > { > W[I+1] = (W[I] + (int)((long)(X[I]*temp))>>15); //update filter > coefficients > //W[I+1] = (W[I] + (X[I]*temp)); > //if (I != 0) > //(X[I] = X[I-1]); > } > for(I=N;I>0;I--) > { > if (I != 0) > (X[I] = X[I-1]); > } > ec_buffer[T] = E; > > } > result = fwrite(ec_buffer,sizeof(short int),NS,final); > > } > result = fclose(final); > fclose (ref); > fclose (des); > return 0; > } > > > --------------------------------------- > Posted through http://www.DSPRelated.comYour declarations tell me that you have not really studied the LMS algorithm implementation. Your NS is much too short. You will probably need on the order of 1000 samples. Get Widrow's book on adaptive signal processing. Maurice Givens
Reply by ●April 30, 20152015-04-30
>On Tuesday, April 28, 2015 at 7:42:25 AM UTC-5, pscall4ram wrote: >> I have been coding a very rudimentary LMS algorithm for Echo >cancellation >> but I am stuck since the code is not working. >> >> I have kept the code to be very simple and yet I am not able to >identify >> where the code is going wrong. >> >> Please help me to identify the error in the code. >> >> FILE *ref; and FILE *des; contains the same data and ideally when the >> algorithm converges,there should not be any voice in the FILE *final >file >> (this is what I expect since there is only echo and no other side >voice is >> assumed),but still I am getting the same voice data as that of the >*ref >> and *des file...there is no echo suppression. >> >> ************************************************************* >> #include <stdio.h> >> #include <math.h> >> #include <stdlib.h> >> >> #define beta 10 //convergence rate >> #define N 40//order of filter >> #define NS 40 //number of samples >> #define Fs 16000 //sampling frequency >> >> >> short int buffer[NS]; >> short int buffer1[NS]; >> short int ec_buffer[NS]; >> >> >> >> int main() >> { >> long I, T; >> short int D, Y, E; >> short int temp = 0; >> short int W[N+1] = {0,}; //Impulse Response of Adaptive FIR filter >> short int X[N+1] = {0,}; //Input Signal of Adaptive FIR filter >> int result; >> FILE *ref; >> FILE *des; >> FILE *final; >> //FILE *coeff; >> >> //File with Echo >> des = fopen ("des.raw","rb"); >> if (des==NULL) >> { >> printf ("fopen des failedn"); >> fclose (des); >> } >> //File without echo >> ref = fopen ("ref.raw","rb"); >> if (ref==NULL) >> { >> printf ("fopen ref failedn"); >> fclose (ref); >> } >> //Ouput file - E = (des.Raw - ref.raw) >> final = fopen ("final","ab+"); >> if (ref==NULL) >> { >> printf ("fopen final failedn"); >> fclose (final); >> } >> /*coeff = fopen ("coeff.txt","w+"); >> if (ref==NULL) >> { >> printf ("fopen coeff failedn"); >> fclose (final); >> }*/ >> while( !feof( ref ) ) >> { >> result = fread(buffer,sizeof(short int),NS,ref); >> result = fread(buffer1,sizeof(short int),NS,des); >> >> >> for (T = 0; T < NS; T++) //start adaptive algorithm >> { >> >> Y = 0; //filter'output set to zero >> X[0] = buffer[T]; >> >> D = buffer1[T]; >> >> for (I = 0; I < N; I++) >> { >> //Y = (Y + (W[I] * X[I])); >> >> Y = Y + (int)(((long)(W[I] * (X[I])))>>15); //calculate filter >output >> }//Loop End >> >> E = D-Y; //calculate error signal >> >> temp = (int)(((long)(beta*E))>>15); >> //temp = beta*E; >> for (I = N; I > 0; I--) >> { >> W[I+1] = (W[I] + (int)((long)(X[I]*temp))>>15); //update filter >> coefficients >> //W[I+1] = (W[I] + (X[I]*temp)); >> //if (I != 0) >> //(X[I] = X[I-1]); >> } >> for(I=N;I>0;I--) >> { >> if (I != 0) >> (X[I] = X[I-1]); >> } >> ec_buffer[T] = E; >> >> } >> result = fwrite(ec_buffer,sizeof(short int),NS,final); >> >> } >> result = fclose(final); >> fclose (ref); >> fclose (des); >> return 0; >> } >> >> >> --------------------------------------- >> Posted through http://www.DSPRelated.com > >Your declarations tell me that you have not really studied the LMS >algorithm implementation. Your NS is much too short. You will probably >need on the order of 1000 samples. Get Widrow's book on adaptive signal >processing. > >Maurice GivensThanks for the reply Maurice,I tried increasing the NS from 1000 to 14000 but still I could not get the desired results. Most of the implementations of the LMS algorithms are in floating point, but here I am trying in fixed point. I am not sure where is this algorithm implementation going wrong...Please help to check this implementation. Regards, Ram --------------------------------------- Posted through http://www.DSPRelated.com
Reply by ●May 2, 20152015-05-02
>>On Tuesday, April 28, 2015 at 7:42:25 AM UTC-5, pscall4ram wrote: >>> I have been coding a very rudimentary LMS algorithm for Echo >>cancellation >>> but I am stuck since the code is not working. >>> >>> I have kept the code to be very simple and yet I am not able to >>identify >>> where the code is going wrong. >>> >>> Please help me to identify the error in the code. >>> >>> FILE *ref; and FILE *des; contains the same data and ideally when >the >>> algorithm converges,there should not be any voice in the FILE *final >>file >>> (this is what I expect since there is only echo and no other side >>voice is >>> assumed),but still I am getting the same voice data as that of the >>*ref >>> and *des file...there is no echo suppression. >>> >>> ************************************************************* >>> #include <stdio.h> >>> #include <math.h> >>> #include <stdlib.h> >>> >>> #define beta 10 //convergence rate >>> #define N 40//order of filter >>> #define NS 40 //number of samples >>> #define Fs 16000 //sampling frequency >>> >>> >>> short int buffer[NS]; >>> short int buffer1[NS]; >>> short int ec_buffer[NS]; >>> >>> >>> >>> int main() >>> { >>> long I, T; >>> short int D, Y, E; >>> short int temp = 0; >>> short int W[N+1] = {0,}; //Impulse Response of Adaptive FIR filter >>> short int X[N+1] = {0,}; //Input Signal of Adaptive FIR filter >>> int result; >>> FILE *ref; >>> FILE *des; >>> FILE *final; >>> //FILE *coeff; >>> >>> //File with Echo >>> des = fopen ("des.raw","rb"); >>> if (des==NULL) >>> { >>> printf ("fopen des failedn"); >>> fclose (des); >>> } >>> //File without echo >>> ref = fopen ("ref.raw","rb"); >>> if (ref==NULL) >>> { >>> printf ("fopen ref failedn"); >>> fclose (ref); >>> } >>> //Ouput file - E = (des.Raw - ref.raw) >>> final = fopen ("final","ab+"); >>> if (ref==NULL) >>> { >>> printf ("fopen final failedn"); >>> fclose (final); >>> } >>> /*coeff = fopen ("coeff.txt","w+"); >>> if (ref==NULL) >>> { >>> printf ("fopen coeff failedn"); >>> fclose (final); >>> }*/ >>> while( !feof( ref ) ) >>> { >>> result = fread(buffer,sizeof(short int),NS,ref); >>> result = fread(buffer1,sizeof(short int),NS,des); >>> >>> >>> for (T = 0; T < NS; T++) //start adaptive algorithm >>> { >>> >>> Y = 0; //filter'output set to zero >>> X[0] = buffer[T]; >>> >>> D = buffer1[T]; >>> >>> for (I = 0; I < N; I++) >>> { >>> //Y = (Y + (W[I] * X[I])); >>> >>> Y = Y + (int)(((long)(W[I] * (X[I])))>>15); //calculate filter >>output >>> }//Loop End >>> >>> E = D-Y; //calculate error signal >>> >>> temp = (int)(((long)(beta*E))>>15); >>> //temp = beta*E; >>> for (I = N; I > 0; I--) >>> { >>> W[I+1] = (W[I] + (int)((long)(X[I]*temp))>>15); //update filter >>> coefficients >>> //W[I+1] = (W[I] + (X[I]*temp)); >>> //if (I != 0) >>> //(X[I] = X[I-1]); >>> } >>> for(I=N;I>0;I--) >>> { >>> if (I != 0) >>> (X[I] = X[I-1]); >>> } >>> ec_buffer[T] = E; >>> >>> } >>> result = fwrite(ec_buffer,sizeof(short int),NS,final); >>> >>> } >>> result = fclose(final); >>> fclose (ref); >>> fclose (des); >>> return 0; >>> } >>> >>> >>> --------------------------------------- >>> Posted through http://www.DSPRelated.com >> >>Your declarations tell me that you have not really studied the LMS >>algorithm implementation. Your NS is much too short. You will probably >>need on the order of 1000 samples. Get Widrow's book on adaptive >signal >>processing. >> >>Maurice Givens > >Thanks for the reply Maurice,I tried increasing the NS from 1000 to >14000 >but still I could not get the desired results. >Most of the implementations of the LMS algorithms are in floating point, >but here I am trying in fixed point. > >I am not sure where is this algorithm implementation going >wrong...Please >help to check this implementation. > >Regards, >Ram >--------------------------------------- >Posted through http://www.DSPRelated.comPlease help to debug the error in this implementation...Is it the residual echo that is causing this issue ? Regards, Ram --------------------------------------- Posted through http://www.DSPRelated.com
Reply by ●May 4, 20152015-05-04
On Saturday, May 2, 2015 at 12:49:02 AM UTC-5, pscall4ram wrote:> >>On Tuesday, April 28, 2015 at 7:42:25 AM UTC-5, pscall4ram wrote: > >>> I have been coding a very rudimentary LMS algorithm for Echo > >>cancellation > >>> but I am stuck since the code is not working. > >>> > >>> I have kept the code to be very simple and yet I am not able to > >>identify > >>> where the code is going wrong. > >>> > >>> Please help me to identify the error in the code. > >>> > >>> FILE *ref; and FILE *des; contains the same data and ideally when > >the > >>> algorithm converges,there should not be any voice in the FILE *final > >>file > >>> (this is what I expect since there is only echo and no other side > >>voice is > >>> assumed),but still I am getting the same voice data as that of the > >>*ref > >>> and *des file...there is no echo suppression. > >>> > >>> ************************************************************* > >>> #include <stdio.h> > >>> #include <math.h> > >>> #include <stdlib.h> > >>> > >>> #define beta 10 //convergence rate > >>> #define N 40//order of filter > >>> #define NS 40 //number of samples > >>> #define Fs 16000 //sampling frequency > >>> > >>> > >>> short int buffer[NS]; > >>> short int buffer1[NS]; > >>> short int ec_buffer[NS]; > >>> > >>> > >>> > >>> int main() > >>> { > >>> long I, T; > >>> short int D, Y, E; > >>> short int temp = 0; > >>> short int W[N+1] = {0,}; //Impulse Response of Adaptive FIR filter > >>> short int X[N+1] = {0,}; //Input Signal of Adaptive FIR filter > >>> int result; > >>> FILE *ref; > >>> FILE *des; > >>> FILE *final; > >>> //FILE *coeff; > >>> > >>> //File with Echo > >>> des = fopen ("des.raw","rb"); > >>> if (des==NULL) > >>> { > >>> printf ("fopen des failedn"); > >>> fclose (des); > >>> } > >>> //File without echo > >>> ref = fopen ("ref.raw","rb"); > >>> if (ref==NULL) > >>> { > >>> printf ("fopen ref failedn"); > >>> fclose (ref); > >>> } > >>> //Ouput file - E = (des.Raw - ref.raw) > >>> final = fopen ("final","ab+"); > >>> if (ref==NULL) > >>> { > >>> printf ("fopen final failedn"); > >>> fclose (final); > >>> } > >>> /*coeff = fopen ("coeff.txt","w+"); > >>> if (ref==NULL) > >>> { > >>> printf ("fopen coeff failedn"); > >>> fclose (final); > >>> }*/ > >>> while( !feof( ref ) ) > >>> { > >>> result = fread(buffer,sizeof(short int),NS,ref); > >>> result = fread(buffer1,sizeof(short int),NS,des); > >>> > >>> > >>> for (T = 0; T < NS; T++) //start adaptive algorithm > >>> { > >>> > >>> Y = 0; //filter'output set to zero > >>> X[0] = buffer[T]; > >>> > >>> D = buffer1[T]; > >>> > >>> for (I = 0; I < N; I++) > >>> { > >>> //Y = (Y + (W[I] * X[I])); > >>> > >>> Y = Y + (int)(((long)(W[I] * (X[I])))>>15); //calculate filter > >>output > >>> }//Loop End > >>> > >>> E = D-Y; //calculate error signal > >>> > >>> temp = (int)(((long)(beta*E))>>15); > >>> //temp = beta*E; > >>> for (I = N; I > 0; I--) > >>> { > >>> W[I+1] = (W[I] + (int)((long)(X[I]*temp))>>15); //update filter > >>> coefficients > >>> //W[I+1] = (W[I] + (X[I]*temp)); > >>> //if (I != 0) > >>> //(X[I] = X[I-1]); > >>> } > >>> for(I=N;I>0;I--) > >>> { > >>> if (I != 0) > >>> (X[I] = X[I-1]); > >>> } > >>> ec_buffer[T] = E; > >>> > >>> } > >>> result = fwrite(ec_buffer,sizeof(short int),NS,final); > >>> > >>> } > >>> result = fclose(final); > >>> fclose (ref); > >>> fclose (des); > >>> return 0; > >>> } > >>> > >>> > >>> --------------------------------------- > >>> Posted through http://www.DSPRelated.com > >> > >>Your declarations tell me that you have not really studied the LMS > >>algorithm implementation. Your NS is much too short. You will probably > >>need on the order of 1000 samples. Get Widrow's book on adaptive > >signal > >>processing. > >> > >>Maurice Givens > > > >Thanks for the reply Maurice,I tried increasing the NS from 1000 to > >14000 > >but still I could not get the desired results. > >Most of the implementations of the LMS algorithms are in floating point, > >but here I am trying in fixed point. > > > >I am not sure where is this algorithm implementation going > >wrong...Please > >help to check this implementation. > > > >Regards, > >Ram > >--------------------------------------- > >Posted through http://www.DSPRelated.com > > Please help to debug the error in this implementation...Is it the residual > echo that is causing this issue ? > > Regards, > Ram > --------------------------------------- > Posted through http://www.DSPRelated.comI am not going to debug your code for you. There are many examples of fixed-point LMS implementations. Do a bit of searching and compare with yours.
Reply by ●May 5, 20152015-05-05