LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

average numbers that wraps around (...126, 127, 0, 1, 2,...)

I'm looking for a way to get the average of a set of numbers that can wrap-around (highest increments to next value, lowest). The other complication is that the numbers can be of any bit-length (but all the same). Thus all the numbers can be say 6-bits in length, or all 8-bit numbers, or all 10-bit numbers, etc. And just to make it more interesting, the numbers are 2's compliments.
 
ex) for 10-bit numbers:  3FF is -1, 3FE is -2, 3FD is -3 etc.
 
The problem arises when the numbers in the set wrap-around.
 
ex) a set of numbers: 3FF, 3FE, 1, 2  The average should be 0
 
Any suggestions?
0 Kudos
Message 1 of 5
(2,503 Views)
Here is a Q&D solution. There are probably more clever ways to play with bit-level functions.
Chilly Charly    (aka CC)

         E-List Master - Kudos glutton - Press the yellow button on the left...
        
Message 2 of 5
(2,488 Views)

Thanks Chilly...

But yours has the same problem mine did. Here is the example that causes problems.

bit length = 7

two input numbers to be averaged: a) 0x 3F and b) 0x 41

The correct average is: 0x 40

but your version gives average as Zero.

 I got the same result with a similar approach calculating error-distance from mid-pt of range based on number of bits.

the problem is that this case strattles the mid-pt. Shifting to another pt. just moves the problem pt. I though of trying a

zero-crossing detection of the error signal to use a straight average for this case. I'm coding now for a try. If you, or

anyone else has any thought s on this please let me know.

Thanks.

0 Kudos
Message 3 of 5
(2,465 Views)
OK, let's start from the beginning...
Arithmetics with 7 bits numbers : Hex numbers goes from 0 to 7F. The corresponding values are 0-127 (U7). With signed integers (I7), the scale spans from -64 to +63. Here it goes :
U7  : 0 1 2 3 . . . 3E 3F  40  41  42 . . . 7D 7E 7F
Dec : 0 1 2 3 . . . 62 63 -64 -63 -62 . . . -3 -2 -1

The average of 0x3F and 0x41 corresponds to the average of 63 and -63 : zero ! The only way to get 0x40 as result is to average a series of 0x40 !? 🙂

Chilly Charly    (aka CC)

         E-List Master - Kudos glutton - Press the yellow button on the left...
        
0 Kudos
Message 4 of 5
(2,457 Views)
Hi Folks,
      This looked like fun - forgive me for jumping in.  I think CC's logic is dead-on here, but Looking seems to be saying the same thing - doesn't 0x40 = 0 (since only the sign-bit is set)(?) (assuming 7-bit signed integers)
When they give imbeciles handicap-parking, I won't have so far to walk!
0 Kudos
Message 5 of 5
(2,418 Views)