简体   繁体   English

Python 2D中的傅立叶变换

[英]Fourier Transform in Python 2D

I want to perform numerically Fourier transform of Gaussian function using fft2 . 我想使用fft2Gaussian function进行数值Fourier transform Under this transformation the function is preserved up to a constant. 在这种转换下,功能被保留到一个常数。

I create 2 grids: one for real space , the second for frequency (momentum, k, etc.). 我创建了2个网格:一个用于real space ,第二个用于frequency (动量,k等)。 (Frequencies are shifted to zero). (频率变为零)。 I evaluate functions and eventually plot the results. 我评估函数并最终绘制结果。

Here is my code 这是我的代码

import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft2, ifft2
from mpl_toolkits.mplot3d import Axes3D

"""CREATING REAL AND MOMENTUM SPACES GRIDS"""
N_x, N_y = 2 ** 11, 2 ** 11
range_x, range_y = np.arange(N_x), np.arange(N_y)
dx, dy = 0.005, 0.005
# real space grid vectors
xv, yv = dx * (range_x - 0.5 * N_x), dy * (range_y - 0.5 * N_y)
dk_x, dk_y = np.pi / np.max(xv), np.pi / np.max(yv)
# momentum space grid vectors, shifted to center for zero frequency
k_xv, k_yv = dk_x * np.append(range_x[:N_x//2], -range_x[N_x//2:0:-1]), \
            dk_y * np.append(range_y[:N_y//2], -range_y[N_y//2:0:-1])

# create real and momentum spaces grids
x, y = np.meshgrid(xv, yv, sparse=False, indexing='ij')
kx, ky = np.meshgrid(k_xv, k_yv, sparse=False, indexing='ij')

"""FUNCTION"""
f = np.exp(-0.5 * (x ** 2 + y ** 2))
F = fft2(f)
f2 = ifft2(F)
"""PLOTTING"""
fig = plt.figure()
ax = Axes3D(fig)
surf = ax.plot_surface(x, y, np.abs(f), cmap='viridis')
# for other plots I changed to
# surf = ax.plot_surface(kx, ky, np.abs(F), cmap='viridis')
# surf = ax.plot_surface(x, y, np.abs(f2), cmap='viridis')
plt.show()

So, the plots for gaussian, fourier(gaussian), inverse_fourier(fourier(gaussian)) are the following: Initial , Fourier , Inverse Fourier 因此, gaussian, fourier(gaussian), inverse_fourier(fourier(gaussian))的图如下: 初始傅里叶逆傅里叶

Using plt.imshow() , I additionally plot fourier of gaussian: 使用plt.imshow() ,我还绘制了高斯的傅立叶图:

   plt.imshow(F)
   plt.colorbar()
   plt.show()

The result is as follows: imshow 结果如下: imshow

That doesn't make sense. 那没有道理。 I expect see the same gaussian function as the initial up to some constant order of unity. 我希望看到与初始相同的gaussian function ,直到某个恒定的统一阶。

I would be very glad if someone could clarify this for me. 如果有人能为我澄清这一点,我将感到非常高兴。

I think you are a bit puzzled by the shape of your output F . 我认为您对输出F的形状有些困惑。 Especially, you might wonder why you see such a sharp peak and not a wide-spread gaussian. 特别是,您可能想知道为什么看到如此尖锐的峰而不是宽广的高斯峰。

I changed your code a little bit: 我对您的代码做了一些更改:

 import numpy as np
 import matplotlib.pyplot as plt
 from scipy.fftpack import fft2, ifft2
 from mpl_toolkits.mplot3d import Axes3D

 """CREATING REAL AND MOMENTUM SPACES GRIDS"""
 N_x, N_y = 2 ** 10, 2 ** 10
 range_x, range_y = np.arange(N_x), np.arange(N_y)
 dx, dy = 0.005, 0.005
 # real space grid vectors
 xv, yv = dx * (range_x - 0.5 * N_x), dy * (range_y - 0.5 * N_y)
 dk_x, dk_y = np.pi / np.max(xv), np.pi / np.max(yv)
 # momentum space grid vectors, shifted to center for zero frequency
 k_xv, k_yv = dk_x * np.append(range_x[:N_x//2], -range_x[N_x//2:0:-1]), \
             dk_y * np.append(range_y[:N_y//2], -range_y[N_y//2:0:-1])

 # create real and momentum spaces grids
 x, y = np.meshgrid(xv, yv, sparse=False, indexing='ij')
 kx, ky = np.meshgrid(k_xv, k_yv, sparse=False, indexing='ij')

 """FUNCTION"""
 sigma=0.05
 f = 1/(2*np.pi*sigma**2) * np.exp(-0.5 * (x ** 2 + y ** 2)/sigma**2)
 F = fft2(f)
 """PLOTTING"""
 fig = plt.figure()
 ax = Axes3D(fig)
 surf = ax.plot_surface(x, y, np.abs(f), cmap='viridis')
 # for other plots I changed to
 fig2 = plt.figure()
 ax2 =Axes3D(fig2)
 surf = ax2.plot_surface(kx, ky, np.abs(F)*dx*dy, cmap='viridis')
 plt.show()

Notice that I introduced a sigma parameter to control the width of the gaussian. 注意,我引入了一个sigma参数来控制高斯的宽度。 I now invite you to play with the following parameters: N_x and N_y , d_x and d_y and sigma . 我现在请你用下面的参数发挥: N_xN_yd_xd_ysigma

You should then see the inverse behaviour of gaussian in real-space and in fourier space: The larger the gaussian in real-space, the narrower in fourier-space and vice-versa. 然后,您应该看到高斯在实空间和傅立叶空间中的逆行为:高斯在实空间中较大,在傅立叶空间中较窄,反之亦然。

So with the currently set parameters in my code, you get the following plots: 因此,使用我的代码中当前设置的参数,您将得到以下图表:

Real space: 实际空间: 在此处输入图片说明

Fourier Space: 傅立叶空间: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM