Skip to content

Instantly share code, notes, and snippets.

@KurtWM
Created December 3, 2014 12:16
Show Gist options
  • Save KurtWM/d4f37b25ca75beb93e89 to your computer and use it in GitHub Desktop.
Save KurtWM/d4f37b25ca75beb93e89 to your computer and use it in GitHub Desktop.
var accelFilter = function ( x, y, z ) {
//ramp-speed - play with this value until satisfied (value is a float number)
var kFilteringFactor = 0.1,
//last result storage - keep definition outside of this function, eg. in wrapping object (values are floats)
accel[3],
highPassFilter = function () {
//acceleration.x,.y,.z is the input from the sensor
//result.x,.y,.z is the filtered result
//high-pass filter to eliminate gravity
accel[0] = acceleration.x * kFilteringFactor + accel[0] * (1.0 - kFilteringFactor);
accel[1] = acceleration.y * kFilteringFactor + accel[1] * (1.0 - kFilteringFactor);
accel[2] = acceleration.z * kFilteringFactor + accel[2] * (1.0 - kFilteringFactor);
result.x = acceleration.x - accel[0];
result.y = acceleration.y - accel[1];
result.z = acceleration.z - accel[2];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment