Search code examples
ioshardware-accelerationcore-motion

How can i use the accererometer to check whether the device is stable?


I know how to get the accel data but was wondering if anyone knows of any code samples that would determine whether the ios device is stable (swaying or shaking)


Solution

  • Store the last 30 samples of the accelerometer, get per-component averages (x, y, z) and standard deviation for each. Define how little deviation is "stable".

    Obj-C: Calculate the standard deviation of an NSArray of NSNumber objects?

    That will do. Make 3 arrays, each will have 30(or more or less depening on the time frame you want to compare) nsnumbers. You will get 3 standard deviations. The smaller those numbers are, the less the device is moving. Check that the 3 of them are smaller than any arbitrary amount you want, like 0.1.s