Algorithm to smooth numbers with variable input time -
Algorithm to smooth numbers with variable input time -
i have app accepts integers @ variable rate every .25 2 seconds.
i'd output info in smoothed format 3, 5 or 7 seconds depending on user input.
if info came in @ same rate, let's every .25 seconds, easy. variable rate confuses me.
data might come in this: time - data 0.25 - 100 0.50 - 102 1.00 - 110 1.25 - 108 2.25 - 107 2.50 - 102 ect...
i'd display 3 sec rolling average every .25 seconds on display.
the simplest form of doing set each item array time stamp.
array.push([0.25, 100]) array.push([0.50, 102]) array.push([1.00, 110]) array.push([1.25, 108])
ect...
then every .25 seconds read through array, front, until got time less now() - rollingaveragetime
. sum , display it. .shift()
origin of array.
that seems not efficient though. wondering if had improve way this.
why don't save timestamp of starting value , accumulate values , number of samples until timestamp >= startingtime + rollingaveragetime
, split accumulator number of samples taken?
edit: if want preserve number of samples, can way:
take accumulator, , each input value sum , store value , timestamp in shift register; @ every cycle, have compare latest sample's timestamp oldest timestamp in shift register plus smoothing time; if it's equal or more, subtract oldest saved value accumulator, delete entry shift register , output accumulator, divided smoothing time. if iterate obtain rolling average (i think) to the lowest degree amount of computation each cycle:
a sum (to increment accumulator) a sum , subtraction (to compare timestamp) a subtraction (from accumulator) a partition (to calculate average, done in smart way can shift right)for total of 4 algebric sums , partition (or shift)
edit:for taking business relationship time lastly sample weighting factor, can split value ratio between time , averaging time, , obtain weighted average, without having split accumulator.
i added part because doesn't add together computational load, can implement quite easy if want to.
algorithm
Comments
Post a Comment