Skip to content

Instantly share code, notes, and snippets.

@riston
Created September 9, 2015 20:30
Show Gist options
  • Save riston/28e8ceb8b9803187afd3 to your computer and use it in GitHub Desktop.
Save riston/28e8ceb8b9803187afd3 to your computer and use it in GitHub Desktop.
Exponential moving average for streams
// Exponential moving average
function average (alpha) {
var last;
alpha = alpha || 3/4;
return function (input) {
if (!last) {
last = input;
return last;
}
last = last + alpha * (input - last);
return last;
}
}
var a = average(1/3);
a(1);
a(2);
a(3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment