Issue using fft() function

3 次查看(过去 30 天)
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:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by