Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
680 views
in Technique[技术] by (71.8m points)

python - FFT in Matlab and numpy / scipy give different results

I am trying to re-implement one of the matlab toolboxes. they use fft over there. when i perform same operation on the same data i get different results to those from matlab. Just take a look:

MATLAB:

Msig =

 0     0     0     0
 0     0     0     0
 0     0     0     0
 0     0     0     0
 0     1     0     0
 0     0     0     0

fft(Msig.')

Columns 1 through 4

    0                  0                  0                  0          
    0                  0                  0                  0          
    0                  0                  0                  0          
    0                  0                  0                  0          

Columns 5 through 6

 1.0000                  0          
      0 - 1.0000i        0          
-1.0000                  0          
      0 + 1.0000i        0    

PYTHON:

Msig=
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]]) 

np.fft.fft(Msig.transpose())
array([[ 0.0 +0.00000000e+00j,  0.0 +0.00000000e+00j,
         0.0 +0.00000000e+00j,  0.0 +0.00000000e+00j,
         0.0 +0.00000000e+00j,  0.0 +0.00000000e+00j],
       [ 1.0 +0.00000000e+00j, -0.5 +8.66025404e-01j,
        -0.5 -8.66025404e-01j,  1.0 -3.88578059e-16j,
        -0.5 +8.66025404e-01j, -0.5 -8.66025404e-01j],
       [ 0.0 +0.00000000e+00j,  0.0 +0.00000000e+00j,
         0.0 +0.00000000e+00j,  0.0 +0.00000000e+00j,
         0.0 +0.00000000e+00j,  0.0 +0.00000000e+00j],
       [ 0.0 +0.00000000e+00j,  0.0 +0.00000000e+00j,
        0.0 +0.00000000e+00j,  0.0 +0.00000000e+00j,
         0.0 +0.00000000e+00j,  0.0 +0.00000000e+00j]])

The best i can get if i mess with parameters(axis etc.) of np.fft.fft()/np.fft.fft2()/np.fft.fftn() is same values but shifted. unfortunately manual shifting is not an option cause the size and shape of the Msig matrix varies depending on input parameters.

you have any clue how to solve this problem, what can be the cause?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Matlab applies the fft over the columns of the matrix, numpy applies the fft over the last axis (the rows) by default. You want:

>>> np.fft.fft(Msig.T, axis=0)
array([[ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  1.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.-1.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j, -1.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+1.j,  0.+0.j]])

or

>>> np.fft.fft(Msig).T
array([[ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  1.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.-1.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j, -1.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+1.j,  0.+0.j]])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...