1

The scenario is like this. I need to generate 90,100 random numbers and on that number I need to compute the simple moving average?

What method is the best to compute the moving average?

Sam
  • 7,252
  • 16
  • 46
  • 65
Jhayphie
  • 17
  • 4
  • What is your input and desired output? – Patrick Hofman Jun 17 '14 at 09:34
  • 1
    This is a nice question, however we need more information. How many numbers are in the set of numbers, and how many are used to compute the moving average from? Is the average always using the latest numbers? – Marcel Jun 17 '14 at 09:36

1 Answers1

2

Probably the fastest is as follows in pseudo-code:

# 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

So, you actually have one subtract, one divide-by-constant, and one add operation per sample.

DrV
  • 22,637
  • 7
  • 60
  • 72