DSPRelated.com
Forums

A piece of code related to DSP

Started by narke May 13, 2010
Hi, 

I have a big code, and I cannot get understand some parts of it. Below
is a piece of them:

void determind_corr_value(void)
{
   int8_t Leading0;
   uint32_t CorrValue;

   ...

   if( CorrValue>0)
   { 
      // Detect bit number of MSB of CorrValue (Leading 0's are
counted)
      Leading0 = 1;

      while(!(*((int16_t*) &CorrValue + 1) < 0))   // MS_Bit = 0
      {
         CorrValue <<= 1;
         Leading0++;
      }

      Leading0 -= 8;

      // increase the resolution for compensation steps to 0.5Bit by
checking the 2nd bit
      //lint -save -e701
      Leading0 <<= 1;
      if(*((int16_t*) &CorrValue + 1) & 0x4000)
      {
         Leading0--;
      }

      // Calculate now the correction value
      if( Current < CalibrationData.I_Limit_Corr) {
         // low range
         Leading0-= (int8_t)Data.Leading0_Corr_Low;

         CorrValue = (unsigned)(long)(32768L + ((int16_t)
(CalibrationData.Correction_Low * Leading0)));
      } else {
         // High range
         Leading0-= (int8_t)Data.Leading0_Corr_High;

         CorrValue = (unsigned)(long)(32768L + ((int16_t)
(CalibrationData.Correction_High * Leading0)));
      }

   } else
      CorrValue = 32768;

   ...
}


I guess it was doing some kind of DSP, but not sure what was exactly
going on.  Especially, I hope some one have a guess and give me some
hints for my below questions so far:

1. Why Leading0 was assigned as 1 at begin of the process. 
2. Why 8 was substracted from the Leading0
3. What means 0.5bit and checking for 2nd bit?
4. Why 32768?

I hope the code indeed implemented some algorithm that is familiar to
some of you.


Regards,
woody

