My codes won't show in the plots.

1 回表示 (過去 30 日間)
Emmanuel Iroulor
Emmanuel Iroulor 2021 年 10 月 23 日
コメント済み: Emmanuel Iroulor 2021 年 10 月 23 日
%% I wrote this program, it runs but doesn't show in the plot. Can someone help me ?
% FFT Coding Practice
clear all
clc
A = arduino('COM3', 'Mega2560');
n = 10;
for i =0:n;
vo1 = readVoltage(A,'A0');
vo2 = readVoltage(A,'A1');
vo3 = readVoltage(A,'A2');
end
Vx = vo1*1000;
Vy = vo2*1000;
Vz = vo3*1000;
Ax = (Vx - 2496.2)/333.7;
Ay = (Vy - 2533.9)/ 346.4;
Az = (Vz - 2492.5)/334.1;
X = Ax;
Y= Ay;
Z = Az;
fprintf(' X %d, Y %d , Z %d \n',X,Y,Z);
% for a = 1:n-1
fs = 1/0.050; % sampling frequency
L1 = length(X)
N=1024; % Block size
f = fs/2*linspace(0,1,N/2+1); % Frequency values
subplot(311)
XX = fft(X,N)/L1; % Accel. along X axis
plot(f,2*abs(XX(1:N/2+1))); grid on;
title('One-sided Amplitude Spectrum');
subplot(312)
YY = fft(Y,N)/L1; % Accel. along Y axis
plot(f, 2*abs(YY(1:N/2+1))); grid on;
subplot(313)
ZZ = fft(Z,N)/L1; % Accel. along Z axis
plot(f,2*abs(ZZ(1:N/2+1))); grid on;
figure(2)
plot(f, 2*abs(XX(1:N/2+1)), 'r',f,2*abs(YY(1:N/2+1)), 'g',f,2*abs(ZZ(1:N/2+1)), 'b'); grid on;
xlabel('f, frequency, [Hz]')
legend('Acc X ','Acc Y','Acc Z')
title('One-sided Amplitude Spectrum')
  2 件のコメント
DGM
DGM 2021 年 10 月 23 日
If nothing shows up, have you verified that X, Y, and Z are actually valid?
X = rand(10,1);
Y = rand(10,1);
Z = rand(10,1);
fs = 1/0.050; % sampling frequency
L1 = length(X);
N=1024; % Block size
f = fs/2*linspace(0,1,N/2+1); % Frequency values
subplot(311)
XX = fft(X,N)/L1; % Accel. along X axis
plot(f,2*abs(XX(1:N/2+1))); grid on;
title('One-sided Amplitude Spectrum');
subplot(312)
YY = fft(Y,N)/L1; % Accel. along Y axis
plot(f, 2*abs(YY(1:N/2+1))); grid on;
subplot(313)
ZZ = fft(Z,N)/L1; % Accel. along Z axis
plot(f,2*abs(ZZ(1:N/2+1))); grid on;
figure(2)
plot(f, 2*abs(XX(1:N/2+1)), 'r',f,2*abs(YY(1:N/2+1)), 'g',f,2*abs(ZZ(1:N/2+1)), 'b'); grid on;
xlabel('f, frequency, [Hz]')
legend('Acc X ','Acc Y','Acc Z')
title('One-sided Amplitude Spectrum')
Otherwise, you'll have to provide example response data. I can't shake your arduino from here.
Emmanuel Iroulor
Emmanuel Iroulor 2021 年 10 月 23 日
Thanks DGM

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2021 年 10 月 23 日
for i =0:n;
vo1 = readVoltage(A,'A0');
vo2 = readVoltage(A,'A1');
vo3 = readVoltage(A,'A2');
end
Every time through the loop, you overwrite all of vo1, vo2, vo3. After the loop, vo1 will have only the value that it got when i == n, likewise vo2 and vo3 are going to hold only the last values they were assigned.
fprintf(' X %d, Y %d , Z %d \n',X,Y,Z);
That line would not have worked properly if X, Y, Z were not scalars because you had saved all of the vo* entries. You would need
fprintf(' X %d, Y %d , Z %d \n',[X(:),Y(:),Z(:)].');
Then
XX = fft(X,N)/L1; % Accel. along X axis
X is a scalar as it is constructed from the vo1 values and you only saved one vo1 value. The output of the fft is going to have the (scalar) value of X in the first entry, and be effectively 0 in the other entries.
You need to save all of the vo* entries. For example,
n = 10;
vo1 = zeros(n+1, 1);
vo2 = zeros(n+1, 1);
vo3 = zeros(n+1, 1);
for i = 0:n;
vo1(i+1) = readVoltage(A,'A0');
vo2(i+1) = readVoltage(A,'A1');
vo3(i+1) = readVoltage(A,'A2');
end
I recommend that you add comments documenting your design decision to count from 0 to n instead of counting from 1 to n+1, or making n one less and counting 1 to n.
  1 件のコメント
Emmanuel Iroulor
Emmanuel Iroulor 2021 年 10 月 23 日
Thank you Walter Roberson. Your response was helpful.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSignal Attributes and Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by