CAPURE DATA SENSORS

Install the mobile Matlab App in your smartphone

In order to capture data from the smartphone sensors you have to install Matlab movile app (Apple Store or Play Store) and open session with your Matlab account.
Check everything works using basic Matlab sentences:
Screenshot_20211115-161645_MATLAB.jpg

Capture Data from sensors

Then open the menu on the app and choose the Sensors menu. Depending on your smartphone, you can access to different data from accelerometers, magnetic field, orientation, angular velocity, position, etc.
Screenshot_20211115-115334_MATLAB.jpg

Capture Accelerometer data

Now let's capture some data from the accelerometer sensor, this is a sensor that measures the rate of change of velocity, the acceleration. It uses a 3-dimensional cartesian coordinate system and returns acceleration values for each of the axes illustrated in the figure below.
To record accelerometer data, use the following steps:
  1. Select Stream to Log.
  2. Adjust the sample rate (default is 10 Hz).
  3. Turn on the Acceleration switch.
  4. Tap START and begin the motion (here to walk)
Screenshot_20211115-115452_MATLAB.jpg
After collecting the data:
  1. Tap STOP.
  2. Enter the file name to be saved (default name is provided).
  3. If you have the Auto Upload option on, the file will be uploaded to your MATLAB Drive as a .mat file to your configured Upload Folder (default is MATLAB Drive/MobileSensorData). (Copyright 2021 The MathWorks, Inc.)

Application Example: Step Counter

Connect to Matlab Online and download the data file (select it + Download (home menu)) and save it on the local current folder.
To visualize the data, first load the .mat file of captured sensor data. According to the recorded motion (here walking), you can identify the vertical axis comparing the results near the g=9.8m/s^2 level. As you can see, here is the z-axis.
load 'walkData.mat'
x = Acceleration.X;
y = Acceleration.Y;
z = Acceleration.Z;
 
plot(x,'r');
hold on;
plot(y,'g');
plot(z,'b');
 

After decide which is the axis corresponding to gravity, we can analyze the recorded data in order to
accelChannel = z; % Acceleration.Z is here the vertical acceleration
mz = mean(accelChannel) %close to gravity value
mz = 9.75669450996563
stz = std(accelChannel)
stz = 2.07911470141584
 
Data = (accelChannel-mz); % center the data to have 0 mean.
numData = size(Acceleration,1);
% plot data and std deviation limits
figure()
plot(Data)
hold on
plot([0,numData],[0,0],'r')
plot([0,numData],[stz,stz],'g')
plot([0,numData],[-stz,-stz],'m')
hold off
As you can see in the above figure, sometimes there are secondary maxima values that does not correspond to proper steps. If we use the std level (green line) as the lower limit for the data, then we can compute the number of maximum points as
figure()
plot(Data)
hold on
plot([0,numData],[0,0],'r')
plot([0,numData],[stz,stz],'g')
plot([0,numData],[-stz,-stz],'m')
level = stz;
[llocMin, valMin, llocMax, valMax] = dataMaxMin(Data);
[irMax,icMax]=find(valMax > level);
plot(llocMax(icMax),valMax(icMax),'sr');
text(llocMax(icMax)+.03,valMax(icMax),num2str((1:numel(valMax(icMax)))'))
[irMin,icMin]=find(valMin < -level);
plot(llocMin(icMin),valMin(icMin),'og');
hold off;
numMax = max(length(irMax),length(icMax));
numMin = max(length(irMin),length(icMin));
fprintf(' num Max = %d, num Min = %d \n',numMax,numMin);
num Max = 35, num Min = 42
As you can see, the number of selected points are too maby. This can be solved changing the cut level
level = 3.4; %aprox the value of the first peak
lloc = find(Data > level); %localize all values above level stz
cutData = zeros(size(Data));
cutData(lloc) = Data(lloc);
plot(cutData)
hold on
[llocMin, valMin, llocMax, valMax] = dataMaxMin(cutData);
[ir,ic]=find(valMax > level);
plot(llocMax(ic),valMax(ic),'sr');
text(llocMax(ic)+.03,valMax(ic),num2str((1:numel(valMax(ic)))'))
hold off;
Still there are some points (here peak number 7) that is too close to the previous one and, therefore, not corresponding to a complete step. In order to filter this case, you can compute the difference (in time) between two consecutive peaks and remove if it is smaller than the rest.
maxDiff = max(diff(llocMax)); %maximum difference between two consecutive peaks
timeStep = diff(llocMax)/maxDiff; %relative time spend between two steps
plot (timeStep,'o-')
row = find(timeStep > 0.4); %discard the closest ones
row=[row, length(llocMax)]; %you loose last point because of the differences
plot(cutData)
hold on
% [llocMin, valMin, llocMax, valMax] = dataMaxMin(cutData);
% [ir,ic]=find(valMax > level);
plot(llocMax(row),valMax(row),'sr');
text(llocMax(row)+.03,valMax(row),num2str((1:numel(valMax(row)))'))
hold off;
Extreme value computation
This function allows you to compute the local extremal values of a vector data
function [llocMin, valMin, llocMax, valMax] = dataMaxMin(x)
% compute the maxima and minima of the data points x
% Input:
% x -> vector data
% Output:
% llocMin -> index of the found minima
% valMin -> values of the original minima
% llocMax -> index of the found maxima
% valMax -> values of the original maxima
numData = length(x);
% compute the derivative
for i=2:numData-1
dx(i) = x(i)-x(i-1);%(x(i+1)-2*x(i)+x(i-1))/2;
end
dx(1) = x(2)-x(1);
dx(numData) = x(end)-x(end-1);
% search for the zeros of the derivative
nMax=0;
nMin=0;
llocMin=[]; valMin=[];
llocMax=[]; valMax=[];
for i=2:numData-1
canvi = dx(i)*dx(i-1);
if (sign(canvi) < 0)
if (dx(i) > 0)
nMin= nMin+1;
llocMin = [llocMin, i-1];
valMin = [valMin, x(i-1)];
else
nMax= nMax+1;
llocMax = [llocMax, i-1];
valMax = [valMax, x(i-1)];
end
end
end
end
% (c) Numerical Factory 2023
© Numerical Factory 2023