On May 13, 10:00&#4294967295;am, narke <narkewo...@gmail.com> wrote:
> Hi, > > I have a big code, and I cannot get understand some parts of it. Below > is a piece of them: > > void determind_corr_value(void) > { > &#4294967295; &#4294967295;int8_t Leading0; > &#4294967295; &#4294967295;uint32_t CorrValue; > > &#4294967295; &#4294967295;... > > &#4294967295; &#4294967295;if( CorrValue>0) > &#4294967295; &#4294967295;{ > &#4294967295; &#4294967295; &#4294967295; // Detect bit number of MSB of CorrValue (Leading 0's are > counted) > &#4294967295; &#4294967295; &#4294967295; Leading0 = 1; > > &#4294967295; &#4294967295; &#4294967295; while(!(*((int16_t*) &CorrValue + 1) < 0)) &#4294967295; // MS_Bit = 0 > &#4294967295; &#4294967295; &#4294967295; { > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;CorrValue <<= 1; > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;Leading0++; > &#4294967295; &#4294967295; &#4294967295; } > > &#4294967295; &#4294967295; &#4294967295; Leading0 -= 8; > > &#4294967295; &#4294967295; &#4294967295; // increase the resolution for compensation steps to 0.5Bit by > checking the 2nd bit > &#4294967295; &#4294967295; &#4294967295; //lint -save -e701 > &#4294967295; &#4294967295; &#4294967295; Leading0 <<= 1; > &#4294967295; &#4294967295; &#4294967295; if(*((int16_t*) &CorrValue + 1) & 0x4000) > &#4294967295; &#4294967295; &#4294967295; { > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;Leading0--; > &#4294967295; &#4294967295; &#4294967295; } > > &#4294967295; &#4294967295; &#4294967295; // Calculate now the correction value > &#4294967295; &#4294967295; &#4294967295; if( Current < CalibrationData.I_Limit_Corr) { > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;// low range > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;Leading0-= (int8_t)Data.Leading0_Corr_Low; > > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;CorrValue = (unsigned)(long)(32768L + ((int16_t) > (CalibrationData.Correction_Low * Leading0))); > &#4294967295; &#4294967295; &#4294967295; } else { > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;// High range > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;Leading0-= (int8_t)Data.Leading0_Corr_High; > > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;CorrValue = (unsigned)(long)(32768L + ((int16_t) > (CalibrationData.Correction_High * Leading0))); > &#4294967295; &#4294967295; &#4294967295; } > > &#4294967295; &#4294967295;} else > &#4294967295; &#4294967295; &#4294967295; CorrValue = 32768; > > &#4294967295; &#4294967295;... > > } > > I guess it was doing some kind of DSP, but not sure what was exactly > going on. &#4294967295;Especially, I hope some one have a guess and give me some > hints for my below questions so far: > > 1. Why Leading0 was assigned as 1 at begin of the process. > 2. Why 8 was substracted from the Leading0 > 3. What means 0.5bit and checking for 2nd bit? > 4. Why 32768? > > I hope the code indeed implemented some algorithm that is familiar to > some of you. > > Regards, > woody
You may not ever get any meaningful comments on this abomination of a function, but without any kind of context, it's even more difficult to figure out what is going on. Do you have any idea what it's used for? Jason
On May 13, 11:00&#4294967295;am, narke <narkewo...@gmail.com> wrote:
> Hi, > > I have a big code, and I cannot get understand some parts of it. Below > is a piece of them: > > void determind_corr_value(void) > { > &#4294967295; &#4294967295;int8_t Leading0; > &#4294967295; &#4294967295;uint32_t CorrValue; > > &#4294967295; &#4294967295;... > > &#4294967295; &#4294967295;if( CorrValue>0) > &#4294967295; &#4294967295;{ > &#4294967295; &#4294967295; &#4294967295; // Detect bit number of MSB of CorrValue (Leading 0's are > counted) > &#4294967295; &#4294967295; &#4294967295; Leading0 = 1; > > &#4294967295; &#4294967295; &#4294967295; while(!(*((int16_t*) &CorrValue + 1) < 0)) &#4294967295; // MS_Bit = 0 > &#4294967295; &#4294967295; &#4294967295; { > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;CorrValue <<= 1; > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;Leading0++; > &#4294967295; &#4294967295; &#4294967295; } > > &#4294967295; &#4294967295; &#4294967295; Leading0 -= 8; > > &#4294967295; &#4294967295; &#4294967295; // increase the resolution for compensation steps to 0.5Bit by > checking the 2nd bit > &#4294967295; &#4294967295; &#4294967295; //lint -save -e701 > &#4294967295; &#4294967295; &#4294967295; Leading0 <<= 1; > &#4294967295; &#4294967295; &#4294967295; if(*((int16_t*) &CorrValue + 1) & 0x4000) > &#4294967295; &#4294967295; &#4294967295; { > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;Leading0--; > &#4294967295; &#4294967295; &#4294967295; } > > &#4294967295; &#4294967295; &#4294967295; // Calculate now the correction value > &#4294967295; &#4294967295; &#4294967295; if( Current < CalibrationData.I_Limit_Corr) { > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;// low range > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;Leading0-= (int8_t)Data.Leading0_Corr_Low; > > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;CorrValue = (unsigned)(long)(32768L + ((int16_t) > (CalibrationData.Correction_Low * Leading0))); > &#4294967295; &#4294967295; &#4294967295; } else { > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;// High range > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;Leading0-= (int8_t)Data.Leading0_Corr_High; > > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;CorrValue = (unsigned)(long)(32768L + ((int16_t) > (CalibrationData.Correction_High * Leading0))); > &#4294967295; &#4294967295; &#4294967295; } > > &#4294967295; &#4294967295;} else > &#4294967295; &#4294967295; &#4294967295; CorrValue = 32768; > > &#4294967295; &#4294967295;... > > } > > I guess it was doing some kind of DSP, but not sure what was exactly > going on.
This is horrible. Basically the idea is to take a 16 bit CorrValue (stored in the second half of a 32 bit integer) and use it to choose one of 32 correction values. There is also a sort of a truncated log transform. The coder had decided to use bit manipulation, probably as a dubious offering to the great god efficiency. It also has the feel of 16 bit code shoehorned into a 32 bit system. If there is any way you can get away from this code do so. Find out what needs to be done and code from scratch.
> &#4294967295;Especially, I hope some one have a guess and give me some > hints for my below questions so far: > > 1. Why Leading0 was assigned as 1 at begin of the process.
Leading0 starts out as the MSB. The MSB starts at 1.
> 2. Why 8 was substracted from the Leading0
Leading 0 has to become a signed quantity. If the MSB is small, then the correction value will be less than 32768 and Leading0 has to be negative. Note the correction value will be changed by some multiple of the (shifted) MSB. This is a log transform.
> 3. What means 0.5bit and checking for 2nd bit?
Rather that using one bit (16 possible positions) the code uses two bits (32 possible positions)
> 4. Why 32768?
This is 2^15, half of an unsigned 16 bit quantity. The correction values are meant to fill a 16 bit unsigned integer. - William Hughes
"Jason" <cincydsp@gmail.com> ha scritto nel messaggio
news:87ef5391-d194-4e7c-908e-3af03868b5ca@h39g2000yqn.googlegroups.com...
On May 13, 10:00 am, narke <narkewo...@gmail.com> wrote:
> Hi, > > I have a big code, and I cannot get understand some parts of it. Below > is a piece of them: > > void determind_corr_value(void) > { > int8_t Leading0; > uint32_t CorrValue; > > ... > > if( CorrValue>0) > { > // Detect bit number of MSB of CorrValue (Leading 0's are > counted) > Leading0 = 1; > > while(!(*((int16_t*) &CorrValue + 1) < 0)) // MS_Bit = 0
the problem i have here: !*a<0 means !(*a<0), or it means ((!*a)<0)? i think it is the second one if it is the second one !*a could be 0 (0<0 false) or 1 (1<0 false) so this while is always false (no iteration) you people get wrong when the over think for the C language grammar, get you to lost the think on the problem to resolve :)
> { > CorrValue <<= 1; > Leading0++; > } > > Leading0 -= 8;
time for pray
"io_x" <a@b.c.invalid> ha scritto nel messaggio 
news:4bec39a4$0$18987$4fafbaef@reader5.news.tin.it...
>> while(!(*((int16_t*) &CorrValue + 1) < 0)) // MS_Bit = 0 > > the problem i have here: !*a<0 means !(*a<0), or it means ((!*a)<0)? > i think it is the second one > if it is the second one !*a could be 0 (0<0 false) or 1 (1<0 false) > so this while is always false (no iteration)
all wrong i never read the second "("
> you people get wrong when the over think for the C language grammar, > get you to lost the think on the problem to resolve :)
this is right
>> { >> CorrValue <<= 1; >> Leading0++; >> } >> >> Leading0 -= 8; > > time for pray > > > >
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4bec39a4$0$18987$4fafbaef@reader5.news.tin.it...
>> while(!(*((int16_t*) &CorrValue + 1) < 0)) // MS_Bit = 0
is it not far better to write: int16_t *CorrValue2Arr=(int16_t ) &CorrValue; while(CorrValue2Arr[1]>=0) // MS_Bit == signBit == 0 {} ?
> the problem i have here: !*a<0 means !(*a<0), or it means ((!*a)<0)? > i think it is the second one > if it is the second one !*a could be 0 (0<0 false) or 1 (1<0 false) > so this while is always false (no iteration) > > you people get wrong when the over think for the C language grammar, > get you to lost the think on the problem to resolve :)
io_x <a@b.c.invalid> wrote:

>"Jason" <cincydsp@gmail.com> ha scritto nel messaggio
>> while(!(*((int16_t*) &CorrValue + 1) < 0)) // MS_Bit = 0
>the problem i have here: !*a<0 means !(*a<0), or it means ((!*a)<0)? >i think it is the second one
If means ((!*a) < 0), since ! and * have precedence over <; which means specifically ((!(*a)) < 0). (Prefix operators of the same precedence, such as ! and *, associate right to left.) Steve
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4bec3df9$0$12123$4fafbaef@reader4.news.tin.it...
> > "io_x" <a@b.c.invalid> ha scritto nel messaggio > news:4bec39a4$0$18987$4fafbaef@reader5.news.tin.it... >>> while(!(*((int16_t*) &CorrValue + 1) < 0)) // MS_Bit = 0 > > > is it not far better to write: > > int16_t *CorrValue2Arr=(int16_t ) &CorrValue;
wrong arghhaahah int16_t *CorrValue2Arr=(int16_t*) &CorrValue; while(CorrValue2Arr[1]>=0)// MS_Bit==signBit(CorrValue2Arr[1])==0 {}
> ? > >> the problem i have here: !*a<0 means !(*a<0), or it means ((!*a)<0)? >> i think it is the second one >> if it is the second one !*a could be 0 (0<0 false) or 1 (1<0 false) >> so this while is always false (no iteration) >> >> you people get wrong when the over think for the C language grammar, >> get you to lost the think on the problem to resolve :) > > > > > >
"io_x" <a@b.c.invalid> writes:

