Effacer les filtres
Effacer les filtres

How to optimize my Matlab code ?

4 vues (au cours des 30 derniers jours)
afef
afef le 10 Avr 2017
Commenté : afef le 21 Mai 2017
Hi , i have written this Matlab code but i realize that's a little long so i want to optimize it but i don't know how . Can anyone help me please?
clc;
clear all;
close all;
load('C:\Users\del.dell-PC\pfe\base de donnée\patient sans
crise\chb01_01_edfm.mat')
deriv1=(val(1,:)-0)/2.559375;
Fs=256;
t1=(0:length(deriv1)-1)/Fs;
deriv2=(val(2,:)-0)/2.559375;
Fs=256;
t2=(0:length(deriv2)-1)/Fs;
deriv3=(val(3,:)-0)/2.559375;
Fs=256;
t3=(0:length(deriv3)-1)/Fs;
deriv4=(val(4,:)-0)/2.559375;
Fs=256;
t4=(0:length(deriv4)-1)/Fs;
deriv5=(val(5,:)-0)/2.559375;
Fs=256;
t5=(0:length(deriv5)-1)/Fs;
deriv6=(val(6,:)-0)/2.559375;
Fs=256;
t6=(0:length(deriv6)-1)/Fs;
deriv7=(val(7,:)-0)/2.559375;
Fs=256;
t7=(0:length(deriv7)-1)/Fs;
deriv8=(val(8,:)-0)/2.559375;
Fs=256;
t8=(0:length(deriv8)-1)/Fs;
deriv9=(val(9,:)-0)/2.559375;
Fs=256;
t9=(0:length(deriv9)-1)/Fs;
deriv10=(val(10,:)-0)/2.559375;
Fs=256;
t10=(0:length(deriv10)-1)/Fs;
deriv11=(val(11,:)-0)/2.559375;
Fs=256;
t11=(0:length(deriv11)-1)/Fs;
deriv12=(val(12,:)-0)/2.559375;
Fs=256;
t12=(0:length(deriv12)-1)/Fs;
deriv13=(val(13,:)-0)/2.559375;
Fs=256;
t13=(0:length(deriv13)-1)/Fs;
deriv14=(val(14,:)-0)/2.559375;
Fs=256;
t14=(0:length(deriv14)-1)/Fs;
deriv15=(val(15,:)-0)/2.559375;
Fs=256;
t15=(0:length(deriv15)-1)/Fs;
deriv16=(val(16,:)-0)/2.559375;
Fs=256;
t16=(0:length(deriv16)-1)/Fs;
deriv17=(val(17,:)-0)/2.559375;
Fs=256;
t17=(0:length(deriv17)-1)/Fs;
deriv18=(val(18,:)-0)/2.559375;
Fs=256;
t18=(0:length(deriv18)-1)/Fs;
deriv19=(val(19,:)-0)/2.559375;
Fs=256;
t19=(0:length(deriv19)-1)/Fs;
deriv20=(val(20,:)-0)/2.559375;
Fs=256;
t20=(0:length(deriv20)-1)/Fs;
deriv21=(val(21,:)-0)/2.559375;
Fs=256;
t21=(0:length(deriv21)-1)/Fs;
deriv22=(val(22,:)-0)/2.559375;
Fs=256;
t22=(0:length(deriv22)-1)/Fs;
deriv23=(val(23,:)-0)/2.559375;
Fs=256;
t23=(0:length(deriv23)-1)/Fs;
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
a = filtfilt(d1,deriv1);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
b = filtfilt(d1,deriv2);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
z= filtfilt(d1, deriv3);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
d = filtfilt(d1, deriv4);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
e = filtfilt(d1, deriv5);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
f = filtfilt(d1, deriv6);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
g = filtfilt(d1, deriv7);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
h= filtfilt(d1, deriv8);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
i = filtfilt(d1,deriv9);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
j = filtfilt(d1, deriv10);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
k= filtfilt(d1, deriv11);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
ll = filtfilt(d1, deriv12);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
m = filtfilt(d1, deriv13);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
n = filtfilt(d1, deriv14);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
o = filtfilt(d1, deriv15);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
p= filtfilt(d1, deriv16);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
q = filtfilt(d1, deriv17);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
r = filtfilt(d1, deriv18);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
s = filtfilt(d1, deriv19);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
x = filtfilt(d1, deriv20);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
u = filtfilt(d1, deriv21);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
v= filtfilt(d1, deriv22);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Passb
andRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple');
w = filtfilt(d1, deriv23);
  1 commentaire
Stephen23
Stephen23 le 11 Avr 2017
Modifié(e) : Stephen23 le 11 Avr 2017
Computers are actually only good at doing just one thing: repeatedly doing very simple tasks. So when you find yourself copy-and-pasting code one thousand time like that you are just doing the computer's job for it. Why waste your life doing exactly what the computer can do faster and better?
Do not replicate code like that: use loops, functions, or whatever other tools make this neater, faster, simpler, and easier to check. Save your time for doing the things that the computer cannot do (like figuring out the algorithm, and picking the colorscheme).
PS: Using lots of sequential variables is also a very bad sign, because then beginners will try to access them dynamically. DO NOT DO THIS unless you really want to write slow, buggy code:

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 11 Avr 2017
What you are asking for is not actually optimization, but rather code compactness. Therefor, I'm going to ignore the actual optimization, as it is not my field.
You can use a for-loop to make your code look more compact.
load('C:\Users\del.dell-PC\pfe\base de donnée\patient sans crise\chb01_01_edfm.mat')
Fs=256;
result=cell(23,1);
d1=designfilt('lowpassfir','PassbandFrequency',0.45,...
'StopbandFrequency',0.5,'PassbandRipple',3,'StopbandAttenuation',...
60,'DesignMethod','equiripple');
for n=1:23
deriv=(val(n,:))/2.559375;%-0 doesn't do anything
t=(0:length(deriv)-1)/Fs;%you are not using this anywhere
result{n} = filtfilt(d1,deriv);
end
%If you have Matlab 7.0 or later, the line below will work
[a,b,z,d,e,f,g,h,i,j,k,ll,m,n,o,p,q,r,s,x,u,v,w]=result{:};
%If it doesn't, use this line:
[a,b,z,d,e,f,g,h,i,j,k,ll,m,n,o,p,q,r,s,x,u,v,w]=deal(result{:});
  3 commentaires
Rik
Rik le 11 Avr 2017
Modifié(e) : Rik le 11 Avr 2017
That is what I would do as well, but I erred on the safe side, assuming the strange order of letters had some specific meaning and were going to be used later on.
And, as Jan suggests: avoid the clear all. The code I put in my answer overwrites all variables that already exist, so clear all is not necessary. If you really want something like this, at the very least replace it with clear variables, because you are even clearing all break-points.
afef
afef le 11 Avr 2017
Modifié(e) : afef le 11 Avr 2017
Ok,thank you for your help.Concerning the clear all i thought that i should write it to clear the workspace but now i understand.And about the order of letters actually i use it because i will use the decomposition using wavelt for each deriv so that's why .

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 11 Avr 2017
Start with omitting the brute clear all. It removes all loaded functions from the memory and reloading then from disk wastes a lot of time. Use functions instead to keep the workspace clean.
  5 commentaires
Walter Roberson
Walter Roberson le 21 Mai 2017
energie{n}{2}(3)
afef
afef le 21 Mai 2017
i trird it but i got this error message
"Cell contents reference from a non-cell array object."

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by