Created
April 3, 2014 18:02
-
-
Save r3econ/9959500 to your computer and use it in GitHub Desktop.
Smooth out the CMMotionManager acceleration readings with Low Pass Filter.
This file contains 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
- (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