Effacer les filtres
Effacer les filtres

Issue using fft() function

1 vue (au cours des 30 derniers jours)
Ganesh Jada
Ganesh Jada le 19 Juin 2023
Commenté : Ganesh Jada le 19 Juin 2023
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");

Réponse acceptée

RANGA BHARATH
RANGA BHARATH le 19 Juin 2023
Modifié(e) : RANGA BHARATH le 19 Juin 2023
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 commentaire
Ganesh Jada
Ganesh Jada le 19 Juin 2023
Thank you for your response I am grateful to you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Fourier Analysis and Filtering dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by