How to speed up my code

2 views (last 30 days)
Tanmay Agarwal
Tanmay Agarwal on 1 Feb 2019
Commented: Tanmay Agarwal on 1 Feb 2019
f= 'test_new.xlsx';
for i=2687:6324
k=num2str(i);
x=num2str(i+1);
xlsrange=['A',x];
FileNames=['acc (',k,').46']
Data=dlmread(FileNames);
j=3;
M=0;
M1=rms(Data(:,j)); %% rms
M2=kurtosis(Data(:,j)); %% kurtosis
M3=max(abs(Data(:,j))); %% peak
M4=max(abs(Data(:,j)))./rms(Data(:,1)); %% crest factor
M5=std(Data(:,j)); %% Standard deviation
M6=rssq(Data(:,j)); %% Root-sum-of-squares level
M7=var(Data(:,j)); %% Variance
M8=peak2peak(Data(:,j)); %% Maximum-to-minimum difference
M9=min(Data(:,j)); %% Smallest elements in array
M10=max(Data(:,j)); %% Largest elements in array
M11=peak2rms(Data(:,j)); %% Peak-magnitude-to-RMS ratio
M12=meanfreq(Data(:,j)); %% Mean frequency
M13=medfreq(Data(:,j)); %% Median frequency
M14=range(Data(:,j)); %% Range of values
M15=mad(Data(:,j)); %% Mean or median absolute deviation
M16=iqr(Data(:,j)); %% Interquartile range
M17=harmmean(Data(:,j)); %% Harmonic mean
M18=snr(Data(:,j)); %% Signal-to-noise ratio
M19=powerbw(Data(:,j)); %% Power bandwidth
M20=enbw(Data(:,j)); %% Equivalent noise bandwidth
M21=obw(Data(:,j)); %% Occupied bandwidth
M22=sfdr(Data(:,j)); %% Spurious free dynamic range
M23=sinad(Data(:,j)); %% Signal to noise and distortion ratio
M24=thd(Data(:,j)); %% Total harmonic distortion
M25=toi(Data(:,j)); %% Third-order intercept point
M26=median(Data(:,j)); %% median
M27=mean(Data(:,j)); %% mean
M28=mode(Data(:,j)); %% Mod
time=10*(i);
M=horzcat(time,M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12,M13,M14,M15,M16,M17,M18,M19,M20,M21,M22,M23,M24,M25,M26,M27,M28);
xlswrite(f,M,j,xlsrange);
end
My code reads 6000 files ,each file including 20k data rows in one column.How can i make this run faster.Its taking around 3 hours now.

Answers (1)

KSSV
KSSV on 1 Feb 2019
Edited: KSSV on 1 Feb 2019
I think almost all the codes up there are vectorised......you can striaght away use a matrix and specify the dimension i.e you want calculation along row or column. Please read the documentation. YOu need not to run a loop, you can striaght away use the matrix as input and specify the dimension.
Ex: RMS
A = rand(100) ;
rms(A,1) % rms along columns
rms(A,2) % rms along rows
  2 Comments
Tanmay Agarwal
Tanmay Agarwal on 1 Feb 2019
Its a fact that vectorized code is faster in matlab,suggest something else
Tanmay Agarwal
Tanmay Agarwal on 1 Feb 2019
And Data in this code is a 20480x4 matrix

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!