Skip to content Skip to sidebar Skip to footer

Fourier Transform In Python 2d

I want to perform numerically Fourier transform of Gaussian function using fft2. Under this transformation the function is preserved up to a constant. I create 2 grids: one for rea

Solution 1:

I think you are a bit puzzled by the shape of your output 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. I now invite you to play with the following parameters: N_x and N_y, d_x and d_y and sigma.

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: enter image description here

Fourier Space: enter image description here

Post a Comment for "Fourier Transform In Python 2d"