I'm wanting to calculate exponential moving averages of a variable, distance
. Is the logic (and math) correct in the below code?
time
and lastTime
are millisecond-precise timestamps in seconds – the former is the current time, the latter is the time of the last calculations.
lastA
etc are the exponential moving averages from the last calculations.
a
etc will be left with the calculated exponential moving averages.
var distance = ...;
var a = Math.pow(1.16, -(time-lastTime)),
b = Math.pow(1.19, -(time-lastTime)),
c = Math.pow(1.22, -(time-lastTime)),
d = Math.pow(1.26, -(time-lastTime)),
e = Math.pow(1.30, -(time-lastTime)),
f = Math.pow(1.35, -(time-lastTime)),
g = Math.pow(1.40, -(time-lastTime));
a = a*lastA + (1-a)*distance;
b = b*lastB + (1-b)*distance;
c = c*lastC + (1-c)*distance;
d = d*lastD + (1-d)*distance;
e = e*lastE + (1-e)*distance;
f = f*lastF + (1-f)*distance;
g = g*lastG + (1-g)*distance;