Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 890
Chris Avatar asked Dec 30 '11 15:12

Chris


1 Answers

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]])
like image 87
Bi Rico Avatar answered Oct 18 '22 22:10

Bi Rico