0

I am trying to perform a twodimensional FFT in python. However, I am having trouble both with the values alternating between negative and positive sign as well as with the shape of the numerical gaussian in comparison to the analytically expected gaussian. I have also tried using fft.shift on both axes of the fft2 result matrix but this cut the gaussian in half and moved it to the sides of the coordinate system. I would really appreciate some help, thank you in advance!

import numpy as np
import scipy
import scipy.fft as fft
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
#generate data array

N=200
lim=50
px=np.linspace(-lim,lim,N)
py=px
pxpx, pypy = np.meshgrid(px,py)
pxpx_2, pypy_2 = np.meshgrid(px**2,py**2)


fnc=1/2*np.exp(-(pypy**2+pxpx**2)*np.pi/2)
ft=np.fft.fftshift(np.fft.fft2(fnc))
x  = np.fft.fftshift((np.fft.fftfreq(ft.shape[0],d=2*lim/(N-1)))) # this takes an argument for the timestep
y  =np.fft.fftshift(np.fft.fftfreq(ft.shape[1],d=2*lim/(N-1)))

xx, yy=np.meshgrid(x,y)
test=np.exp(-(yy**2+xx**2)*2*np.pi) #normalised, symmetrical FT (scaling by sqrt 2pi) to compare fft result with
fig = plt.figure()
ax2 = fig.add_subplot(projection='3d')

ax2.plot_wireframe(xx, yy, ft, rstride=25, cstride=25)
ax2.plot_wireframe(xx, yy, (test), rstride=11, cstride=11,color='red')

plt.show()

plot

caprisun
  • 1
  • 1
  • Does this answer your question? [Strange phase for gaussian beam 2D](https://stackoverflow.com/questions/55556913/strange-phase-for-gaussian-beam-2d) – Cris Luengo Jun 03 '23 at 14:01
  • I don't understand how this helps me with the alternating signs and the shape of the gaussian. Could you be more explicit? – caprisun Jun 03 '23 at 14:18
  • It’s exactly the same problem and exactly the same solution. Alternating signs means every other sample has the phase flipped by 180 degrees, like in the linked question. Apply `ifftshift` to your input before `fft`, and you should get the right result. Do make sure that `px` has a 0 in the middle (at `len(px)//2`). This is because the FFT requires the zero at the top-left sample, the `ifftshift` moves it there from the middle. If it’s not there, you have a shifted Gaussian. Shift affects the phase. – Cris Luengo Jun 03 '23 at 15:18
  • Ah I see, thanks a lot! The shape of the fft gaussian is still off, though. It's either a lot larger or a lot smaller than the analytical one. I tried solving this trying the different norm options in fft2, but it has not worked out so far – caprisun Jun 04 '23 at 09:34
  • Is it taller or wider? – Cris Luengo Jun 04 '23 at 13:55
  • It's stretched/compressed in z-direction in comparison to the analytical one – caprisun Jun 04 '23 at 14:01
  • That’s just a normalization issue. There are different definitions for the normalization in the Fourier transform. Also, if your `px` does not have unit distances between samples, you’ll have a different weight than you expect, because the DFT assumes unit spacing for samples. A normalized Gaussian is not one that integrates to 1 before sampling, but one whose samples sum to 1. – Cris Luengo Jun 04 '23 at 14:09
  • Yes, that's why I tried the different options for "norm" in fft2. I also tried scaling the analytical one by factors of $\sqrt(2\pi$, but none of the combinations work so far. I tried setting the px spacing to 1, but it's the same issue still and in addition, the gaussian is shiftet by a constant factor in z-direction. – caprisun Jun 04 '23 at 14:19

0 Answers0