DSPRelated.com
Forums

Need help in understanding the code

Started by ssharma December 14, 2011
Hi, I need to understand the filter logic implemented in the part of the
code I've written below. This is basically a c program written for a pic
controller. The signals are coming at the ADC channels of the controller
and the signal is being filtered. I am finding it difficult to understand
the logic used in filtering. Please help me with it.

  #if ((FUS_EF_V - AdcRecuOrder - FDS_EF_V) >= 0)
#if 1
//samples are being accumulated here.
  i32EF_VmN_FZ[0] = i32EF_VmN_FZ[0]
        + (((i32T)AdcSamples[AdcSampleIndex_EF_V]
            - (i32T)AdcSamples[AdcSampleIndex_EF_N])
           << (FUS_EF_V - AdcRecuOrder - FDS_EF_V));//(19-5-12=2)
#endif
#else
#error Dieser Fall ist nicht implementiert
#endif
// Recursive trigger is being checked
  if ((AdcInterruptCount & ((1<<AdcRecuOrder)-1)) == RT_EF_V) { (if
AdcInterruptCount & 31 ==1)
//A recursion for the 2nd order filters. Output value in Filter time [1]
 
    if (i32EF_VmN_FZ[0] > +0xEFFFFFFF) i32EF_VmN_FZ[0] = +0xEFFFFFFF;
//Maximum
   if (i32EF_VmN_FZ[0] < -0xEFFFFFFF) i32EF_VmN_FZ[0] = -0xEFFFFFFF;
//Minimum
    D = (i32EF_VmN_FZ[0] - i32EF_VmN_FZ[1]);
//    D = D - (((((D >> 1) + D) >> 1) + D) >> (FDS_EF_V/2));
    D = D - (((((((D >> 1) + D) >> 1) + D) >> 1) + D) >> (FDS_EF_V/2));// D
=D-15D/48;D=D-0.3D; I dont know why is this for.. Need help
	i32EF_VmN_FZ[1] = i32EF_VmN_FZ[0];//Fz[1]=Fz[0];
	i32EF_VmN_FZ[0] = i32EF_VmN_FZ[0] - (i32EF_VmN_FZ[0] >> FDS_EF_V) +
D;//FZ[0]=Fz[0]-(Fz[0]>>12)+D;
//    if (i32EF_VmN_FZ[0] < 0L) i32EF_VmN_FZ[0] = 0; //Begrenzung auf min
  }
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- */
#if MitFilterVierterOrdnungFuer_VmN//4th order
  //Zweite Stufe f&uuml;r Filter EF_VmN (VV minus VN)//2nd stage for filter
EF_Vmn
  if ((AdcInterruptCount & ((1<<AdcRecuOrder)-1)) == RT_EF_V_2) {
    i32EF_VmN_FZ[2] = i32EF_VmN_FZ[2] + (i32EF_VmN_FZ[1] >>
FDS_EF_V);//Fz[2]=Fz[2]+Fz[1]>>12; dont know why is it used..
    //A recursion for the 2nd order filters. Output value in Filter time
[3]
    D = (i32EF_VmN_FZ[2] - i32EF_VmN_FZ[3]);
//    D = D - (((((D >> 1) + D) >> 1) + D) >> (FDS_EF_V/2));
    D = D - (((((((D >> 1) + D) >> 1) + D) >> 1) + D) >>
(FDS_EF_V/2));//D=D-15D/48;
    i32EF_VmN_FZ[3] = i32EF_VmN_FZ[2];
    i32EF_VmN_FZ[2] = i32EF_VmN_FZ[2] - (i32EF_VmN_FZ[2] >> FDS_EF_V) + D;
//    if (i32EF_VmN_FZ[2] < 0L) i32EF_VmN_FZ[2] = 0; 
  }


