DSPRelated.com
Forums

Real time implementation - Adaptive filtering of evoked potentials

Started by Axey May 19, 2005
         Hi all, we are doing a project on "Adaptive Filtering of
Evoked Potentials". The thing is we wanted to implement the same in
real time i.e get the evoked potentials data in real time and filter
the noise using adaptive LMS algorithm. We are using the DSP processor
from Texas Instruments "TMS320C54X" for this purpose. We are using the
"Code composer" software where the C-code is converted to ALP by the
software and then loaded to the processor. But the thing is the C-code
is not working, so it would be really helpful if we get a fair idea or
C-code or MATLAB code for real time implementation of our project. PLZ
reply soon......

        Some details....
          we are using the interference cancelling configuration of
adaptive filtering. we apply original signal with noise in the primary
and the signal and noise which is correlated to the noise (the noise in
primary) is the reference input since the noise is of broadband.
Infact, we are using the EMG and EOG as the noises.

Axey wrote:
> Hi all, we are doing a project on "Adaptive Filtering of > Evoked Potentials". The thing is we wanted to implement the same in > real time i.e get the evoked potentials data in real time and filter > the noise using adaptive LMS algorithm. We are using the DSP processor > from Texas Instruments "TMS320C54X" for this purpose. We are using the > "Code composer" software where the C-code is converted to ALP by the > software and then loaded to the processor. But the thing is the C-code > is not working, so it would be really helpful if we get a fair idea or > C-code or MATLAB code for real time implementation of our project. PLZ > reply soon...... > > Some details.... > we are using the interference cancelling configuration of > adaptive filtering. we apply original signal with noise in the primary > and the signal and noise which is correlated to the noise (the noise in > primary) is the reference input since the noise is of broadband. > Infact, we are using the EMG and EOG as the noises. >
Some (hopefully relavent) questions: In what way is the C code not working? Does the C code work on other platforms? Where did you get the C code? How much do you trust it? and one that is technically irrelevant but necessary none the less: From your phrasing this is a school project -- what part are _you_ supposed to be doing? ------------------------------------------- Tim Wescott Wescott Design Services http://www.wescottdesign.com
       Well, this is a graduation project.  what we need right now is
just a fair idea to implement our project in real time. The concept is
needed, its just that we are looking for. Once we get a fair idea how
to implement in real time we can convert that concept in C-code to
implement.
    We knowing the concept of LMS algorithm we wrote our own code, it
just works in Borland C. The problem arrived when we wanted to
implement it to real time, we did change the code relevant to the real
time implementation. One more thing is the number of samples is
limited, the Borland C or the Code composer software cant take more
than 1000 samples. Any more details, do ask me. Thank you for your
reply....  What we need is a working concept or probably the C-code
iself...

Hi
I am doing the same project as you. And I have got some c code on
dsk.
But there are still some problems so that the result is not the same
as the ideal one. I will give you the c code and maybe you can find
where the problem is. Whatever the result is,please tell me! My
e-mail address is: timhust@126.com.

c code:
static short LCh,RCh;
#define FILTERLENGTH 32

static unsigned short RFlag = 0;//Data receive flag, 1 data got
	asm( " ssbx intm ");
	CPU_ini();//sysclk=10MHz*10,12.5M*8,2004-12-08

interrupt void  aic_rint0() //
{  

   LCh =  *MCBSP0_DRR1;
   RCh =  *MCBSP0_DRR2;
     
   //*MCBSP0_DXR1 = 0x8000;//test only
   
   //*MCBSP0_DXR2 = 0x8000;//test only
   RFlag = 1;
}
void Algor_1(void)
{
 while(1)
 	{
 while(!RFlag);
 RFlag = 0;
 DATAIO();
 	}
}
//First algorithm demo function,Digital Loop Back
void DATAIO(void)
{
    short mu=1638 ; //stepsize
    short d[FILTERLENGTH]={0};  // reference input
    short w[FILTERLENGTH];      //filter weight 
    short primary ;      //including s+n
    short k;
    short sum=0;
    short filtered=0;
    short temp;
    short j=0;

    if(j==0)
    {
       for(k=0;k<FILTERLENGTH;k++)
        {
          w[k]=0x7fdf; //the initial value of the coefficients is
0.999 (0x7fdf)
        }
      j=1;
    } 
     
    for(k=0; k<FILTERLENGTH-1;k++) // update the values in the
reference array
    {
     d[k]=d[k+1];
    
    }
  
    d[FILTERLENGTH-1]=LCh;
    primary=RCh;
   
    
    for(k=0; k<FILTERLENGTH;k++)
    {
        sum= sum+ ((w[k]*d[k])>>15);
    }
    filtered=primary- sum;
  
    temp=mu*filtered>>15;
    for(k=0;k<FILTERLENGTH;k++)
    {
     w[k]=w[k] + ((d[k]*temp)>>15);
    }

    


//app algorithm
*MCBSP0_DXR1 = (primary<<16) + filtered;
 *MCBSP0_DXR2 = (primary<<16) + filtered;
}

