Need a clue please: How to average 2's comp values?

Pongo

Senior Member
How, with the limited resources of the picaxe, do I average 16 bit 2's complement values? (Magnetometer chip output.)
 

stan74

Senior Member
google says add the values and divide by number of values. added values<32768 I suppose.no a word
 
Last edited:

Pongo

Senior Member
I know how to average Stan, but I don't think it's that simple with 2's complement, especially when there are only 16 bits and positive integers to play with. Go ahead and average these 2's complement numbers:

1111100000110000 63536 (=-2000)
1111010001001000 62536 (=-3000)
1111110000011000 64536 (=-1000)
0000001111101000 1000
 

stan74

Senior Member
Try this or similar a=-1000,b=-2000,c=-3000
if a<b and a<c then x=b-a:y=c-a:average=x+y/2+a
if b<a and b<c then x=a-b:y=c-b:average=x+y/2+b
if c<a and c<b then x=a-c:y=b-c:average=x+y/2+c
no,that works with 4 numbers...add another number :)
 
Last edited:

westaust55

Moderator
Try something along these lines

W9 = -W0 - W1 - W2 / 3
W9 = -W9

Then with
W0 = %1111100000110000 ; 63536 (=-2000)
W1 = %1111010001001000 ; 62536 (=-3000)
W2 = %1111110000011000 ; 64536 (=-1000)

W9 = Sum / No items ==> 6000 total / 3 ==> 2000
and W9 = -W9 converts back to 2's compliment and

W9 = %1111100000110000 ; 63536

Obviously no checks there for overflow however
 

stan74

Senior Member
The sign isn't a problem,its the overflow which makes it interesting. Depend how accurate Pongo needs it.
wo/4+w1/4+w2/4/ /3 *4
 

AllyCat

Senior Member
Hi,

google says add the values and divide by number of values. added values<32768 I suppose.no a word
PICaxe Basic is fine for adding and subtracting 2's complement numbers (up to +/- 32767) but can't do multiplication or division (directly). However, you only need to negate any negative numbers (i.e. if the MSB = 1), then divide as usual and negate the result (unless both numerator and denominator were negative).

Discussed in this recent thread although it did go rather off-topic. ;)

Cheers, Alan.
 

Pongo

Senior Member
Hi,

PICaxe Basic is fine for adding and subtracting 2's complement numbers (up to +/- 32767) but can't do multiplication or division (directly). However, you only need to negate any negative numbers (i.e. if the MSB = 1), then divide as usual and negate the result (unless both numerator and denominator were negative).

Cheers, Alan.
Thanks :) After some experimentation that's where I ended up, the addition is no problem for positive or negative numbers, just the final division has to be handled appropriately. That's aside from the potential overflow issue, but I can live with reducing the resolution to handle that.
 

hippy

Technical Support
Staff member
It is a little more difficult than it first appeared, can't simply do ...

avg = ( w1 + w2 + w3 + w4 ) /4

Because of overflow, and can't do it this way on a PICAXE with simple division ...

avg = ( w1 / 4 ) + ( w2 / 4 ) + ( w3 / 4 ) + ( w4 / 4 )

Because PICAXE integer math doesn't sign extend on division. I ended up with ...

Code:
#Macro Add( dst, src )
  If src >= $8000 Then
    dst = src / 4 | $C000 + dst
  Else
    dst = src / 4 + dst
  End If
#EndMacro

w1 = -400
w2 = -200
w3 = -100
w4 =  100

w0 = 0
Add( w0, w1 )
Add( w0, w2 )
Add( w0, w3 )
Add( w0, w4 )

If w0 >= $8000 Then
  w0 = -w0
  SerTxd( "-", #w0 )
Else
  SerTxd( #w0 )
End If
 
Top