Skip to content

Instantly share code, notes, and snippets.

@r3econ
Created April 3, 2014 18:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r3econ/9959500 to your computer and use it in GitHub Desktop.
Save r3econ/9959500 to your computer and use it in GitHub Desktop.
Smooth out the CMMotionManager acceleration readings with Low Pass Filter.
- (CMAcceleration)smoothOutAcceleration:(CMAcceleration)acceleration
{
static CGFloat x0 = 0;
static CGFloat y0 = 0;
static CGFloat z0 = 0;
const NSTimeInterval dt = (1.0 / 20);
const double RC = 0.3;
const double alpha = dt / (RC + dt);
CMAcceleration smoothedAcceleration;
smoothedAcceleration.x = (alpha * acceleration.x) + (1.0 - alpha) * x0;
smoothedAcceleration.y = (alpha * acceleration.y) + (1.0 - alpha) * y0;
smoothedAcceleration.z = (alpha * acceleration.z) + (1.0 - alpha) * z0;
x0 = smoothed.x;
y0 = smoothed.y;
z0 = smoothed.z;
return smoothedAcceleration;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment