# Integrate Acceleration to velocity, wrong plot

7 views (last 30 days)
Daniel Basow on 16 Jul 2021
Answered: John D'Errico on 16 Jul 2021
I try to use my Smartphonesensor to check if the theoretical speed of the Roboter ist the same like the real speed.
So I installed the Matlab App and used the acceleration sensor to check it. Because i didn´t mounted the smartphone totally zero in every direction (x,y,z) i needed to subtract an offset. I took the offset from the resting time bevore the movement, here i took the values from 300-500 (100hz) and calculated the average. Because i had sensor noice i also added a gauß filter. At the End i had two plots one in x-direction the over in y-direction.
So the plot for the Acceleration in the x-Direction looks very good and seems to be right.
And the Velocity of the x-direction, which i calculated with this code, doesnt look right:
velocity_y = cumtrapz(t_LP,y_ac');
plot(t_LP,velocity_y,'g')
How you can see there is an rising chart, which in the end has an velocity of 0.5 m/s but i the velocity should be zero.
I think the offset is not totally exact and after the integration i get an big mistake so the chart ist frequently rising. Does anyone knows how to erease this mistake?
Mathieu NOE on 16 Jul 2021
Your velocity graph looks ok to me - what did you expect ?
you can do the following test : derivation of the velocity (using diff or gradient) and you'll fall back on your initial acceleration data.

Mathieu NOE on 16 Jul 2021
so finally... try this / adapt it to your own needs .
FYI : this code does two integrations, to get velocity and displacement (this may not really interest you, but I leave it just in case)
clc
clearvars
% Acceleration data
acc = acc*9.81; % Add gravity effect
acc = detrend(acc);
% Time
tStep = 0.00048828; % Length of each time step
N = length(acc)*tStep;
t = 0:tStep:N;
t(end) = [];
N = length(t);
dt = mean(diff(t)); % Average dt
fs = 1/dt; % Frequency [Hz] or sampling rate
% some additionnal high pass filtering
N = 2;
fc = 0.5; % Hz
[B,A] = butter(N,2*fc/fs,'high');
acc2 = filter(B,A,acc);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
velocity = cumtrapz(dt,acc2);
velocity = detrend(velocity);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp = cumtrapz(dt,velocity);
%
figure(1)
plot(t,acc);
figure(2)
plot(t,velocity);
figure(3)
plot(t,disp);
Mathieu NOE on 16 Jul 2021
hello again
I just come back on test1 data and did a quick check ... with still a couple of questions to you :
• your acceleration data are expressed in g or in m/s² ?
• in the beginning of your post, you mention that your "rested" signal (sensor at rest) correspond to samples 300 to 500 but in the code below , for the y direction I could not really see that (would correspond to time between 6 and 10 s)
• so in my case, as "reference" zero acceleration I took the average accel value from t = 0 to 1.7 s and that give me this result. As we can see the velocity remains zero until t = 1.7 s , which is ok - as we consider this as "zero" acceleration - once we have corrected the accel data
• but after we have a trend and I cannot say if it's real of an numerical artifact (which shoud not be the case if we are sure if the calibration)
• if we change the static acceleration correction (calibration), this of course will create a different linear trend but that is not "scientific" by any means , as the only truly reliable method is to a have a record (long enough) with garanteed zero mechanical acceleration; after that there can be arguments about how accurate and reliable is the sensor and electronic system used in this experiment
code for my quick test
clc
clearvars
x_ac = Acceleration.X; %Beschl. x-Achse
y_ac = Acceleration.Y; %Beschl. x-Achse
z_ac = Acceleration.Z; %Beschl. x-Achse
% Time
tStep = 0.02; % Length of each time step
N = length(x_ac);
t_ac = (0:N-1)*tStep;
fs = 1/tStep; % Frequency [Hz] or sampling rate
% acc = y_ac*9.81; % Add No gravity factor (assumes data in g)
acc = y_ac; % No gravity factor (assumes data in m/s²)
% remove "rest" acceleration (must start at zero)
ind = find(t_ac>0 & t_ac< 1.7);
acc_ref = mean(acc(ind));
acc = acc - acc_ref; % ??
% acc = acc - mean(acc); % ??
velocity = cumtrapz(tStep,acc);
figure(1)
subplot(211),plot(t_ac,acc);
title('acceleration (unit ?)');
subplot(212),plot(t_ac,velocity);
title('velocity (unit ?)');
xlabel('Time (s)')
plot :

John D'Errico on 16 Jul 2021
I would suggest your problem may be a simple one.
When you integrate a function, there is a constant of integration, which is unknown. It can be any value. Effectively, it comes down to the idea that you need to know the INITIAL velocity. Or the final velocity. Or the velocity at any given point in time. Think of it as if you are solving a differential equation. You need that initial value before the solution is complete.
Again, the integral of acceleration is velocity + C. So if you know that the final velocity must be zero, then you can correct for that by subtracting the final velocity that was obtained. Subtract that value from the entire curve.
The implicit assumption with cumtrapz is that the INITIAL velocty was zero. If that is not true, this is the source of your problem.

R2019b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!