Skip to content

Instantly share code, notes, and snippets.

@burnto
Forked from huned/simple_moving_average.js
Created May 30, 2010 17:27
Show Gist options
  • Save burnto/419182 to your computer and use it in GitHub Desktop.
Save burnto/419182 to your computer and use it in GitHub Desktop.
Array.prototype.simple_moving_average = function(window_length) {
var averaged = new Array(this.length);
var chunk = [];
for (k = 0; k < this.length; k++) {
chunk.push(this[k]);
if (chunk.length > window_length) chunk.shift();
averaged[k] = chunk.sum() / chunk.length; // sum() is an exercise left to the reader
}
return averaged;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment