Forums

LMS algorithm in fixed point arithmetic

Started by pscall4ram April 28, 2015
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&rsquo;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
>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&acirc;&#128;&#153;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
Really, 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
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
>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
>>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.com
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.com
I 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.
One basic step I haven't seen mentioned in all of this: Have you tried to run your algorithm first in floating point, just to get a meaningful result for comparison?  Without that you can't be sure that your problem is due to fixed-point implementation.