Gee, I thought the point of doing a "graduate project" 
was to do it yourself.  

On 19 May 2005 23:52:52 -0700, "Axey" <girish.tp@rediffmail.com>
wrote:
> Well, this is a graduation project. what we need right now is >just a fair idea to implement our project in real time. The concept is >needed, its just that we are looking for. Once we get a fair idea how >to implement in real time we can convert that concept in C-code to >implement. > We knowing the concept of LMS algorithm we wrote our own code, it >just works in Borland C. The problem arrived when we wanted to >implement it to real time, we did change the code relevant to the real >time implementation. One more thing is the number of samples is >limited, the Borland C or the Code composer software cant take more >than 1000 samples. Any more details, do ask me. Thank you for your >reply.... What we need is a working concept or probably the C-code >iself...
Not gone through the code in detail, but if you right shift a short
int by 15 bits, you won't have much left.

> short sum=0; > short d[FILTERLENGTH]={0}; // reference input > short w[FILTERLENGTH]; //filter weight > sum= sum+ ((w[k]*d[k])>>15);
On Fri, 20 May 2005 03:17:00 -0500, timhust@126-dot-com.no-spam.invalid (timhust) wrote:
>Hi >I am doing the same project as you. And I have got some c code on >dsk. >But there are still some problems so that the result is not the same >as the ideal one. I will give you the c code and maybe you can find >where the problem is. Whatever the result is,please tell me! My >e-mail address is: timhust@126.com. > >c code: >static short LCh,RCh; >#define FILTERLENGTH 32 > >static unsigned short RFlag = 0;//Data receive flag, 1 data got > asm( " ssbx intm "); > CPU_ini();//sysclk=10MHz*10,12.5M*8,2004-12-08 > >interrupt void aic_rint0() // >{ > > LCh = *MCBSP0_DRR1; > RCh = *MCBSP0_DRR2; > > //*MCBSP0_DXR1 = 0x8000;//test only > > //*MCBSP0_DXR2 = 0x8000;//test only > RFlag = 1; >} >void Algor_1(void) >{ > while(1) > { > while(!RFlag); > RFlag = 0; > DATAIO(); > } >} >//First algorithm demo function,Digital Loop Back >void DATAIO(void) >{ > short mu=1638 ; //stepsize > short d[FILTERLENGTH]={0}; // reference input > short w[FILTERLENGTH]; //filter weight > short primary ; //including s+n > short k; > short sum=0; > short filtered=0; > short temp; > short j=0; > > if(j==0) > { > for(k=0;k<FILTERLENGTH;k++) > { > w[k]=0x7fdf; //the initial value of the coefficients is >0.999 (0x7fdf) > } > j=1; > } > > for(k=0; k<FILTERLENGTH-1;k++) // update the values in the >reference array > { > d[k]=d[k+1]; > > } > > d[FILTERLENGTH-1]=LCh; > primary=RCh; > > > for(k=0; k<FILTERLENGTH;k++) > { > sum= sum+ ((w[k]*d[k])>>15); > } > filtered=primary- sum; > > temp=mu*filtered>>15; > for(k=0;k<FILTERLENGTH;k++) > { > w[k]=w[k] + ((d[k]*temp)>>15); > } > > > > >//app algorithm >*MCBSP0_DXR1 = (primary<<16) + filtered; > *MCBSP0_DXR2 = (primary<<16) + filtered; >} >
AntiSPAM_g9u5dd43@yahoo.com writes:

> Not gone through the code in detail, but if you right shift a short > int by 15 bits, you won't have much left. > >> short sum=0; >> short d[FILTERLENGTH]={0}; // reference input >> short w[FILTERLENGTH]; //filter weight >> sum= sum+ ((w[k]*d[k])>>15);
You're warning on this potential issue is prudent, but in the case of overflow the compiler designers are free to do what they please. In TI's case they do the expected (and easiest) thing, compute the product in the accumulator (with all 40 bits available) and then shift and store back into a 16-bit int. So the warning is good, but the code, in this case, will work properly. --Randy
> > > > > On Fri, 20 May 2005 03:17:00 -0500, > timhust@126-dot-com.no-spam.invalid (timhust) wrote: > >>Hi >>I am doing the same project as you. And I have got some c code on >>dsk. >>But there are still some problems so that the result is not the same >>as the ideal one. I will give you the c code and maybe you can find >>where the problem is. Whatever the result is,please tell me! My >>e-mail address is: timhust@126.com. >> >>c code: >>static short LCh,RCh; >>#define FILTERLENGTH 32 >> >>static unsigned short RFlag = 0;//Data receive flag, 1 data got >> asm( " ssbx intm "); >> CPU_ini();//sysclk=10MHz*10,12.5M*8,2004-12-08 >> >>interrupt void aic_rint0() // >>{ >> >> LCh = *MCBSP0_DRR1; >> RCh = *MCBSP0_DRR2; >> >> //*MCBSP0_DXR1 = 0x8000;//test only >> >> //*MCBSP0_DXR2 = 0x8000;//test only >> RFlag = 1; >>} >>void Algor_1(void) >>{ >> while(1) >> { >> while(!RFlag); >> RFlag = 0; >> DATAIO(); >> } >>} >>//First algorithm demo function,Digital Loop Back >>void DATAIO(void) >>{ >> short mu=1638 ; //stepsize >> short d[FILTERLENGTH]={0}; // reference input >> short w[FILTERLENGTH]; //filter weight >> short primary ; //including s+n >> short k; >> short sum=0; >> short filtered=0; >> short temp; >> short j=0; >> >> if(j==0) >> { >> for(k=0;k<FILTERLENGTH;k++) >> { >> w[k]=0x7fdf; //the initial value of the coefficients is >>0.999 (0x7fdf) >> } >> j=1; >> } >> >> for(k=0; k<FILTERLENGTH-1;k++) // update the values in the >>reference array >> { >> d[k]=d[k+1]; >> >> } >> >> d[FILTERLENGTH-1]=LCh; >> primary=RCh; >> >> >> for(k=0; k<FILTERLENGTH;k++) >> { >> sum= sum+ ((w[k]*d[k])>>15); >> } >> filtered=primary- sum; >> >> temp=mu*filtered>>15; >> for(k=0;k<FILTERLENGTH;k++) >> { >> w[k]=w[k] + ((d[k]*temp)>>15); >> } >> >> >> >> >>//app algorithm >>*MCBSP0_DXR1 = (primary<<16) + filtered; >> *MCBSP0_DXR2 = (primary<<16) + filtered; >>} >> >
-- % Randy Yates % "So now it's getting late, %% Fuquay-Varina, NC % and those who hesitate %%% 919-577-9882 % got no one..." %%%% <yates@ieee.org> % 'Waterfall', *Face The Music*, ELO http://home.earthlink.net/~yatescr
Randy Yates <yates@ieee.org> writes:
> [...] >>> sum= sum+ ((w[k]*d[k])>>15);
PS: Using C's integral promotion rules, the proper way to do this would be sum = sum + (((long)w[k] * d[k]) >> 15); -- % Randy Yates % "Maybe one day I'll feel her cold embrace, %% Fuquay-Varina, NC % and kiss her interface, %%% 919-577-9882 % til then, I'll leave her alone." %%%% <yates@ieee.org> % 'Yours Truly, 2095', *Time*, ELO http://home.earthlink.net/~yatescr
  Thanks for your reply with the code...  We will surely check it out
and send out the results and i think you can check out the suggetions
by the experts given right in this discussion... Thanks once again...

On 19 May 2005 23:52:52 -0700, Axey <girish.tp@rediffmail.com> wrote:
> Well, this is a graduation project. what we need right now is > just a fair idea to implement our project in real time. The concept is > needed, its just that we are looking for. Once we get a fair idea how > to implement in real time we can convert that concept in C-code to > implement.
How are you defining "RealTime?" There's a definition of realtime that means "Take a temperature reading every day at noon." There's another definition that means, "play the movie with no data glitches and try not to have any dropouts greater than 1/30 second." There's still another that means, "If you don't respond to this interrupt within 250ms, things blow up (occasionally literally). All three are "real time" by a reasonable definition. The third is MUCH harder to achieve than the first two. What you need to do is the same thing we always need to do in optimization: 1. Profile your code to determine where it's taking too much time. 2. Optimize the critical loops It's a lot easier to ask for suggestions on three lines of code versus a lengthy uncommented program listing.