> "Jason" <cincydsp@gmail.com> ha scritto nel messaggio > news:87ef5391-d194-4e7c-908e-3af03868b5ca@h39g2000yqn.googlegroups.com... > On May 13, 10:00 am, narke <narkewo...@gmail.com> wrote:
<snip>
>> while(!(*((int16_t*) &CorrValue + 1) < 0)) // MS_Bit = 0 > > the problem i have here: !*a<0 means !(*a<0), or it means ((!*a)<0)? > i think it is the second one
!*a<0 means (!*a) < 0 but that is not what was written. The ! is outside a fully parenthesised relational expression: !(E < 0) or E >= 0 By the way, I agree entirely with your re-write (in a subsequent post). Using array notation is much clearer: int16_t *half_word = (void *)&CorrValue; while (half_word[1] >= 0) ... however this still confuses significance with address order (i.e. it assumes endianness). <snip> -- Ben.
Ben Bacarisse  <ben.usenet@bsb.me.uk> wrote:

>!*a<0 means (!*a) < 0 but that is not what was written. The ! is >outside a fully parenthesised relational expression: > > !(E < 0) or E >= 0 > >By the way, I agree entirely with your re-write (in a subsequent post). >Using array notation is much clearer: > > int16_t *half_word = (void *)&CorrValue; > while (half_word[1] >= 0) ... > >however this still confuses significance with address order (i.e. it >assumes endianness).
Tangentially, there are these nice things called log functions in the math libraries that can be used for leading zero counts. It is (usually) no longer necessary to worry about library functions being too slow and hand-coding this stuff... Steve