如何计算简单的移动平均线?

时间:2014-06-17 09:32:20

标签: c# algorithm moving-average computation

场景是这样的。我需要生成90,100个随机数,在这个数字上我需要计算简单的移动平均数?

最好计算移动平均线的方法是什么?

1 个答案:

答案 0 :(得分:2)

伪代码可能最快如下:

# P: input signal array
# S: sum accumulator
# I: counter
# N: number of samples in your sliding average

S <- P[0] + P[1] + ... + P[N-1]

I <- N

forever:
   save_new_result(S / N)
   S <- S - P[I-N] + P[I]
   I <- I + 1

因此,实际上每个样本有一个减法,一个除以常数和一个加法运算。