Why do we use ifft instead of fft when looking at the spectrum of a sech pulse (uu) in this code?
조회 수: 3 (최근 30일)
이전 댓글 표시
In the code below, temp = fftshift(ifft(uu)) is used to view the sech pulse in the spectrum. However, since ifft is a function that transforms from the frequency domain to the time domain, wouldn't temp = fftshift(fft(uu))) be correct? Why does the book do it this way?
% SSFM code for solving the normalized NLS equation
%% By G. P. Agrawal for the 6th edition of NLFO book
fiblen = 5;
beta2 =-1;
N = 1;
% fiber length (in units of L_D)
% sign of GVD parameter beta_2
% soliton order
%---set simulation parameters
nt = 1024; Tmax = 32;
% FFT points and window size
step_num = round(20*fiblen*N^2);
% No. of z steps
deltaz = fiblen/step_num; % step size in z
dtau = (2*Tmax)/nt;
tau = (-nt/2:nt/2-1)*dtau;
% step size in tau
% time array
omega = fftshift(-nt/2:nt/2-1)*(pi/Tmax); % omega array
uu = sech(tau);
% sech pulse shape (can be modified)
%---Plot input pulse shape and spectrum
temp = fftshift(ifft(uu));
% Fourier transform
spect = abs(temp).^2;
spect = spect./max(spect);
freq = fftshift(omega)/(2*pi);
subplot(2,1,1);
% input spectrum
% normalize
% freq. array
plot(tau, abs(uu).^2, '--k'); hold on; axis([-5 5 0 inf]);
xlabel('Normalized Time'); ylabel('Normalized Power');
subplot(2,1,2);
plot(freq, spect, '--k'); hold on; axis([-.5 .5 0 inf]);
xlabel('Normalized Frequency'); ylabel('Spectral Power');
댓글 수: 2
Umar
2024년 6월 21일
Hi Shieon,
In the given code snippet, temp = fftshift(ifft(uu)) is correctly used to analyze the sech pulse in the spectrum. The ifft function performs an Inverse Fast Fourier Transform (IFFT) to convert the signal from the frequency domain to the time domain. By applying fftshift after ifft, the spectrum is centered properly for visualization.
On the other hand, temp = fft(shift(fft(uu))) would not be appropriate as it unnecessarily shifts the spectrum twice, leading to incorrect results. The book's approach aligns with standard practices in signal processing, ensuring accurate spectral analysis of the sech pulse.
채택된 답변
Paul
2024년 6월 21일
편집: Paul
2024년 6월 21일
Hi Siheon,
Your interpretation of fft as going from time to frequency, and ifft as going from frequency to time, with fft and ifft as defined in Matlab, is a common convention. But it's just a convention. There may be some technical communities that use the opposite conventions, which is fine as long as one stays consistent. Perhaps the author purposefully uses a different convention than you expect.
Having said that, I think that there are other concerns with the code.
fiblen = 5;
beta2 =-1;
N = 1;
% fiber length (in units of L_D)
% sign of GVD parameter beta_2
% soliton order
%---set simulation parameters
nt = 1024; Tmax = 32;
% FFT points and window size
step_num = round(20*fiblen*N^2);
% No. of z steps
deltaz = fiblen/step_num; % step size in z
dtau = (2*Tmax)/nt;
tau = (-nt/2:nt/2-1)*dtau;
% step size in tau
% time array
Here is the first problem. I have no idea why fftshift is being used here on the frequency vector. That makes no sense. And then later the code appplies fftshift to omega again. Because nt is even, fftshift is its own inverse operator, but that wouldn't be the case if nt were odd.
% omega = fftshift(-nt/2:nt/2-1)*(pi/Tmax); % omega array
omega = (-nt/2:nt/2-1)*(pi/Tmax); % omega array
uu = sech(tau);
% sech pulse shape (can be modified)
%---Plot input pulse shape and spectrum
Here is the crux of your question. Here is the original line of code
temp = fftshift(ifft(uu));
Note that temp has negative elements, which shouldn't be the case for the Fourier transform of sech. But that gets covered up later because the code only uses abs(temp), so temp is ok in that context.
figure
plot(omega,real(temp))
The correct expression for temp would be
temp = fftshift(ifft(ifftshift(uu)));
Which results in temp having the correct shape
figure
plot(omega,real(temp))
The approach that you're thinking of is
temp = fftshift(fft(ifftshift(uu)));
figure
plot(omega,real(temp))
The result is the same as that from ifft to within a constant scale factor, which reflects the different scaling between fft and ifft.
% Fourier transform
spect = abs(temp).^2;
spect = spect./max(spect);
This line is incorrect, IMO, even w/o the fftshift. The scaling is incorrect, at least based on what I think this code is trying to demonstrate. I think it should be
%freq = fftshift(omega)/(2*pi)
freq = omega*pi/2;
I'm going to skip these plots
%{
subplot(2,1,1);
% input spectrum
% normalize
% freq. array
plot(tau, abs(uu).^2, '--k'); hold on; axis([-5 5 0 inf]);
xlabel('Normalized Time'); ylabel('Normalized Power');
subplot(2,1,2);
plot(freq, spect, '--k'); hold on; axis([-.5 .5 0 inf]);
xlabel('Normalized Frequency'); ylabel('Spectral Power');
%}
And instead plot
figure
hold on
plot(tau,uu)
plot(freq,real(temp)*dtau/pi,'x')
xlim([-5 5])
This plot shows the expected result, i.e., the Continuous Time Fourier Transform (CTFT) of sech(t) is pi*sech(pi/2*w). The extra scaling of dtau is the proper scaling to convert the Discrete Fourier Transform (DFT) samples as computed by fft (not ifft, which would be different scaling) to approximate samples of the CTFT.
댓글 수: 0
추가 답변 (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!