Skip to content

Instantly share code, notes, and snippets.

@uhho
Created April 7, 2014 10:42
Show Gist options
  • Save uhho/10018102 to your computer and use it in GitHub Desktop.
Save uhho/10018102 to your computer and use it in GitHub Desktop.
Low Pass Filter
// array of values
var values = [10,8,12,10,11,9,10,12,8,50,10,12,8];
// smoothing whole array
function lpf(values, smoothing){
var value = values[0];
for (var i = 1; i < values.length; i++){
var currentValue = values[i];
value += (currentValue - value) / smoothing;
values[i] = Math.round(value);
}
}
// buffer
var values = [10,8,12,10,11,9,10,12,8,50,10,12,8];
// smoothing value using buffer
function lpfBuffer(nextValue, smoothing) {
// push new value to the end, and remove oldest one
var initial = values.shift();
values.push(nextValue);
// smooth value using all values from buffer
return values.reduce(function(last, current) {
return smoothing * current + (1 - smoothing) * last;
}, initial);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment