i Have 2 programs of Matlab but I need these codes in python code.

5 views (last 30 days)
Data Extract(Matlab code)
function [Allvals]= DataExtract(fname, Fs, nfft,fnum, classl, classval) data = csvread(fname);
Allvals=[];
% % Visualization
% subplot(2,1,1);
% h1 = plot(data(:,7:8));
% %---- % Window parameters
wlen=128;
olap = 20;
ind = 1;
hold on;
num=1;
while ind+wlen < size(data,1) hold on;
t = ind:ind+wlen-1;
dSliceA=data(t,7);
dSliceG=data(t,8);
% % Visualize
% subplot(2,1,1);
% h2 = plot(t,dSliceA,'g');
% subplot(2,1,1);
% h3 = plot(t,dSliceG,'g');
% title([classl,' Iteration #',num2str(fnum)]);
% pause(0.1);
% delete(h2); delete(h3);
% % % -------- %
ind = ind+olap;
% Frequency Analysis (Accelerometer);
x = dSliceA - mean(dSliceA);
y = fft(x,nfft);
y = abs(y.^2);
y = y(1:1+nfft/2);
[v,k] = max(y);
f_scale = (0:nfft/2)* Fs/nfft;
festA = f_scale(k);
% Frequency Analysis (Gyroscope);
x = dSliceG - mean(dSliceG);
y = fft(x,nfft);
y = abs(y.^2);
y = y(1:1+nfft/2);
[v,k] = max(y);
f_scale = (0:nfft/2)* Fs/nfft;
festG = f_scale(k);
% Extract Values Accvals(num,:)=[max(abs(dSliceA)),min(abs(dSliceA)), mean(dSliceA), std(dSliceA), festA];
Gyrovals(num,:)=[max(abs(dSliceG)),min(abs(dSliceG)), mean(dSliceG), std(dSliceG), festG];
Allvals=[Allvals;
[Accvals(num,:), Gyrovals(num,:), classval]];
% % Visualization
% subplot(2,1,2);
% stem(num,Allvals(num,5));
% hold on;
% num=num+1;
%------------------ end end
Getdata(Matlab code)
% Get Data from files
% Acquiring Statistics from Parkinson's Motion Data using Moving Window
% Data Sampled at 20 Hz
% Received Data has 8 columns:
% Acc_x, Acc_y, Acc_z, Gyro_X, Gyro_y, Gyro_z,Acc_Norm, Gyro_norm clear all;
close all;
clc;
Fs = 20;
nfft = 64;
classes = {'Intentional', 'Rest', 'Tremor'};
% Switch to directory dirAll ='C:\Program Files\MATLAB\Work\Parkinsons\Datasets';
Values = [];
for i = 3:3 cd([dirAll,'\',classes{i}]);
files = dir('*.csv');
%Loads into structure for file = 1:length(files)
% i=10; fname = files(file).name;
Avals=DataExtract(fname,Fs,nfft,file, classes{i},i);
close;
disp([classes{i},' Iteration #',num2str(file),' done!']);
Values = [Values;Avals]; end cd(dirAll);
csvwrite([classes{i},'.csv'],Values);
display(['Completed writing CSV file for ', classes{i}]);
end csvwrite('Alldata.csv',Values);
display('Completed writing CSV file for all data');

Answers (1)

Arjun
Arjun on 2 Jun 2025
I understand that you want to convert MATLAB code to Python.
Kindly refer to the following MATLAB Answers :
  1. To call MATLAB code from Python: https://www.mathworks.com/matlabcentral/answers/426258-how-to-convert-the-matlab-code-into-the-python
  2. To convert MATLAB code to Python: https://www.mathworks.com/matlabcentral/answers/1904000-convert-matlab-code-to-python
I hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!