2016-12-02 46 views
-1

有人可以解释为什么将numpy的fft和fft2应用于相同的一维数组会产生不同的结果吗?为什么numpy的fft和fft2不等于1D数组?

x = np.linspace(0, 2*np.pi, 10) 
x = np.reshape(x, (10, 1)) 
x = np.sin(x) 
f1 = np.fft.fft(x) 
f2 = np.fft.fft2(x) 
np.equal(f1,f2) 

理论上,f1应该等于f2。

回答

1

回答重写(对不起,第一个是有点短):

不同的是,该fft采取其它imnput参数比fft2(傅里叶变换(FT)在2名维)。

如果你在你的例子中的print(f1)你可以很好地看到,所有的值大致为0.这应该让你怀疑,因为你傅立叶变换窦。

发生了什么是,fft例程得到了一个输入aruments而不是一个数组的列表,所以它为每个条目(1个元素)做了FT。这对应于一个常数函数,对此,数学告诉我们:FT(const1)= const1。四个这个原因你得到了像fft输入一样的输出。您正确使用的fft2例程。

下面你找到你例如在修改后的版本,这说明了一点:

import numpy as np 
import copy 
x1 = np.linspace(0, 2*np.pi, 10) 
x2 = np.reshape(x1, (10, 1)) 
x1 = np.sin(x1) 
x2 = np.sin(x2) 
f1 = np.fft.fft(x1) 
f2 = np.fft.fft2(x2) 

print('input arrays for fft and fft2:') 
print(x1) 
print(x2) 
print('your old output of fft, which is exactly equal to the input x2') 
print(np.fft.fft(x2)) 
print('Now we compare our results:') 
for ii in range(0,len(x1)): 
    print('f1:',f1[ii],'\tf2:',f2[ii,0]) 
print('and see, they agree') 

输出:

input arrays for fft and fft2: 

[ 0.00000000e+00 6.42787610e-01 9.84807753e-01 8.66025404e-01 

    3.42020143e-01 -3.42020143e-01 -8.66025404e-01 -9.84807753e-01 

    -6.42787610e-01 -2.44929360e-16] 

[[ 0.00000000e+00] 

[ 6.42787610e-01] 

[ 9.84807753e-01] 

[ 8.66025404e-01] 

[ 3.42020143e-01] 

[ -3.42020143e-01] 

[ -8.66025404e-01] 

[ -9.84807753e-01] 

[ -6.42787610e-01] 

[ -2.44929360e-16]] 

your old output of fft, which is exactly equal to the input x2 

[[ 0.00000000e+00+0.j] 

[ 6.42787610e-01+0.j] 

[ 9.84807753e-01+0.j] 

[ 8.66025404e-01+0.j] 

[ 3.42020143e-01+0.j] 

[ -3.42020143e-01+0.j] 

[ -8.66025404e-01+0.j] 

[ -9.84807753e-01+0.j] 

[ -6.42787610e-01+0.j] 

[ -2.44929360e-16+0.j]] 

Now we compare our results: 

f1: (-1.11022302463e-16+0j)  f2: (-1.11022302463e-16+0j) 

f1: (1.42837120544-4.39607454395j) f2: (1.42837120544-4.39607454395j) 

f1: (-0.485917547994+0.668808127899j) f2: (-0.485917547994+0.668808127899j) 

f1: (-0.391335729991+0.284322050566j) f2: (-0.391335729991+0.284322050566j) 

f1: (-0.36913281032+0.119938520599j) f2: (-0.36913281032+0.119938520599j) 

f1: (-0.363970234266+1.55431223448e-15j) f2: (-0.363970234266+1.55431223448e-15j) 

f1: (-0.36913281032-0.119938520599j) f2: (-0.36913281032-0.119938520599j) 

f1: (-0.391335729991-0.284322050566j) f2: (-0.391335729991-0.284322050566j) 

f1: (-0.485917547994-0.668808127899j) f2: (-0.485917547994-0.668808127899j) 

f1: (1.42837120544+4.39607454395j) f2: (1.42837120544+4.39607454395j) 

and see, they agree 

约FFT2一些例子,你可以找到here

+1

但是如果输入是一维数组,则输出应该是相同的。 – proton

+0

好的,好点。不同之处在于fft和fft2如何解释输入 - >请参阅重写的答案 –