RosettaCodeData/Task/Averages-Simple-moving-average/AutoHotkey/averages-simple-moving-average-2.ahk
2023-07-01 13:44:08 -04:00

11 lines
452 B
AutoHotkey

MovingAverage(x,len="") { ; for floating point numbers
Static
Static n:=0, m:=10 ; default averaging length = 10
If (len>"") ; non-blank 2nd parameter: set length, reset
n := i := 0, m := len
n += n < m, sum := 0
v%i% := x, i := mod(i+1,m) ; remember last m inputs, cycle insertion point
Loop %n% ; recompute sum to avoid error accumulation
j := A_Index-1, sum += v%j%
Return sum/n
}