MPU6050 display raw data and fft from the reading get

10 views (last 30 days)
from this code what is the problem that i faced because the fft not show
a = arduino;
imu = mpu6050(a);
xlabel('Count');
ylabel('Acceleration (m/s^2)');
title('Acceleration values from mpu6050 sensor (Z-axis only)');
z_val = animatedline('Color','b');
axis tight;
legend('Acceleration in Z-axis');
stop_time = 100;
count = 1;
tic;
while(toc <= stop_time)
[accel] = readAcceleration(imu);
if ~isempty(accel)
addpoints(z_val,count,accel(:,3));
% Compute FFT of the Z-axis acceleration
fft_z = fft(accel(:,3));
n = length(fft_z);
freq = (0:n-1)*(100/stop_time)/n; % Assuming sampling rate based on stop_time
% Plot the FFT magnitude
subplot(2,1,2);
plot(freq, abs(fft_z));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('FFT of Z-axis Acceleration');
drawnow limitrate;
count = count + 1;
end
end

Answers (2)

Walter Roberson
Walter Roberson on 14 Jan 2025
Edited: Walter Roberson on 14 Jan 2025
[accelReadings,timestamp] = readAcceleration(sensor) returns one sample of the acceleration data from the MPU-6050 IMU sensor. The acceleration sample is returned as a 3-by-1 vector in m/s2 along the X, Y, and Z axes. The function also returns the timestamp at which MATLAB® receives the acceleration data from the MPU-6050 sensor. The timestamp output is optional.
So accel(:,3) is a single sample, and fft() of a single sample returns a scalar value (the same as the original value, it happens.)
plot() of a single value by default displays nothing. plot() by default displays lines only when there are at least two adjacent finite values, and by default plot() does not display any markers.
You need to keep a record of all of the inputs (or at least of the last so-many inputs)

William Rose
William Rose on 14 Jan 2025
Edited: William Rose on 14 Jan 2025
[edit: fix spelling errors]
Do you get an error when you run this code?
I don't have the arduino add-on package, so I cannot confirm my analysis below.
Your code:
tic
while(toc <= stop_time)
[accel] = readAcceleration(imu);
if ~isempty(accel)
addpoints(z_val,count,accel(:,3));
% Compute FFT of the Z-axis acceleration
fft_z = fft(accel(:,3));
n = length(fft_z);
freq = (0:n-1)*(100/stop_time)/n; % Assuming sampling rate based on stop_time
% Plot the FFT magnitude
subplot(2,1,2);
plot(freq, abs(fft_z));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('FFT of Z-axis Acceleration');
drawnow limitrate;
count = count + 1;
end
end
Move the FFT calculation and FFT display outside of the while loop. As it stands above, accel is a 3-element vector on each pass through the loop, so fft_z=fft(accel(:,3)) is the FFT of a single value, on each pass, and n=1, and
freq=(0:n-1)*100/(stop_time)/n is a scalar, =0.
It seems count is unused.
The calculation of vector freq is incorrect. See correction below.
I would rearrange your code to something like
accel=[];
tic
while(toc <= stop_time)
[new_acc] = readAcceleration(imu);
if ~isempty(accel)
addpoints(z_val,count,accel(:,3));
accel=[accel;new_acc];
end
drawnow limitrate;
end
% Compute FFT of the Z-axis acceleration
fft_z = fft(accel(:,3));
n = length(fft_z);
freq = (0:n-1)/stop_time; % Assuming sampling rate based on stop_time
% Plot the FFT magnitude
figure
plot(freq, abs(fft_z));
xlabel('Frequency (Hz)');
ylabel('Magnitude (m/s^2)');
title('FFT of Z-axis Acceleration');
You assume above that the acceleration samples are acquired at a constant rate. Is that a good assumption? I don't know. If you want to check, you could get the timestamps as well as the accelerations, each time you call readAcceleration().
accel=[]; times=[];
tic
while(toc <= stop_time)
[new_acc,timestamp] = readAcceleration(imu);
if ~isempty(accel)
addpoints(z_val,count,accel(:,3));
accel=[accel;new_acc];
times=[times;timestamp];
end
drawnow limitrate;
end
% and so on
Then you could check the intervals between the timestamps, or you could interpolate the acceleration samples to a constant sampling rate. Good luck with your project.

Community Treasure Hunt

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

Start Hunting!