Reputation: 1
I have Matlab code and I want to translate it into Python. here I have an array
python array
import numpy as np
a = [ 81.42663125 -5.6074337j, -26.39236508 -2.20896985j, 0.+0.j, 0.+0.j, 0.+0.j, 81.42663125-5.6074337j, -26.39236508-2.20896985j, 0.+0.j, 0.+0.j, 0.+0.j, -41.21717133+13.27058111j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,0.+0.j]
b = np.asarray(a)
inverse = np.fft.ifft(b)
Matlab array (same as python)
a = [ 81.42663125 -5.6074337i; -26.39236508; -2.20896985i; 0.+0.i; 0.+0.i; 0.+0.i; 81.42663125-5.6074337i; -26.39236508-2.20896985i; 0.+0.i; 0.+0.i; 0.+0.i; -41.21717133+13.27058111i; 0.+0.i; 0.+0.i; 0.+0.i; 0.+0.i; 0.+0.i];
inverse = ifft (x);
I tried to inverse it with numpy.fft.ifft
it turns like this:
[4.30321006-0.14763913j 5.66633932+3.89294213j -0.79347125-5.89179416j
6.84928702-3.05455565j 9.8032857 +2.39782633j -2.65998456-3.6116546j
10.67141555-4.33907798j 11.24112597+2.56947076j -2.57607321+0.82941132j
11.85609673-4.26124876j 9.03685629+3.33776417j -1.27864139+4.64168411j
8.82623526-4.48145694j 5.49420633+2.5781028j 1.44185722+5.49124955j
3.54488621-5.55845764j]
while the result from matlab looks like this
[4.30321006250000 + 0.147639125000000i
3.54488621443892 + 5.55845764203018i
1.44185722459799 - 5.49124955185143i
5.49420632611877 - 2.57810280349092i
8.82623525708333 + 4.48145694500000i
-1.27864138655093 - 4.64168410598597i
9.03685628560142 - 3.33776416665201i
11.8560967270980 + 4.26124875584204i
-2.57607320833333 - 0.829411319166666i
11.2411259692567 - 2.56947075987604i
10.6714155516520 + 4.33907798310142i
-2.65998456069909 + 3.61165460362471i
9.80328570125000 - 2.39782632583333i
6.84928701535530 + 3.05455564883182i
-0.793471249351424 + 5.89179416040201i
5.66633931998235 - 3.89294213097583i]
I tried with another matrix, it turns out different. I tried transpose the python array first but nothing good came. how can I obtain result same as matlab?
what I've changed:
Upvotes: 0
Views: 171
Reputation: 11
I just checked the output for the ifft
of your MATLAB array. I'm sure your first and second element are supposed to be [ 81.42663125-5.6074337i; -26.39236508-2.20896985i;
. The spacing in between the real and imaginary part of these equations and the semicolon in your second element are causing you these issues.
edit:
My python results:
This is what I have for my MATLAB code:
Unless you are talking about how MATLAB is outputting the array vs python, I believe you just have minor errors in the MATLAB. Python is going to be outputting is as a one-dimensional array
Upvotes: 1