Integrating discrete accelerometer data, displacement result too big

4 views (last 30 days)
Hi, I have this code trying to detrend, filter and integrate accelerometer data. My displacement should be oscillating between 15cm to -15cm. The result I get is around 200m. not sure where I went wrong.
Thanks!
t = cell2mat(Time(:))%getting time from accelerometer
acc = AccZ{1}(:)%getting 1axis acceleration from accelerometer
accmps = acc * 9.81; %converting from g's to [m/s^2]
accmps=detrend(accmps);%accmps detrended and in m/s
%plot(t,accmps)
[b,a] = butter(2,0.4)
accmps_filt = filter(b,a,accmps) %afiltering acceleration
%plot(t,accmps_filt)
%title('Acceleration vs. time for 15cm initial displacement')
%xlabel('Time (s)')
%ylabel('Acceleration m/s')
vel = cumtrapz(accmps_filt,t)%integrating acceleration
vel = detrend(vel)%removing any velocity trends
vel_filt = filter(b,a,vel) %filtering velocity
%plot(t,vel)
disp = cumtrapz(vel_filt, t)-0.15 %integrating velocity with 15cm intial displacement
plot(t,disp)
  1 Comment
Torsten
Torsten on 7 Dec 2024
We must work with your acceleration data - we cannot foresee theoretically what the code does with them.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 7 Dec 2024
Observe:
y = randn(1,10)
y = 1×10
0.6540 -1.4476 -1.0262 -0.4446 -0.1849 -0.2907 -1.6271 1.2713 -0.9509 -1.1262
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
cumtrapz(y)
ans = 1×10
0 -0.3968 -1.6337 -2.3691 -2.6838 -2.9216 -3.8806 -4.0584 -3.8983 -4.9369
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Notice that the first output is 0. This is a property of cumtrapz(). cumtrapz() is not treating the inputs as being absolute values: cumptrapz() is treating the inputs as being relative values. The first output location is the first input relative to the first input, so the first output is 0. The second output location is the second output relative to the first input (and divided by 2), and so on.
You cumtrapz() to get the velocity -- so the first velocity is going to be 0.
But relative velocities matter when calculating displacements...

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!