필터 지우기
필터 지우기

Issue using fft() function

조회 수: 1 (최근 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 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