KHAAAAAAAAN
KHAAAAAAAAN

Reputation: 161

Reducing Noise in iPad Accelerometer

I'm collecting data from the iPad's accelerometer in only one direction, and it comes out quite noisy. I have looked around for noise reducing filters, but haven't found one that I understood (case in point, the Kalman filter). I guess I have two questions, is there actual significant noise associated with the accelerometer, as it appears, and if so how can I reduce it? Even if you have a link to a noise filter with an explanation I would be very grateful.

My app itself is written in swift, and my data analysis is written in python, if that matters.

Upvotes: 0

Views: 626

Answers (1)

Patrick Lynch
Patrick Lynch

Reputation: 2782

I've used some simple easing that smoothes out any spikes in the values. It'll add a bit of latency, but you can determine the balance of latency vs. smoothness to suit your application by adjusting the easing property.

import UIKit
import CoreMotion

class MyViewController: UIViewController {

    var displayLink: CADisplayLink?
    let motionQueue = NSOperationQueue()

    var acceleration = CMAcceleration()

    var smoothAcceleration = CMAcceleration() {
        didSet {
            // Update whatever needs acceleration data
        }
    }

    var easing: Double = 10.0

    override func viewDidLoad() {
        super.viewDidLoad()

        self.displayLink = CADisplayLink(target: self, selector: "updateDisplay:" )
        self.displayLink?.addToRunLoop( NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode )

        var coreMotionManager = CMMotionManager()
        coreMotionManager.startAccelerometerUpdatesToQueue( self.motionQueue ) { (data: CMAccelerometerData!, error: NSError!) in
            self.acceleration = data.acceleration
        }
    }

    func updateDisplay( displayLink: CADisplayLink ) {
        var newAcceleration = self.smoothAcceleration
        newAcceleration.x += (self.acceleration.x - self.smoothAcceleration.x) / self.easing
        newAcceleration.y += (self.acceleration.y - self.smoothAcceleration.y) / self.easing
        newAcceleration.z += (self.acceleration.z - self.smoothAcceleration.z) / self.easing

        self.smoothAcceleration = newAcceleration
    }
}

Upvotes: 5

Related Questions