主要内容

通过从移动设备捕获加速数据来计算步骤

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.

从MATLAB移动设置登录到MathWorks®云。

Create a Connection to Your Mobile Device

On theCommandsscreen of MATLAB Mobile, use themobiledev命令创建代表您的移动设备的对象。

M = MobileDev;

The displayed output should showConnected: 1, indicating that themobiledevobject has successfully established a connection to the app.

准备数据获取

在设备上启用加速传感器。

m.AccelerationSensorEnabled = 1;

Start Acquiring Data

After enabling the sensor, theSensorsscreen of MATLAB Mobile will show the current data measured by the sensor. TheLoggingproperty allows you to begin sending sensor data tomobiledev.

m.Logging = 1;

The device is now transmitting sensor data.

在伐木过程中,设备在四处走动时被固定或放在口袋里。无论设备方向如何,这都会在所有三个轴上产生加速度的变化。

Stop Acquiring Data

TheLoggingproperty is used to again to have the device stop sending sensor data tomobiledev.

m.Logging = 0;

Retrieve Logged Data

accellogis used to retrieve the XYZ acceleration data and timestamps transmitted from the device tomobiledev.

[a,t] = accellog(m);

Plot Raw Sensor Data

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

情节(t,a);传奇('X','Y','Z');Xlabel('Relative time (s)');ylabel(加速度(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.

情节(t,mag);Xlabel('Time (s)');ylabel(加速度(M/S^2)');

的显示,acc的阴谋eleration magnitude is not zero-mean. Subtracting the mean from the data will remove any constant effects, such as gravity.

Magnog = mag-平均(MAG);情节(t,magnog);Xlabel('Time (s)');ylabel(加速度(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

Findpeaksis 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;情节(t(locs),pks,'r',“标记”,'v','linestyle','none');标题(“计数步骤”);Xlabel('Time (s)');ylabel('Acceleration Magnitude, No Gravity (m/s^2)');holdoff;

Clean Up

Turn off the acceleration sensor and clearmobiledev.

m.AccelerationSensorEnabled = 0; clearm;