Matlab和numpy/scipy中的FFT给出不同的结果 [英] FFT in Matlab and numpy / scipy give different results

查看:604
本文介绍了Matlab和numpy/scipy中的FFT给出不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重新实现matlab工具箱之一. 他们在那边使用fft. 当我对相同的数据执行相同的操作时,我得到的结果与Matlab的结果不同. 看看吧:

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

如果我弄乱了np.fft.fft()/np.fft.fft2()/np.fft.fftn()的参数(轴等),我得到的最好的是相同的值但发生了偏移.不幸的是,手动移位不是一种选择,因为Msig矩阵的大小和形状取决于输入参数.

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?

推荐答案

Matlab默认将fft应用到矩阵的列上,numpy默认将fft应用到最后一个轴(行)上.您想要:

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

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

这篇关于Matlab和numpy/scipy中的FFT给出不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