Shop OBEX P1 Docs P2 Docs Learn Events
average serial inputs — Parallax Forums

average serial inputs

oldhippyoldhippy Posts: 36
edited 2014-12-19 09:11 in BASIC Stamp
Found a solution thanks everyone

Comments

  • SapphireSapphire Posts: 496
    edited 2014-11-22 19:07
    What size data (Byte, Word)? Why do you want to throw out the high and the low? What if there are more than one high and/or low values? What if they are all the same?

    But if you must, then put the 10 data points into an array, sort the array (low to high or high to low, doesn't matter), sum up the middle 8 values and divide by 8.

    If you keep all the values, then sum them up as they come in, and after 10 data points divide the sum by 10.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-11-22 19:09
    Maybe what you need instead is a running median.

    -Phil
  • oldhippyoldhippy Posts: 36
    edited 2014-11-22 19:33
    Word, sometimes the sensor gets off by much more than 2 or 3 mm, if they are all the same OK Thanks
  • oldhippyoldhippy Posts: 36
    edited 2014-12-09 15:04
    I am using a BS2
  • Mike GreenMike Green Posts: 23,101
    edited 2014-12-10 19:26
    Like Sapphire said, put the 10 values into an array and use a simple sort-in-place. Ignore the first (lowest) and last (highest) values. Add up the 8 remaining values and divide by 8 to get the average. The BS2 can have 26 bytes of variable values. An array of 10 takes 20 bytes of those. Once you're done with the first and last, you can re-use those bytes for other variables. A BubbleSort (http://en.wikipedia.org/wiki/Bubble_sort) will do ... it's slow, but that won't matter for a list of values this short.

    You can also eliminate the lowest and highest values during a separate phase to free up those storage locations before you do the sort.
  • MarkCrCoMarkCrCo Posts: 89
    edited 2014-12-12 20:51
    If you need to save space you can add them as they come in saving the data value if it is the lowest or highest so far then subtract out the lowest and highest and divide by 8

    '9 bytes used instead of 20 byte array
    lowpoint var word
    highpoint var word
    sumpoint var word
    datapoint var word
    i var byte
    lowpoint = 65535
    highpoint = 0
    sumpoint = 0
    ' get_data routine returns value in datapoint
    for i = 1 to 10
    gosub get_data
    if datapoint < lowpoint then
    lowpoint = datapoint
    endif
    if datapoint > highpoint then
    highpoint = datapoint
    endif
    sumpoint = sumpoint + datapoint
    next
    sumpoint = sumpoint - (highpoint + lowpoint)
    sumpoint = sumpoint / 8
  • oldhippyoldhippy Posts: 36
    edited 2014-12-18 15:58
    Thanks everyone...I have it working
  • oldhippyoldhippy Posts: 36
    edited 2014-12-19 05:42
    The problem has been solved Thanks for the input
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-12-19 09:11
    You can mark the thread "solved" by editing your original post.

    -Phil
Sign In or Register to comment.