Error using plot Vectors must be the same length

조회 수: 2 (최근 30일)
Ha
Ha 2023년 3월 22일
댓글: Matt J 2023년 3월 22일
% Define time range
t = -0.2:0.0001:0.2;
% Calculate signal s(t)
s = cos(2*pi*10*t).*cos(27*pi*200*t);
% Plot s(t)
figure(1);
plot(t, s);
xlabel('Time (s)');
ylabel('Amplitude');
title('AM Modulated Signal s(t)');
% Calculate Fourier transform of s(t)
Fs = 10000; % Sampling frequency
S = fftshift(fft(s))/length(s);
size(S)
ans = 1×2
1 4001
f = (-Fs/2:Fs/2-1)/length(s);
size(f)
ans = 1×2
1 10000
% Plot two-sided spectrum of S(f)
figure(2);
plot(f, abs(S));
Error using plot
Vectors must be the same length.
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Two-Sided Spectrum of S(f)');

답변 (1개)

Torsten
Torsten 2023년 3월 22일
이동: Matt J 2023년 3월 22일
As you can see above, S and f have a different number of elements. So they cannot be plotted against each other.
  댓글 수: 2
Ha
Ha 2023년 3월 22일
I didn't understand what you mean..but what is wrong with the code? .. As for S.f, they have the same number of elements as shown in the code. Can the problem be solved?
Matt J
Matt J 2023년 3월 22일
As for S.f, they have the same number of elements as shown in the code.
It is puzzling that you think so. Running the code clearly shows that length(S)=4001 while length(f)=10000
% Define time range
t = -0.2:0.0001:0.2;
% Calculate signal s(t)
s = cos(2*pi*10*t).*cos(27*pi*200*t);
% Calculate Fourier transform of s(t)
Fs = 10000; % Sampling frequency
S = fftshift(fft(s))/length(s);
f = (-Fs/2:Fs/2-1)/length(s);
length(S)
ans = 4001
length(f)
ans = 10000

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Discrete Fourier and Cosine Transforms에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by