How to use FFTshift to produce a 0 frequency centered plot

조회 수: 41 (최근 30일)
Here is my plot, but I don't know why the right plot is shifted after using FFTshift function, how can I solve this problem?
fs = 250;
t = (0:1/fs:(0.1-.5/fs)); % [0, 0.1)
g = sin(2*pi*50*t)+0.3;
plot(t, g,'-o'),
title('g(t) = sin(2 * pi * 50 * t)+0.3' );
xlabel('time/s');
ylabel('g(t) ');
grid on
n = length(g);
L = 25;
Y = fft(g);
f = fs*(0:L-1)/L;
G = fftshift(Y);
fshift = (-n/2:n/2-1)*(fs/n); % zero-centered frequency range
powershift = abs(G).^2/n; % zero-centered power
figure
plot(fshift,powershift,'-o') % zero-centered plot which have shifts
grid on
figure
plot(f, abs(Y),'-o'), % correct frequency plot
xlabel 'Frequency (Hz)';
ylabel 'Magnitude';
title('Magnitude Plot');
grid on
  댓글 수: 2
Star Strider
Star Strider 2021년 9월 28일
What precisely is the problem?
The right plot looks to be the appropriate result after using fftshift.
What were you expecting?
.
Yian Chen
Yian Chen 2021년 9월 28일
편집: Yian Chen 2021년 9월 28일
Hi, thanks for your answer, if you observe the the right plot, you can see that the peaks are exactly not 50Hz,may be less than 50Hz, and not zero-centered. I just wanna figure out why and how to solve.

댓글을 달려면 로그인하십시오.

채택된 답변

Ashutosh Singh Baghel
Ashutosh Singh Baghel 2021년 10월 18일
Hi Yian,
I understand your curves and data values are true, yet the plot is shifted left by "-5". Please try to update the starting and final value of 'fshift' to,
fs = 250;
t = (0:1/fs:(0.1-.5/fs)); % [0, 0.1)
g = sin(2*pi*50*t)+0.3;
n = length(g);
L = 25;
Y = fft(g);
f = fs*(0:L-1)/L;
G1 = abs(Y);
G = fftshift(Y);
fshift = (-(n+1)/2+1:(n)/2)*(fs/n) % zero-centered frequency range
fshift = 1×25
-120 -110 -100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120
powershift = abs(G).^2/n; % zero-centered power
figure
plot(fshift(1:end),powershift(1:end),'-o');% zero-centered plot which have shifts
grid on
Please find the MATLAB documentation link for 'fftshift'.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by