[英]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). 其中
A_t
是脉冲(要求解的函数)。 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
. 我不明白的是,在一开始就使用
fftshift
零频率转移到中心,但随后在循环后,它并没有fftshift
。 I tried adding fftshift
to the main loop, or removing it in the very beginning. 我尝试将
fftshift
添加到主循环中,或者在开始时将其删除。 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? 一般来说,我什么时候应该使用
fftshift
和ifftshift
,特别是当我试图像这种情况那样解决微分方程时?
thanks 谢谢
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. 首先,使用fftshift和ifftshift取决于你正在做什么类型的信号。
fft function considers your signal to begin at 0 , unlike most cases we generally use in signal processing. fft函数认为您的信号从0开始,与我们通常用于信号处理的大多数情况不同。 Same with ifft.
与ifft相同。
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. 你的实际负面被认为是倒置并转移到最右边,基本上使你从实例-5到5的实际情节为0到10。
That's where we use fftshift to rearrange the data to get it back to centered at 0. 这就是我们使用fftshift重新排列数据以使其回到0中心的位置。
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. 如果你想将信号转换回无序形式来计算fft或ifft(你基本上应该这样),你应该使用ifftshift。 Its not shifting ifft.
它没有转移ifft。 Its opposite of fftshift.
它与fftshift相反。
Ok, to make things simple follow this switch case - 好的,为了简化这个开关案例 -
Switch (Signal): { 开关 (信号):{
Case (signal has both -ve and +ve parts, centered at zero): 情况 (信号有-ve和+ ve部分,以零为中心):
Case ( Signal is already unordered ): 案例 (信号已经无序):
Case ( Applying both fft and ifft simultaneously ): 案例 (同时应用fft和ifft):
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.