- Here is the link to the documentation of fftshift() function: https://www.mathworks.com/help/matlab/ref/fftshift.html#d124e458005
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");
0 个评论
采纳的回答
RANGA BHARATH
2023-6-19
编辑:RANGA BHARATH
2023-6-19
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 Center 和 File Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!