Smoothing out jumpy values

BlitzMax Forums/BlitzMax Programming/Smoothing out jumpy values

wmaass(Posted 2009) [#1]
I have a value that is returned from a sensor that fluctuates a bit, about plus or minus 100 or so. I need a way to "smooth" it out. A friend mentioned something along the lines of a Moving Average Filter which I will check out but does anyone have a suggestion for this?


Jim Teeuwen(Posted 2009) [#2]
simple really. Just accumulate 10 or more sensor values into an array.. As soon as you get 10 or so. calculate the average of those 10 and use that as the value to work with.. Clear the array and collect 10 new values from the sensor.

10 is just an example here. You can tweak it to whatever makes you happy.

Small example:
local arr:int = new int[10];
local index:int = 0;

while(mainloop)
  arr[index] = ReadSensor();
  index :+ 1;

  if(index >= arr.Length - 1) then
    local average:double = GetAverage(arr);
    DoStuffWithSensorValue(average);
    index = 0;
  end if
wend

function GetAverage:double(arr:int[])
  local avg:double = 0;
  for local n:int = 0 to arr.Length - 1
    avg :+ arr[n];
  next
  return (avg / arr.Length);
end function



wmaass(Posted 2009) [#3]
That was fast! (runs off to try)


_Skully(Posted 2009) [#4]
Averaging might not do the trick properly since those spikes will influence your average value... you might have to look for large deviations +/- 25% and ignore them within those samples... i'm using over 10 year old electronics technician memories here so you'll have to play with it :)


wmaass(Posted 2009) [#5]
I think your right _Skully. Thanks for the tips and example guys, I appreciate it.