Shop OBEX P1 Docs P2 Docs Learn Events
using larger than 16 bit numbers....still stuck — Parallax Forums

using larger than 16 bit numbers....still stuck

cplattcplatt Posts: 55
edited 2011-07-13 17:18 in BASIC Stamp
I am still trying to use larger than 16 bit numbers in math calculations without resorting to using upper memory or very long loops. Thanks to everyone's help my code is close but I still cannot get the right average formula when I am using short loops from 50 - 5000 and not counting every loop but just the ones that the data is under 3000.

FOR i = 1 TO 50

  GOSUB read_sensor
      IF sensor <3000 THEN
        IF (low_sum + sensor < low_sum) THEN high_sum = high_sum + 1
        low_sum = low_sum + sensor
        y=y+1
      endif
  DEBUG ? sensor

NEXT

average= ???????????

DEBUG ? average

END

read_sensor:
'read the accelerometer raw data 4000-2000.
RETURN


Thank you for looking!!!!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-07-13 09:27
    Do you actually have to have the average or are you just trying to smooth the data? If you're just trying to smooth the data, you can do what's called a "moving average" which may not require the use of multiple precision. For example, you could save the previous sensor reading (P) and use the current (C) sensor reading with the average (A) as follows: A = (A + P + C) / 3, then P = C. Since all values will be less than 4000, the sum will be less than 16 bits and you can use 16-bit arithmetic to calculate the new average value.
  • cplattcplatt Posts: 55
    edited 2011-07-13 10:11
    Thanks Mike and Hi!! Unfortunately, I need an overall average...
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-07-13 10:56
    The following formula works for numbers of samples from 1 up to 256. If you have to go higher than that, the formula will have to be more complicated! :tongue: If you can accumulate data until you have a certain number of samples, say 50 or 1000 or any power of 2, then the formula with a fixed divisor known in advance is more manageable.
    wz   VAR  high_sum   ' these variables wx to wz, wj are aliases for your variables
    wy  VAR low_sum
    wx  VAR average
    wj   VAR y
    wx=65535 // wj * wz // wj + wz + (wy // wj)
    wx=65535 / wj * wz + (65535 // wj * wz / wj) + (wy / wj) + (wx / wj)
    ' wx is effectively the double precision accumulation wz:wy, divided by number of samples wj.
    ' the position of parentheses matters
    ' this division is for accumulations, where the high word is always less than the number of samples
    
  • cplattcplatt Posts: 55
    edited 2011-07-13 14:36
    Thanks Tracy !!! How would. The formula change for maybe a thousand samples?
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-07-13 17:18
    There's info on how these formulas are derived here.
    For exactly 1000 samples, it would be,
    calc:
    ' enter with double precision, wz:wy, two words, wz<1000, result wx is wz:wy / 1000
       wx=((wz**536*536//1000)+(wz*536)//1000+(wy//1000))
       wx=(wz*65)+(wz**35127)+(wy/1000)+(wx/1000)
    return
    
Sign In or Register to comment.