简体   繁体   中英

How can I get same results for fft2() in MATLAB and Python?

I am translating a MATLAB code to Python code but I get different results from numpy fft2() and MATLAB fft2() . Difference is not small. How can I get same result?

Where S is (10,10,3) array

In Python:

Normin1 = fft2(S)
print("Normin is\n",Normin1[:,:,0])

Output(Just first row):
 Normin is
[[ 1.29098039e+01+0.00000000e+00j
-8.14368936e-01-4.02146547e-01j
4.57184468e-01+4.59545965e-01j  
2.37898348e-01-1.58643666e-01j
-6.89491738e-02+1.65467814e-01j  
3.29411765e-01-1.38777878e-17j
-6.89491738e-02-1.65467814e-01j  
2.37898348e-01+1.58643666e-01j
 4.57184468e-01-4.59545965e-01j 
-8.14368936e-01+4.02146547e-01j]

But in MATLAB:

 Normin1 = fft2(S);

 Output of first row
44.27451 +  0.00000i   
-1.04275 -  3.36765i
 0.72446 -  1.92128i   
 0.02706 +  0.09694i    
0.70692 +  0.28154i   
-0.90980 +  0.00000i    
0.70692 -  0.28154i    
0.02706 -  0.09694i
0.72446 +  1.92128i   
-1.04275 +  3.36765i

I solved my problem. Python does array operations row-wise. In contrast, Matlab/Octave does array operations column-wise. That why, it should be as follows to get same result,

MATLAB code:

Normin1 = fft2(S);

Python equivalent:

Normin1 = np.fft2(S.T).T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM