0

I am trying to convert a simple plot code from Matlab to Python. The original code can be seen as:

   figure()
testlist = [
        0.03046874845
        -0.05527424795
        0.01682650241
        0.006369163085
        0.00203891158
        0.0161380391
        -0.04452231783
        0.05169710771
        -0.03534597353
        0.0162988922
        -0.007158760388
        0.009835218937
        0.00781369055
        -0.03391821702
        0.03218318175
        -0.02270531812
        -0.00313687103
        -0.003567927366
        0.02211840897
        -0.00289949565
        0.003331161139
        -0.00084616717
        0.03405780102
        -0.0155820586
        -0.03855276047
        0.01964289036
        -0.03500874771
        -0.03498725681
        -0.03611383299
        0.04926392503
        0.0641901016
        0.04232309722
        0.1535235824
        0.0563995637
        0.04254218955
        -0.4660826409
        -0.1076933526
        -0.6828857313
        0.6867590539
        1.262460447 ]; % #eqs = 28922979, pred_res_rms = 0.02156535305
[h,f] =freqz(0.0215654,[1 -flipud(testlist)'],2048,2);

In order to convert this code into Python, I defined a List with the same name as in Matlab (testlist) with the same values in the Matlab array.

import matplotlib
import scipy
from scipy.signal import freqz
import numpy as np

testlist = list(reversed(testlist))

for i in range(len(testlist)):

    testlist[i] = 1 - testlist[i] 

w, h =freqz(0.0215654,testlist,2048)
f = 2 * w /(2*np.pi)

With this code, I get the same f value as the matlab code. However, when I compare the h values between two codes, I get completely different values. Both the Matlab array h and the Python list h contain complex values but they aren't the same even though they should be. How can I bring the Python version so that the output list h is identical to the one from Matlab?

The first 15 values of the Matlab output of h:

-95002000
-1.2697e+01 - 6.6976e+02i
-1.2737e+01 - 3.3507e+02i
-1.2811e+01 - 2.2359e+02i
-1.2914e+01 - 1.6790e+02i
-1.3047e+01 - 1.3453e+02i
-1.3208e+01 - 1.1230e+02i
-13.3990 -96.4322i
-13.6180 -84.5337i
-13.8644 -75.2708i
-14.1373 -67.8428i
-14.4352 -61.7387i
-14.7561 -56.6166i
-15.0977 -52.2382i
-15.4568 -48.4324i

The first 15 values of the Python output of h:

 0.00055296+0.00000000e+00j 0.00055278+1.69856946e-05j
 0.00055224+3.39703866e-05j 0.00055135+5.09530728e-05j
 0.00055009+6.79327484e-05j 0.00054847+8.49084066e-05j
 0.0005465 +1.01879038e-04j 0.00054416+1.18843629e-04j
 0.00054146+1.35801163e-04j 0.00053839+1.52750617e-04j
 0.00053495+1.69690963e-04j 0.00053114+1.86621167e-04j
 0.00052697+2.03540186e-04j 0.00052241+2.20446971e-04j
 0.00051748+2.37340461e-04j
L. Scott Johnson
  • 4,213
  • 2
  • 17
  • 28
Oguzhan
  • 1
  • 1
  • "Completely different" is impossible for us to assess, given you don't show their values. Besides, MATLAB is not Python, so it's likely that the implementation of `freqz` is different between the two (read the docs and tells us whether they are or not) and even if the implementation is (sort-of) equal, thei translation into machine code is bound to result in small differences. These differences might seem large, but may well boil down to linear algebra and some rotations of vectors. Again, we need to see your `h` vectors, please [edit] the question to include them. – Adriaan Nov 02 '21 at 12:46
  • I added the first 15 outputs of the h value. What I do not understand is exactly the implementation difference of freqz on both platforms. I made some research and that is why I implemented my Python freqz a bit different than the Matlab freqz but it did not seem to work very well. – Oguzhan Nov 02 '21 at 12:55

1 Answers1

1

The error is not in the implementation of freqz, they are the same. The error is instead in the way you are passing the data into freqz.

MATLAB interprets

1 -testlist

as a 1 followed by the negative of testlist. If you remove the space:

1-testlist

will be interpreted as 1 minus the testlist, which is what you need to have the two implementations match.

TallBrianL
  • 1,230
  • 1
  • 9
  • 14