2014-02-15 150 views
0

在下面的程序中,我想計算由U給出的給定場的快速傅立葉變換。對於fft和fft2,返回值之間的差異是什麼?任何幫助,將不勝感激!謝謝。應用於矩陣的fft/fft2的返回值之間的區別是什麼?

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! 

這裏是我所得到的:

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]] 

謝謝!

+1

有你看着這個值從每個返回? 'numpy'文檔? – jonrsharpe

+0

@jonrsharpe是的。我查看了文檔,但我想知道這兩個函數的返回結果有什麼不同。 –

+0

@jonrsharpe請問我剛剛添加了一些關於這個問題的細節!謝謝 –

回答

1

np.fft模塊的文檔字符串。

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. 

如果您不想將差異可視化,則繪製兩個基質可以給出這個結果。我對fft的知之甚少,甚至不知道是否有意義以這種方式來設計它們。

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") 
+0

謝謝你!事實上,現在我不能現在使用哪一個來計算我的場U的DFT。我甚至可以理解爲什麼有很大的值是fft2的域邊界!無論如何,謝謝你的時間和精力! –

+0

@Strömungsmechanik也添加了兩個矩陣的直方圖,如果有幫助的話。 – M4rtini

+0

超級!那很棒!謝謝 :) –

相關問題