On Dec 14, 9:15=A0am, "ssharma"
<sharma.subrahmanya@n_o_s_p_a_m.gmail.com> wrote:
> Hi, I need to understand the filter logic implemented in the part of the > code I've written below. This is basically a c program written for a pic > controller. The signals are coming at the ADC channels of the controller > and the signal is being filtered. I am finding it difficult to understand > the logic used in filtering. Please help me with it. > > =A0 #if ((FUS_EF_V - AdcRecuOrder - FDS_EF_V) >=3D 0) > #if 1 > //samples are being accumulated here. > =A0 i32EF_VmN_FZ[0] =3D i32EF_VmN_FZ[0] > =A0 =A0 =A0 =A0 + (((i32T)AdcSamples[AdcSampleIndex_EF_V] > =A0 =A0 =A0 =A0 =A0 =A0 - (i32T)AdcSamples[AdcSampleIndex_EF_N]) > =A0 =A0 =A0 =A0 =A0 =A0<< (FUS_EF_V - AdcRecuOrder - FDS_EF_V));//(19-5-1=
2=3D2)
> #endif > #else > #error Dieser Fall ist nicht implementiert > #endif > // Recursive trigger is being checked > =A0 if ((AdcInterruptCount & ((1<<AdcRecuOrder)-1)) =3D=3D RT_EF_V) { (if > AdcInterruptCount & 31 =3D=3D1) > //A recursion for the 2nd order filters. Output value in Filter time [1] > > =A0 =A0 if (i32EF_VmN_FZ[0] > +0xEFFFFFFF) i32EF_VmN_FZ[0] =3D +0xEFFFFFF=
F;
> //Maximum > =A0 =A0if (i32EF_VmN_FZ[0] < -0xEFFFFFFF) i32EF_VmN_FZ[0] =3D -0xEFFFFFFF=
;
> //Minimum > =A0 =A0 D =3D (i32EF_VmN_FZ[0] - i32EF_VmN_FZ[1]); > // =A0 =A0D =3D D - (((((D >> 1) + D) >> 1) + D) >> (FDS_EF_V/2)); > =A0 =A0 D =3D D - (((((((D >> 1) + D) >> 1) + D) >> 1) + D) >> (FDS_EF_V/=
2));// D
> =3DD-15D/48;D=3DD-0.3D; I dont know why is this for.. Need help > =A0 =A0 =A0 =A0 i32EF_VmN_FZ[1] =3D i32EF_VmN_FZ[0];//Fz[1]=3DFz[0]; > =A0 =A0 =A0 =A0 i32EF_VmN_FZ[0] =3D i32EF_VmN_FZ[0] - (i32EF_VmN_FZ[0] >>=
FDS_EF_V) +
> D;//FZ[0]=3DFz[0]-(Fz[0]>>12)+D; > // =A0 =A0if (i32EF_VmN_FZ[0] < 0L) i32EF_VmN_FZ[0] =3D 0; //Begrenzung a=
uf min
> =A0 } > =A0 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - =
- -
> - */ > #if MitFilterVierterOrdnungFuer_VmN//4th order > =A0 //Zweite Stufe f=FCr Filter EF_VmN (VV minus VN)//2nd stage for filte=
r
> EF_Vmn > =A0 if ((AdcInterruptCount & ((1<<AdcRecuOrder)-1)) =3D=3D RT_EF_V_2) { > =A0 =A0 i32EF_VmN_FZ[2] =3D i32EF_VmN_FZ[2] + (i32EF_VmN_FZ[1] >> > FDS_EF_V);//Fz[2]=3DFz[2]+Fz[1]>>12; dont know why is it used.. > =A0 =A0 //A recursion for the 2nd order filters. Output value in Filter t=
ime
> [3] > =A0 =A0 D =3D (i32EF_VmN_FZ[2] - i32EF_VmN_FZ[3]); > // =A0 =A0D =3D D - (((((D >> 1) + D) >> 1) + D) >> (FDS_EF_V/2)); > =A0 =A0 D =3D D - (((((((D >> 1) + D) >> 1) + D) >> 1) + D) >> > (FDS_EF_V/2));//D=3DD-15D/48; > =A0 =A0 i32EF_VmN_FZ[3] =3D i32EF_VmN_FZ[2]; > =A0 =A0 i32EF_VmN_FZ[2] =3D i32EF_VmN_FZ[2] - (i32EF_VmN_FZ[2] >> FDS_EF_=
V) + D;
> // =A0 =A0if (i32EF_VmN_FZ[2] < 0L) i32EF_VmN_FZ[2] =3D 0; > =A0 }
Have you tried going back to the source of the information you used to write this? And what is it you are trying to do? If you are trying to filter data from the ADC, hopefully you have a spec that includes such things as the sampling rate, filter order, filter type, filter specs (Q, cutoff, ripple etc.) so you can know what your goal is. The best I can see here is you want to do some kind of recursive filtering but don't indicate here what type. Clay Clay
On 12/14/2011 9:15 AM, ssharma wrote:
> Hi, I need to understand the filter logic implemented in the part of the > code I've written below. This is basically a c program written for a pic > controller. The signals are coming at the ADC channels of the controller > and the signal is being filtered. I am finding it difficult to understand > the logic used in filtering. Please help me with it.
[code snipped] This looks like a typical case of reverse engineering undertaken by someone not equipped to do the forward design. That's very hard. Start by assigning names to the numeric constants; dummies at first, then descriptions as their use becomes clear. Jerry P.S. It seems to be a fourth-order filter: MitFilterVierterOrdnungFuer_VmN//4th order -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Thanks Clay and Jerry,

Please consider the lines from the above code,

D = (i32EF_VmN_FZ[0] - i32EF_VmN_FZ[1]);

    D = D - (((((((D >> 1) + D) >> 1) + D) >> 1) + D) >> (FDS_EF_V/2));
    i32EF_VmN_FZ[1] = i32EF_VmN_FZ[0];
    i32EF_VmN_FZ[0] = i32EF_VmN_FZ[0] - (i32EF_VmN_FZ[0] >> FDS_EF_V) + D;

In the last 2 lines, we can see the output being considered in calculation
and thus say its a IIR filter. But  could not understand why

