# 基于安卓采集数据的计步算法
Adaptiv: An Adaptive Jerk Pace Buffer Step Detection Algorithm
===========
## Abstract
Mobile consumer technology today is equipped with a myriad of sophisticated sensors capable of measuring multiple environment variables. One of the most exciting sensors on most mobile devices today is the accelerometer, capable of measuring the device's proper acceleration, i.e. acceleration relative to free fall. Proper acceleration data has many uses in today's mobile applications ranging from, determining the device's orientation, gesture recognition, and detecting user movement, namely step detection. In this git repository we explore the use of android's raw accelerometer data in the application of step detection. Step detection has many uses in today's mobile computing field, including activity tracking as well as providing correction in indoor localization methods. Noticing this significance of accurate step detection, the goal of this repository was to explore a few algorithms in precise step detection.
## Introduction
In this repository we will introduce our algorithm, Adaptiv, an adaptive jerk pace buffer step detection algorithm. First we will explain our method of data collection and filtering, then we will discuss the iterations our step detection algorithm design took that led us to our final implementation.
## Collection (Android App)
In the app file you will find our Android app responsible for logging the accelerometer sensor data and writing that data out to CSV file in external storage. The application itself is a single activity dynamically being updated with the devices live accelerometer data. The bottom row is the Android built-in step detection sensor data, to serve as a reference. At the top is the button that initializes and terminates the trial collection.
![](http://www.writebug.com/myres/static/uploads/2021/10/19/5f009c781648682abfebc3217c9c7cb8.writebug)
## Filtering
The accelerometer data provided by the android service is fairly noisy. High frequency oscillations from the device and ambient environment skew the clean oscillations of a user's natural step. In order to remove this noise, we put the acceleration data through a lowpass band filter to remove the noise and give us a cleaner signal to work with. Below you can see the implemented low pass filter band with a 3.6667 Hz cut-off, as well as the resulting filtered data overlaid on top of the unfiltered raw data.
```bash
bash$ python sensordata/main.py lp
```
![](http://www.writebug.com/myres/static/uploads/2021/10/19/4fe56158dcfe62c5366d133d8477cbcc.writebug)
## Step Counting Algorithms
Once filtered we may then begin implementing step detection algorithms. In this section we will discuss the process that brought us to the Adaptiv, adaptive jerk threshold, method. As a quick note, the blue line data you see in the following graphs is the filtered *r* data. *R* is the scalar magnitude of the three combined acceleration data points: *x*, *y*, and *z*. The advantage of using *r* versus any particular acceleration data direction is that it is impartial to device orientation and can handle dynamic reorientations of the device. How we calculate *r* is given below.
![](http://www.writebug.com/myres/static/uploads/2021/10/19/c9c3f3bad1499de75bb59234546b47bf.writebug)
### Peak Acceleration Threshold
Our first iteration is the standard peak threshold algorithm. We count the number of spikes by setting a threshold and counting the times the data crosses that threshold. The number of steps should be equal to the number of crossings divided by two. On our first iterations we looked at Dan's 100-step trial with a threshold of 10.5
```bash
bash$ python sensordata/main.py pat data/walk/inHand/dan1
```
![](http://www.writebug.com/myres/static/uploads/2021/10/19/7380bbee4348cfcad1544205a582720f.writebug)
Each red dot represents a crossing of our threshold, and as you can see, this algorithm is quite effective at pinpointing the peaks and the resulting steps. In fact, in the above test case, after taking 100 steps the program counted 101 steps, that is 1% error.
However, when applying the same algorithm and threshold to Zhang's 100-step trial, the program only counted 88 steps. In the graph below we see how Zhang's walking style results in steps that do not break the threshold.
```bash
bash$ python sensordata/main.py pat data/walk/inHand/zhang
```
![](http://www.writebug.com/myres/static/uploads/2021/10/19/e44edce492fe0cdf47bdb5376430ba01.writebug)
What we concluded was that a static threshold is not a reliable method for counting steps of individuals with different walking styles. If you lowered the threshold to accept more "glide" walkers, you may be getting false positives for the more "stomp" walkers. We figured this was an unsatisfactory approach.
### Peak Jerk Threshold
The issues of uncommon gaits seemed troublesome. However, what was common throughout Zhang and Dan's gaits was the major drop from peak to trough. We figured if we went deeper, i.e. derived the acceleration data, we could perhaps extract this peak change in acceleration, otherwise known as jerk. So we derived each acceleration data point at index *i* to find j, or jerk. Where *t* is the timestamp at index *i*.
![](http://www.writebug.com/myres/static/uploads/2021/10/19/3557e1d6e633459da29d14fcc323c15d.writebug)
We then graphed the resulting jerk data points...
```bash
bash$ python sensordata/main.py pjt data/walk/inHand/dan1
```
![](http://www.writebug.com/myres/static/uploads/2021/10/19/65b1097dc691ca2877e887161990bce2.writebug)
What we found was that the resulting jerk data set was just as noisy, if not noisier than the orignial acceleration data. We figured that deriving the accelerometer data provided no cleared distinction of a user's step.
### Step Jerk Threshold
We knew these large spikes in jerk were significant, so in our next algorithm we sought out identifying these peaks and troughs programmatically. We achieved this by identifying the crest and trough within a single oscillation. We define an oscillation as three consecutive zero crossings, points A, C, and E in the graph shown below. In our example, a zero crossing is defined by the moment when acceleration is equal to gravity.
![](http://www.writebug.com/myres/static/uploads/2021/10/19/85775493e8e4b5a9f80f712c0bdb27dc.writebug)
In the graph below, red dots are the crest and troughs, and yellow dots are the zero crossings.
```bash
bash$ python sensordata/main.py sjt data/walk/inHand/dan1
```
![](http://www.writebug.com/myres/static/uploads/2021/10/19/38ee522bbbbe6e50e0126d527f610a00.writebug)
We then calculate the change from crest to trough, we call this value Step Jerk. If this value is greater than the threshold we consider the oscillation a step. In the following trial we look at Dan's 100 steps with a step threshold of 1.5.
![](http://www.writebug.com/myres/static/uploads/2021/10/19/2349842c7e194756d7c46b954c2d3e8a.writebug)
In the graph above we mark all valid step troughs with a red dot. Again, as you can see this algorithm is quite effective at pinpointing the steps. The above trial counted 98 steps.
However, yet again, when applying this algorithm to a different walking gait, we receive poor results. Testing Zhang's 100-step trial, the program only counted 48 steps, an even worse result. In the graph below we see how Zhang's walking style results in steps that do not break the jerk threshold.
```bash
bash$ python sensordata/main.py sjt data/walk/inHand/zhang
```
![](http://www.writebug.com/myres/static/uploads/2021/10/19/182a57aee9bee4d7c727fe34910b3e7f.writebug)
We were right back to where we started, however we felt we were on to something. We figured if the step jerk could adapt to the user, and the situation, the algorithm would be better suited to count steps. So we developed a method to do just that...
### Adaptive Step Jerk Threshold
The intuition of this met