2016-12-02 11 views
-1

numpyのfftとfft2を同じ1D配列に適用すると、異なる結果が生じる理由を説明できますか?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(2次元のフーリエ変換(FT))fft2以外imnputの引数を取ることです。

あなたの例では、print(f1)の場合、すべての値がおおよそ0であることがわかります。これは、洞結膜をフーリエ変換するときに疑わしいものになります。

何が起こったかというと、fftルーチンは配列の代わりに入力欄のリストを取得したので、各エントリ(1要素)に対してFTを実行しました。これは定数関数に相当し、そのために数学はFT(const1)= const1を教えてくれます。この4つの理由から、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') 

出力:あなた以下

はポイントを示して修正されたバージョン、であなたを見つけるFFT2について

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 

いくつかの例は、あなたが見つけることができるhere

+1

入力が1次元配列の場合、出力は同じでなければなりません。 – proton

+0

ok、良い点。違いは、fftとfft2が入力をどのように解釈するかです - >書き換えられた答えを見る –

関連する問題