Question

Getting data from the CMMotionManager is fairly straight forward, processing it not so much.

Does anybody have any pointers to code for relatively accurately detecting a step (and ignoring smaller movements) or guidelines in a general direction how to go about such a thing?

Était-ce utile?

La solution

What you basically need is a kind of a Low Pass Filter that will allow you to ignore small movements. Effectively, this “smooths” out the data by taking out the jittery.

- (void)updateViewsWithFilteredAcceleration:(CMAcceleration)acceleration
{
    static CGFloat x0 = 0;
    static CGFloat y0 = 0;

    const NSTimeInterval dt = (1.0 / 20);
    const double RC = 0.3;
    const double alpha = dt / (RC + dt);

    CMAcceleration smoothed;
    smoothed.x = (alpha * acceleration.x) + (1.0 - alpha) * x0;
    smoothed.y = (alpha * acceleration.y) + (1.0 - alpha) * y0;

    [self updateViewsWithAcceleration:smoothed];

    x0 = smoothed.x;
    y0 = smoothed.y;
}

The alpha value determines how much weight to give the previous data vs the raw data. The dt is how much time elapsed between samples. RC value controls the aggressiveness of the filter. Bigger values mean smoother output.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top