How can I plot fft ?

1 回表示 (過去 30 日間)
Negar Sharafdoust
Negar Sharafdoust 2017 年 6 月 11 日
コメント済み: Walter Roberson 2017 年 6 月 13 日
Hi,
I'm new student for writing in Matlab. so, I don't know how I can plot Y which is fourier transform of X(n). there is an error which says:"Vectors must be the same length".
x(n)=sin(100t)+sin(2000t)+sin(6000t);
my script is :
fs=6000;
% I consider 2 seconds for input signal
N=2;
t=(0:1/fs:N-1/fs);
w=(-N*fs/2:1/fs:(N-1)*fs/2);
X=sin(100*t)+sin(2000*t)+sin(6000*t);
X_fft=fft(X,fs);
X_fft=fftshift(X_fft);
Y=abs(X_fft);
figure;
subplot(3,1,1);
plot(t,X);
title('input signal X(t)before filtering')
%plot the first 100 samples
subplot(3,1,2);
plot(t(1:100),X(1:100));
title('Input signal with the first 100 samples')
%plot the DFT
subplot(3,1,3);
plot(w,Y);
title ('DFT for input signal befor filtering')

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 6 月 11 日
You have
X_fft=fft(X,fs);
that tells fft to produce an output of length fs, 6000 . But your original data is length fs*N so you have problems when you try to plot(t,X) since t is length 12000 but X is length 6000
  2 件のコメント
Negar Sharafdoust
Negar Sharafdoust 2017 年 6 月 13 日
I tried to solve the problem in the way you said and put N*fs instead of fs in fft(X,fs). but I got error. also I changed the w to get the result but it gives error in line plot (w,abs(X_fft)). can you tell me exactly which line I should correct?and how to write the command. thanks a lot
Walter Roberson
Walter Roberson 2017 年 6 月 13 日
Your w is just completely the wrong size. Also, remember, it should be symmetrical, and remember that two adjacent frequencies are in each bin.
w = -N : 2/fs : N;
w(end) = [];
The second line there gets rid of an extra point as you would otherwise have an odd number (the same number for negative as positive, plus the entry for 0 exactly).

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

カテゴリ

Help Center および File ExchangeParametric Spectral Estimation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by