[ACCEPTED]-How to detect walking with Android accelerometer-kalman-filter

Accepted answer
Score: 24

Google provides an API for this called DetectedActivity that 8 can be obtained using the ActivityRecognitionApi. Those docs can 7 be accessed here and here.

DetectedActivity has the method public int getType() to get 6 the current activity of the user and also 5 public int getConfidence() which returns a value from 0 to 100. The 4 higher the value returned by getConfidence(), the more 3 certain the API is that the user is performing 2 the returned activity.

Here is a constant 1 summary of what is returned by getType():

  • int IN_VEHICLE The device is in a vehicle, such as a car.
  • int ON_BICYCLE The device is on a bicycle.
  • int ON_FOOT The device is on a user who is walking or running.
  • int RUNNING The device is on a user who is running.
  • int STILL The device is still (not moving).
  • int TILTING The device angle relative to gravity changed significantly.
  • int UNKNOWN Unable to detect the current activity.
  • int WALKING The device is on a user who is walking.
Score: 5

My first intuition would be to run an FFT 18 analysis on the sensor history, and see 17 what frequencies have high magnitudes when 16 walking.

It's essentially seeing what walking 15 "sounds like", treating the accelerometer 14 sensor inputs like a microphone and seeing 13 the frequencies that are loud when walking 12 (in other words, at what frequency is the 11 biggest acceleration happening).

I'd guess 10 you'd be looking for a high magnitude at 9 some low frequency (like footstep rate) or 8 maybe something else. It would be interesting 7 to see the data.

My guess is you run the 6 FFT and look for the magnitude at some frequency 5 to be greater than some threshold, or the 4 difference between magnitudes of two of 3 the frequencies is more than some amount. Again, the 2 actual data would determine how you attempt 1 to detect it.

Score: 4

For walking detection I use the derivative 7 applied to the smoothed signal from accelerometer. When 6 the derivative is greater than threshold 5 value I can suggest that it was a step. But 4 I guess that it's not best practise, furthermore 3 it only works when the phone is placed in 2 a pants pocket.

The following code was used 1 in this app https://play.google.com/store/apps/details?id=com.tartakynov.robotnoise

    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER){
        final float z = smooth(event.values[2]); // scalar kalman filter                               
        if (Math.abs(z - mLastZ) > LEG_THRSHOLD_AMPLITUDE)
            mInactivityCount = 0;
            int currentActivity = (z > mLastZ) ? LEG_MOVEMENT_FORWARD : LEG_MOVEMENT_BACKWARD;                  
            if (currentActivity != mLastActivity){
                mLastActivity = currentActivity;
        } else {
            if (mInactivityCount > LEG_THRSHOLD_INACTIVITY) {
                if (mLastActivity != LEG_MOVEMENT_NONE){
                    mLastActivity = LEG_MOVEMENT_NONE;
            } else {
        mLastZ = z;
Score: 0

EDIT: I don't think it's accurate enough 8 since when walking normally the average 7 acceleration would be near 0. The most you 6 could do measuring acceleration is detect 5 when someone starts walking or stops (But 4 as you said, it's difficult to filter it 3 from the device moved by someone standing 2 at one place)

So... what I wrote earlier, probably 1 wouldn't work anyway:

You can "predict" whether the user is moving by discarding when the user is not moving (obvious), And first two options coming to my mind are:
Check whether the phone is "hidden", using proximity and light sensor (optional). This method is less accurate but easier.
Controlling the continuity of the movement, if the phone is moving for more than... 10 seconds and the movement is not despicable, then you consider he is walking. I know is not perfet either, but it's difficult wihout using any kind of positioning, by the way... why don't you just use LocationManager?

Score: 0

Try detecting the up and down oscillations, the 24 fore and aft oscillations and the frequency 23 of each and make sure they stay aligned 22 within bounds on average, because you would 21 detect walking and specifically that person's 20 gait style which should remain relatively 19 constant for several steps at once to qualify 18 as moving. As long as the last 3 oscillations 17 line up within reason then conclude walking 16 is occurring as long as this also is true:-

You 15 measure horizontal acceleration and update 14 a velocity value with it. Velocity will 13 drift with time, but you need to keep a 12 moving average of velocity smoothed over 11 the time of a step, and as long as it doesn't 10 drift more than say half of walking speed 9 per 3 oscillations then it's walking but 8 only if it initially rose to walking speed 7 within a short time ie half a second or 6 2 oscillations perhaps.
All of that should 5 just about cover it. Of course, a little 4 ai would help make things simpler or just 3 as complex but amazingly accurate if you 2 considered all of these as inputs to a NN. Ie 1 preprocessing.

More Related questions