use of fft, ifft and fftshift in matlab
Asked Answered
T

1

7

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

Tidbit answered 20/8, 2017 at 1:48 Comment(2)
We’ll need to see what omega is—usually you need fftshift et al. when you assume your frequencies are zero-centered, because fft and ifft assume inputs/outputs to be zero-beginning. Can you post a short but complete example?Gastrin
edited the codeTidbit
R
2

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

}

Ransome answered 20/8, 2017 at 11:33 Comment(2)
Thanks, but in my example why is fftshift used in the very beginning but no inside the main loop?Tidbit
This would be super helpful with some simple code examples (the code in the OP, with the for loop, is just not very clear). For instance, case 3 what does that mean to just "go on it is alright" and what does it mean to apply both fft and ifft simultaneously? It is infuriating how poorly implemented and documented fft() is in Matlab. The internet is filled with confusion about how to work with it properly.Etoile

© 2022 - 2024 — McMap. All rights reserved.