Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of fft, ifft and fftshift in matlab

I am trying to implement the split-step fourier method to solve the nonlinear schrodinger equation in optics. It basically treats the linear part and nonlinear part separately. It solves the linear part by using fourier transform and the nonlinear part in the time domain.

The following code is copied from a book:

alpha = 0
beta_2 = 1
gamma = 1

T = linspace(-5,5,2^13);
delta_T = T(2)-T(1);

L = max(size(A));
delta_omega = 1/L/delta_T*2*pi;
omega = (-L/2:1:L/2-1)*delta_omega;

A = 2*sech(T);
A_t = A;
step_num = 1000;
h = 0.5*pi/step_num;
results = zeros(L,step_num);

A_f = fftshift(fft(A_t));
for n=1:step_num
    A_f = A_f.*exp(-alpha*(h/2)-1i*beta_2/2*omega.^2*(h/2));
    A_t = ifft(A_f);
    A_t = A_t.*exp(1i*gamma*(abs(A_t).^2*h));
    A_f = fft(A_t);
    A_f = A_f.*exp(-alpha*(h/2)-1i*beta_2/2*omega.^2*(h/2));
    A_t = ifft(A_f);
    results(:,n) = abs(A_t);
end

where A_t is the pulse (the function to be solved). What I don't understand is that in the very beginning it uses fftshift to shift the zero frequency to the center, but then later in the loop it doesn't have fftshift. I tried adding fftshift to the main loop, or removing it in the very beginning. Both give wrong results, why is that? In general, when should I use fftshift and ifftshift, especially when I am trying to solve differential equation like in this case?

thanks

like image 831
Physicist Avatar asked Aug 20 '17 01:08

Physicist


1 Answers

You can partially clarify your doubt by plotting the signals as images and noticing the apparent difference, as i did when i tried the same.

Firstly, to use fftshift and ifftshift or not depends on what type of signal you are working on.

  1. fft function considers your signal to begin at 0 , unlike most cases we generally use in signal processing. Same with ifft.

  2. your actual negative side is considered inverted and shifted to the extreme right, essentially making your actual plot from example -5 to 5 to be 0 to 10.

  3. That's where we use fftshift to rearrange the data to get it back to centered at 0.

  4. If you want to shift the signal back to the unordered form to compute fft or ifft (which you essentially should), you should use ifftshift. Its not shifting ifft. Its opposite of fftshift.

    Ok, to make things simple follow this switch case-

Switch (Signal): {

Case(signal has both -ve and +ve parts, centered at zero):

  • apply ifftshift to make it unordered before fft or ifft
  • apply fftshift to the result to get back the output same as signal form.

Case( Signal is already unordered ):

  • directly apply fft or ifft.
  • apply fftshift to the result if you want to see it in natural type.

Case( Applying both fft and ifft simultaneously ):

  • go on, its alright. Worry about signal when you are performing single operations

}

like image 82
I.Newton Avatar answered Sep 20 '22 13:09

I.Newton