Python Forum
problem of converting Matlab code to python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: problem of converting Matlab code to python (/thread-39341.html)



problem of converting Matlab code to python - DongyanZ - Feb-02-2023

Hi my friend,

I am using Jupyter Notebook to handle my data. Now I have a csv file with 6 columns and 32768 rows, but I only need the 2nd, 5th and 6th columns and their first 4000 rows data. Here is my Matlab code:

clear
clc
close all

A=readmatrix('.\50clk.csv');

Time = A(2:32768,2);
I = A(2:32768,5)-mean(A(2:32768,5));
Q = A(2:32768,6)-mean(A(2:32768,6));

Q_BP = bandpass(Q,[15e3 25e3],Fs);
I_BP = bandpass(I,[15e3 25e3],Fs);

S =I_BP+1i*Q_BP;
phi_unwrap = unwrap(angle(S(1:4000)));
c = polyfit(Time(1:4000),phi_unwrap,1);
f_cal(1) = abs((c(1)/2/pi))

Can anyone help me to convert such code into python?


RE: problem of converting Matlab code to python - deanhystad - Feb-02-2023

https://realpython.com/matlab-vs-python/


RE: problem of converting Matlab code to python - jefsummers - Feb-03-2023

Strongly recommend reading the material Dean Hystad linked to above.
Will add - best to use a pandas dataframe for your data - like a smart spreadsheet. You mentioned that you have a csv file to convert. See the method read_csv in the Pandas Documentation Here with particular attention to the usecols option to select the columns and skipfooter option to limit to the first 4000 rows. All of this should come down to something like (untested, forgive me in advance)
import pandas as pd

df = pd.read_csv("myfile.csv", delimiter=',', usecols=[1,4,5], skipfooter=28767)
df.head()