Issue using fft() function

3 ビュー (過去 30 日間)
Ganesh Jada
Ganesh Jada 2023 年 6 月 19 日
コメント済み: Ganesh Jada 2023 年 6 月 19 日
I sampled the waveform x (t) = 10*cos(2*pi*1000t) + 6*cos(2*pi*2000t) + 2*cos(2*pi*4000t) with a sampling rate of 12000 Hz. And I want to plot the DFT of x (t) with N=64 points using fft() function. But the fft graph is not as expected. It is shifting. How can I solve this? How can I make it to plot correctly without any shifting?
I have attached my code below.
f = 1000;
fs = 12*f;
T = 0.01;
t = 0:1/fs:T;
x = 10*cos(2*pi*f*t) + 6*cos(2*pi*2*f*t) + 2*cos(2*pi*4*f*t);
plot(t,x);
title("x(t)");
xlabel("Time (in s)");
ylabel("Amplituide");
N = 64;
y = fft(x,N)/N;
freq = (-N/2:N/2 - 1)*fs/N;
stem(freq,abs(y));
xlabel("Normalized Frequency (in Hz)");
ylabel("|X(f)|/N");

採用された回答

RANGA BHARATH
RANGA BHARATH 2023 年 6 月 19 日
編集済み: RANGA BHARATH 2023 年 6 月 19 日
Hi @Ganesh Jada. Here is the solution and code for your question.
Question: How to shift the DFT to the center of the spectrum?
Solution:
The fft() function automatically shifts the spectrum away from the center. So, there is a function called fftshift() which rearranges the outputs of fft(), fft2() and fftn() by moving the zero-frequency component to the center of the array. It is useful for visualizing a Fourier transform with the zero-frequency component in the middle of the spectrum.
So, you can use the below code which uses fftshift() function and plots that using stem() function.
Code:
f = 1000;
fs = 12*f;
T = 0.01;
t = 0:1/fs:T;
x = 10*cos(2*pi*f*t) + 6*cos(2*pi*2*f*t) + 2*cos(2*pi*4*f*t);
plot(t,x);
title("x(t)");
xlabel("Time (in s)");
ylabel("Amplituide");
N = 64;
y = fft(x,N)/N;
freq = (-N/2:N/2 - 1)*fs/N;
%% Here is the modification
y_shift = fftshift(y);
stem(freq,abs(y_shift));
xlabel("Normalized Frequency (in Hz)");
ylabel("|X(f)|/N");
Links to Documentation:
  1 件のコメント
Ganesh Jada
Ganesh Jada 2023 年 6 月 19 日
Thank you for your response I am grateful to you

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFourier Analysis and Filtering についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by