Python Forum
problem of converting Matlab code to python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem of converting Matlab code to python
#1
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?
Reply
#2
https://realpython.com/matlab-vs-python/
Reply
#3
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convolution "same" in Python doesn't work as Matlab claw91 4 3,774 Oct-01-2020, 08:59 AM
Last Post: claw91
  Python equivalent of Matlab code kwokmaster 5 7,098 Apr-03-2020, 12:02 AM
Last Post: scidam
  Is there similar function to lsqnonlin (in matlab) in python? Jay_Nerella 1 5,969 Nov-11-2019, 08:40 AM
Last Post: feli_x
  Matlab to Python Ariane 6 7,093 Jun-14-2018, 12:08 PM
Last Post: Ariane
  Python code optimization problem servanm 2 2,820 May-23-2018, 01:28 PM
Last Post: servanm
  How to use .m matlab file in python ? sameer 5 15,401 May-10-2018, 09:39 AM
Last Post: wavic
  Importing matlab cell array (.mat) into a python list scanato 0 8,674 Nov-15-2017, 11:04 AM
Last Post: scanato
  The problem of converting string to float by using CSV file CChen 2 12,956 Jul-11-2017, 03:32 PM
Last Post: CChen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020