I got error with "error using plot, vectors must be the same length"

조회 수: 4 (최근 30일)
Tran Hai Khanh
Tran Hai Khanh 2023년 10월 10일
답변: Paul 2023년 10월 11일
  댓글 수: 1
dpb
dpb 2023년 10월 11일
Did you even look to see what you have done???
T=10;
Ts=0.01;
t=-T:Ts:T;
x=square(2*pi*t);
X=fft(x);
f=(-1/(2*Ts):1/Ts:1/(2*Ts))
f = 1×2
-50 50
m=abs(fftshift(X));
numel(f), numel(m)
ans = 2
ans = 2001

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

답변 (1개)

Paul
Paul 2023년 10월 11일
Hi Tran,
Next time, please copy/paste your code into the Question, which makes it much easier for people to help.
Original code
T = 10;
Ts = 0.01;
t = -T:Ts:T;
x1 = square(2*pi*t);
X1 = fft(x1);
frequencies = fftshift(-1/(2*Ts):1/Ts:1/(2*Ts));
magnitude_spectrum = abs(fftshift(X1));
Number of elements in the vectors
numel(frequencies)
ans = 2
numel(magnitude_spectrum)
ans = 2001
So the error arises becasue 2001 elements can't be plotted against 2 elements.
The correct expression for frequencies in this case would be, assuming units of rad/sec which looks like what might be wanted
frequencies = (-1000:1000)/2001/Ts;
Also, because x1 is defined from -T to T, it's likely that ifftshift should be applied to x1 before taking its fft.

카테고리

Help CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by