Created
April 7, 2014 10:42
-
-
Save uhho/10018102 to your computer and use it in GitHub Desktop.
Low Pass Filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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