MATLAB: Velocity integrating to get displacement issues

acceleration integration

Hello, I am recording the accelerometer data and I want to get an displacement from acceleration. This means, that I have to integrate acceleration twice. When I integrate acceleration first time I get velocity and I am satisfied with the result (see attached). But strange thing happens when I integrate velocity to get a displacement. The graph is just not right. It should be pretty much the same as velocity. I guess the cumtrapz should integrate velocity the same way as it integrates acecleration. Do you have any idea what am I doing wrong? Thanks in advance
taxis=xlsread('test.xls', 'A:A');
yaxis=xlsread('test.xls','C:C');
length=30;
mask = taxis > length;
taxis(mask) = [];
yaxis(mask) = [];
figure(1)
plot(taxis, yaxis)
xlabel('Time, s')
ylabel('Acceleration in Y axis, m/s^2')
grid on
vel=cumtrapz(taxis,yaxis);
detrend_vel=detrend(vel);
figure(2)
plot(taxis, vel)
hold on
[pks,locs] = findpeaks(detrend_vel, 'minpeakdistance',5, 'minpeakheight', 0.2);
plot(taxis, detrend_vel, 'r',taxis(locs), pks, 'or')
grid on
xlabel('Time, s')
ylabel('Velocity, m/s')
legend('Raw','Detrended')
displacement=cumtrapz(taxis,vel);
detrend_displacement=detrend(displacement);
figure(3)
plot(taxis, displacement)
hold on
plot(taxis, detrend_displacement)
grid on
xlabel('Time, s')
ylabel('Displacement, m')
legend('Raw','Detrended')

Best Answer

Try to cumtrapz the detrended velocity
displacement=cumtrapz(taxis,detrend_vel);
detrend_displacement=detrend(displacement);
Related Question