when do I need use fftshift?
Mostrar comentarios más antiguos
Bx = 10;
A = sqrt(log(2))/(2*pi*Bx);
fs = 500; %sampling frequency
dt = 1/fs; %time step
T=1; %total time window
t = -T/2:dt:T/2-dt; %time grids
df = 1/T; %freq step
Fmax = 1/2/dt; %freq window
f=-Fmax:df:Fmax-df; %freq grids, not used in our examples, could be used by plot(f, X)
%-------------------------------------------------------------------
% Numerical evaluations
x = exp(-t.^2/2/A^2);
Xan = A*sqrt(2*pi)*exp(-2*pi^2*f.^2*A^2); %X(f), analytical Fourier transform of x(t), real
Xfft = dt * fft(x); %directly using fft()
Xfftshift = dt * fft(fftshift(x)); %using fftshift() before fft()
Xfinal = dt * fftshift(fft(fftshift(x)));
figure;
plot(f, imag(Xfinal), 'LineWidth', 3, 'DisplayName', 'Imaginary (Numerical)');
hold on;
plot(f, real(Xfinal), 'LineWidth', 3, 'DisplayName', 'Real (Numerical)');
plot(f, Xan, '--', 'LineWidth', 2, 'DisplayName', 'Analytical');
legend('show');
xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Fourier Transform of Gaussian Pulse');
grid on;
%shift property of Fourier transform, gaussian function move right T/2
%should be a complex number. after test, the x should not use fftshift can
%get the right result.
%different from whyusefftshift.m is t and x
clear
Bx = 10;
A = sqrt(log(2))/(2*pi*Bx);
fs = 500; %sampling frequency
dt = 1/fs; %time step
T = 1; %total time window
t = 0:dt:T-dt; %time grids starting from t=0
df = 1/T; %freq step
Fmax = 1/2/dt; %freq window
f = -Fmax:df:Fmax-df; %freq grids, not used in our examples, could be used by plot(f, X)
% Numerical evaluations
x = exp(-(t - T/2).^2 / (2*A^2)); % Adjusted the time grid
Xan_real = A * sqrt(2*pi) * exp(-2*pi^2*f.^2*A^2).*cos(pi*T*f); %X(f), analytical Fourier transform of x(t), real
Xfft = dt * fft(x); %directly using fft()
%Xfinal = dt * fft(fftshift(x)); %using fftshift() before fft()
%Xfinal = dt * fftshift(fft(fftshift(x)));
%Xfinal = dt * fftshift(fft(fftshift(x)));
Xfinal = dt * fftshift(fft(x));
figure;
plot(f, imag(Xfinal), 'LineWidth', 10, 'DisplayName', 'Imaginary (Numerical)');
legend('show');
figure;
plot(f, imag(Xfinal), 'LineWidth', 10, 'DisplayName', 'Imaginary (Numerical)');
hold on;
plot(f, real(Xfinal), 'LineWidth', 3, 'DisplayName', 'Real (Numerical)');
plot(f, Xan_real, '--', 'LineWidth', 2, 'DisplayName', 'Analytical-real');
legend('show');
xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Fourier Transform of Gaussian Pulse');
grid on;
In the above two code snippet, I can figure out when I need use the fftshift.
In the first code, I must using fftshift before using fft function to get the right result. But In the second code,
I must not using fftshift before x and must using fft directly to get the right result.
Your help would be highly appreciated!
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Multirate Signal Processing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!