D = D - (((((((D >> 1) + D) >> 1) + D) >> 1) + D) >> (FDS_EF_V/2)); 
 has been used. Just wanted to know if the above line means anything to
you.



On Wed, 14 Dec 2011 11:25:38 -0600, ssharma wrote:

> Thanks Clay and Jerry, > > Please consider the lines from the above code, > > D = (i32EF_VmN_FZ[0] - i32EF_VmN_FZ[1]); > > D = D - (((((((D >> 1) + D) >> 1) + D) >> 1) + D) >> (FDS_EF_V/2)); > i32EF_VmN_FZ[1] = i32EF_VmN_FZ[0]; > i32EF_VmN_FZ[0] = i32EF_VmN_FZ[0] - (i32EF_VmN_FZ[0] >> FDS_EF_V) + > D; > > In the last 2 lines, we can see the output being considered in > calculation and thus say its a IIR filter. But could not understand why > > D = D - (((((((D >> 1) + D) >> 1) + D) >> 1) + D) >> (FDS_EF_V/2)); > has been used. Just wanted to know if the above line means anything to > you.
It looks to me like someone is trying to do a fast, reduced-precision multiply on a machine that does not support hardware multiplication (like, for instance, a PIC). You know what the '>>' operator does? If so -- work it out. You'll find that D is being multiplied by some (probably) small number (no, I'm not going to work it out for you), and that product is then subtracted from D, with an overall result that D is multiplied by a number that is close to, but not quite, 1. Take the '>> 1' as '/ 2' and see if it all makes more sense. -- My liberal friends think I'm a conservative kook. My conservative friends think I'm a liberal kook. Why am I not happy that they have found common ground? Tim Wescott, Communications, Control, Circuits & Software http://www.wescottdesign.com
compile it, run it, dump the impulse response to a file and things should
get much clearer.
Unless it turns out to be a "random response filter". Then we're in
trouble.
On Wed, 14 Dec 2011 15:42:05 -0600, mnentwig wrote:

> compile it, run it, dump the impulse response to a file and things > should get much clearer. > Unless it turns out to be a "random response filter". Then we're in > trouble.
Better yet, compile it, run it in a debugger, and see what it does to the variables at each step. _Then_ run it the rest of the way and see what it does to the data overall. -- My liberal friends think I'm a conservative kook. My conservative friends think I'm a liberal kook. Why am I not happy that they have found common ground? Tim Wescott, Communications, Control, Circuits & Software http://www.wescottdesign.com
On 12/14/2011 4:53 PM, Tim Wescott wrote:
> On Wed, 14 Dec 2011 15:42:05 -0600, mnentwig wrote: > >> compile it, run it, dump the impulse response to a file and things >> should get much clearer. >> Unless it turns out to be a "random response filter". Then we're in >> trouble. > > Better yet, compile it, run it in a debugger, and see what it does to the > variables at each step.
And give the variables meaningful names. I don't even write like that in assembler.
> _Then_ run it the rest of the way and see what it does to the data > overall.
Jerry -- Engineering is the art of making what you want from things you can get. &macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;
On Wed, 14 Dec 2011 17:05:07 -0500, Jerry Avins wrote:

> On 12/14/2011 4:53 PM, Tim Wescott wrote: >> On Wed, 14 Dec 2011 15:42:05 -0600, mnentwig wrote: >> >>> compile it, run it, dump the impulse response to a file and things >>> should get much clearer. >>> Unless it turns out to be a "random response filter". Then we're in >>> trouble. >> >> Better yet, compile it, run it in a debugger, and see what it does to >> the variables at each step. > > And give the variables meaningful names. I don't even write like that in > assembler. > >> _Then_ run it the rest of the way and see what it does to the data >> overall. > > Jerry
Back in the days of the TRS-80, you had a choice between BASIC and assembly. I ended up writing code in assembly, because I could use symbolic variable names. (Trash-80 BASIC only branched to line numbers, and only gave you the variables A through Z, all of which were global). -- My liberal friends think I'm a conservative kook. My conservative friends think I'm a liberal kook. Why am I not happy that they have found common ground? Tim Wescott, Communications, Control, Circuits & Software http://www.wescottdesign.com
Tim Wescott <tim@seemywebsite.com> wrote:

(snip, someone wrote)
>> And give the variables meaningful names. I don't even write >> like that in assembler.
(snip)
> Back in the days of the TRS-80, you had a choice between BASIC and > assembly. I ended up writing code in assembly, because I could use > symbolic variable names.
> (Trash-80 BASIC only branched to line numbers, and only gave you the > variables A through Z, all of which were global).
My TRS-80 Color Computer BASIC allowed multi-letter variables. (It was more affordable, and I liked the 6809 better than the Z80, especially for assembly programming.) I did have, sometime later, OS-9 and BASIC-09, where BASIC-09 is a structure version of BASIC with many features not in more ordinary BASIC, such as line numbers not required on all lines, and fancier structuring than many languages now. Among others, when you exit a loop you get to execute some statements on the way out. -- glen