문서

## Counting Steps by Capturing Acceleration Data from Your Mobile Device

This example shows how to collect acceleration data from an Android™ or iOS mobile device and use it to count the number of steps taken.

This example requires Signal Processing Toolbox™.

### Set Up Your Mobile Device

In order to receive data from a mobile device in MATLAB®, you will need to install and set up the MATLAB Mobile™ app on your mobile device.

### Create a Connection to Your Mobile Device

On the Commands screen of MATLAB Mobile, use the `mobiledev` command to create an object that represents your mobile device.

`m = mobiledev;`

The displayed output should show `Connected: 1`, indicating that the `mobiledev` object has successfully established a connection to the app.

### Prepare for Data Acquisition

Enable the acceleration sensor on the device.

`m.AccelerationSensorEnabled = 1;`

### Start Acquiring Data

After enabling the sensor, the Sensors screen of MATLAB Mobile will show the current data measured by the sensor. The `Logging` property allows you to begin sending sensor data to `mobiledev`.

`m.Logging = 1;`

The device is now transmitting sensor data.

During logging, the device is held or kept in a pocket while walking around. This generates changes in acceleration across all three axes, regardless of device orientation.

### Stop Acquiring Data

The `Logging` property is used to again to have the device stop sending sensor data to `mobiledev`.

`m.Logging = 0;`

### Retrieve Logged Data

`accellog` is used to retrieve the XYZ acceleration data and timestamps transmitted from the device to `mobiledev`.

`[a,t] = accellog(m);`

### Plot Raw Sensor Data

The logged acceleration data for all three axes can be plotted together.

```plot(t,a); legend('X', 'Y', 'Z'); xlabel('Relative time (s)'); ylabel('Acceleration (m/s^2)');``` ### Process Raw Acceleration Data

To convert the XYZ acceleration vectors at each point in time into scalar values, the magnitude is calculated. This allows large changes in overall acceleration, such as steps taken while walking, to be detected regardless of device orientation.

```x = a(:,1); y = a(:,2); z = a(:,3); mag = sqrt(sum(x.^2 + y.^2 + z.^2, 2));```

The magnitude is plotted to visualize the general changes in acceleration.

```plot(t,mag); xlabel('Time (s)'); ylabel('Acceleration (m/s^2)');``` The plot shows that the acceleration magnitude is not zero-mean. Subtracting the mean from the data will remove any constant effects, such as gravity.

```magNoG = mag - mean(mag); plot(t,magNoG); xlabel('Time (s)'); ylabel('Acceleration (m/s^2)');``` The plotted data is now centered about zero, and clearly shows peaks in acceleration magnitude. Each peak corresponds to a step being taken while walking.

### Count the Number of Steps Taken

`findpeaks` is a function from Signal Processing Toolbox that is used to find the local maxima of the acceleration magnitude data. Only peaks with a minimum height above one standard deviation are treated as a step. This threshold should be tuned experimentally to match a person's level of movement while walking, hardness of floor surfaces, etc.

```minPeakHeight = std(magNoG); [pks,locs] = findpeaks(magNoG,'MINPEAKHEIGHT',minPeakHeight);```

The number of steps taken is simply the number of peaks found.

`numSteps = numel(pks)`

The peak locations can be visualized with the acceleration magnitude data.

```hold on; plot(t(locs), pks, 'r', 'Marker', 'v', 'LineStyle', 'none'); title('Counting Steps'); xlabel('Time (s)'); ylabel('Acceleration Magnitude, No Gravity (m/s^2)'); hold off;``` ### Clean Up

Turn off the acceleration sensor and clear `mobiledev`.

```m.AccelerationSensorEnabled = 0; clear m;```
{"ft_lang_label":"__label__en","ft_lang_prob":0.7863421,"math_prob":0.85350764,"size":3319,"snap":"2019-51-2020-05","text_gpt3_token_len":756,"char_repetition_ratio":0.14751132,"word_repetition_ratio":0.019880716,"special_character_ratio":0.23049112,"punctuation_ratio":0.1583851,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9852222,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,1,null,1,null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-27T00:04:41Z\",\"WARC-Record-ID\":\"<urn:uuid:46f069c3-405b-4ee6-a54f-d99246ff126a>\",\"Content-Length\":\"35374\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2e795d78-adbb-4e15-95b0-bfe74de5ff2b>\",\"WARC-Concurrent-To\":\"<urn:uuid:a3082fd7-7714-472b-9368-4740b1337550>\",\"WARC-IP-Address\":\"104.110.193.39\",\"WARC-Target-URI\":\"https://kr.mathworks.com/help/matlabmobile/ug/counting-steps-by-capturing-acceleration-data.html\",\"WARC-Payload-Digest\":\"sha1:URU3EYZDYFYQVRPS2KIEFOJBEQI44MCA\",\"WARC-Block-Digest\":\"sha1:WROBMYYEUHVV6IRFT3WJIYEFD4ZPRJYR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579251694071.63_warc_CC-MAIN-20200126230255-20200127020255-00404.warc.gz\"}"}