user3261475
user3261475

Reputation:

What is the difference between the return of fft / fft2 applied to a matrix?

In the following program, I would like to compute the fast Fourier transform of a given field given by U. what is the difference between the returned values for fft and for fft2? Any help would be appreciated! Thank you.

import numpy as np
from numpy import sin, cos, pi

nx=3
ny=3

px=2*pi
py=2*pi

qx=1.0*px/(nx-1)
qy=1.0*py/(ny-1)

x = np.linspace(0,px,nx)
y = np.linspace(0,py,ny)

X,Y = np.meshgrid(x,y)

U=cos(X)*sin(Y)

#compite fft's
Uh1=np.fft.fft(U)

Uh2=np.fft.fft2(U)

print('For fft')
print(Uh1)

print('For fft2')
print(Uh2)

#What is the difference between Uh1 and Uh2? Thank you!

Here is what I get:

For fft
[[  0.00000000e+00 +0.00000000e+00j   0.00000000e+00 +0.00000000e+00j
0.00000000e+00 +0.00000000e+00j]
[  1.22464680e-16 +0.00000000e+00j   1.22464680e-16 +2.12115048e-16j
1.22464680e-16 -2.12115048e-16j]
[ -2.44929360e-16 +0.00000000e+00j  -2.44929360e-16 -4.24230095e-16j
-2.44929360e-16 +4.24230095e-16j]]
For fft2
[[ -1.22464680e-16 +0.00000000e+00j  -1.22464680e-16 -2.12115048e-16j
-1.22464680e-16 +2.12115048e-16j]
[  6.12323400e-17 -3.18172572e-16j   6.12323400e-16 -2.12115048e-16j
-4.89858720e-16 -4.24230095e-16j]
[  6.12323400e-17 +3.18172572e-16j  -4.89858720e-16 +4.24230095e-16j
6.12323400e-16 +2.12115048e-16j]]

Thank you!

Upvotes: 1

Views: 2217

Answers (1)

M4rtini
M4rtini

Reputation: 13539

docstring of the np.fft module.

Standard FFTs
-------------

.. autosummary::
   :toctree: generated/

   fft       Discrete Fourier transform.
   ifft      Inverse discrete Fourier transform.
   fft2      Discrete Fourier transform in two dimensions.
   ifft2     Inverse discrete Fourier transform in two dimensions.
   fftn      Discrete Fourier transform in N-dimensions.
   ifftn     Inverse discrete Fourier transform in N dimensions.

Plotting the two matricies gives this if you wan't to visualize the differences. I do not know enough about fft's to even know if it makes any sense to plot them this way.

enter image description here

enter image description here

plt.figure()

plt.subplot(2,2,1)
plt.plot(Uh1.real.ravel())
plt.title("1 - real")
plt.subplot(2,2,2)
plt.plot(Uh2.real.ravel())
plt.title("2 - real")

plt.subplot(2,2,3)
plt.plot(Uh1.imag.ravel())
plt.title("1 - imaginary")
plt.subplot(2,2,4)
plt.plot(Uh2.imag.ravel())
plt.title("2 - imaginary")

plt.figure()

plt.subplot(2,2,1)
plt.hist(Uh1.real.ravel())
plt.title("1 - real")
plt.subplot(2,2,2)
plt.hist(Uh2.real.ravel())
plt.title("2 - real")

plt.subplot(2,2,3)
plt.hist(Uh1.imag.ravel())
plt.title("1 - imaginary")
plt.subplot(2,2,4)
plt.hist(Uh2.imag.ravel())
plt.title("2 - imaginary")

Upvotes: 2

Related Questions