How to optimize my Matlab code ?

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 comentario

Stephen23
Stephen23 el 11 de Abr. de 2017
Editada: Stephen23 el 11 de Abr. de 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:

Iniciar sesión para comentar.

 Respuesta aceptada

Rik
Rik el 11 de Abr. de 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 comentarios

Jan
Jan el 11 de Abr. de 2017
Or even better: Do not create a bunch of variables, but use the cell result as result.
Rik
Rik el 11 de Abr. de 2017
Editada: Rik el 11 de Abr. de 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 el 11 de Abr. de 2017
Editada: afef el 11 de Abr. de 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 .

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 11 de Abr. de 2017

0 votos

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 comentarios

afef
afef el 20 de Mayo de 2017
Editada: afef el 20 de Mayo de 2017
Please i have this part this part in my code :
[c,l]=wavedec(a,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea1,Ed1]=wenergy(c,l)
[c,l]=wavedec(b,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea2,Ed2]=wenergy(c,l)
[c,l]=wavedec(z,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea3,Ed3]=wenergy(c,l)
[c,l]=wavedec(d,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea4,Ed4]=wenergy(c,l)
[c,l]=wavedec(e,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea5,Ed5]=wenergy(c,l)
[c,l]=wavedec(f,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea6,Ed6]=wenergy(c,l)
[c,l]=wavedec(g,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea7,Ed7]=wenergy(c,l)
[c,l]=wavedec(h,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea8,Ed8]=wenergy(c,l)
[c,l]=wavedec(i,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea9,Ed9]=wenergy(c,l)
[c,l]=wavedec(j,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea10,Ed10]=wenergy(c,l)
[c,l]=wavedec(k,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea11,Ed11]=wenergy(c,l)
[c,l]=wavedec(ll,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea12,Ed12]=wenergy(c,l)
[c,l]=wavedec(m,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea13,Ed13]=wenergy(c,l)
[c,l]=wavedec(n,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea14,Ed14]=wenergy(c,l)
[c,l]=wavedec(o,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea15,Ed15]=wenergy(c,l)
[c,l]=wavedec(p,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea16,Ed16]=wenergy(c,l)
[c,l]=wavedec(q,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea17,Ed17]=wenergy(c,l)
[c,l]=wavedec(r,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea18,Ed18]=wenergy(c,l)
[c,l]=wavedec(s,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea19,Ed19]=wenergy(c,l)
[c,l]=wavedec(x,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea20,Ed20]=wenergy(c,l)
[c,l]=wavedec(u,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea21,Ed21]=wenergy(c,l)
[c,l]=wavedec(v,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea22,Ed22]=wenergy(c,l)
[c,l]=wavedec(w,4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea23,Ed23]=wenergy(c,l)
and i tried to make it short like this :
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;
t=(0:length(deriv)-1)/Fs;
result{n} = filtfilt(d1,deriv);
moyenne{n}=mean(result{n},2);
variance{n} = std(result{n},[], 2);
Maximale{n}= max(result{n},[],2);
Minimale{n} = min(result{n},[],2);
[c,l]=wavedec(result{n},4,'db4');
[cd1,cd2,cd3,cd4]=detcoef(c,l,[1,2,3,4]);
cA4=appcoef(c,l,'db4',4);
[Ea,Ed]=wenergy(c,l)
[Ea, Ed] = wenergy(c,l);
energie{n}= [Ea,Ed](result{n},[],2);
end
[a,b,z,d,e,f,g,h,i,j,k,ll,m,n,o,p,q,r,s,x,u,v,w]=result{:};
But i got error message "Unbalanced or unexpected parenthesis or bracket." Can you please help me how to fix it.
Replace
energie{n}= [Ea,Ed](result{n},[],2);
with
energie{n} = [Ea, Ed];
afef
afef el 21 de Mayo de 2017
Editada: afef el 21 de Mayo de 2017
ok it works thank you. Just i want to know if i want to call the second value of Ed how can i do that (because for Ea i have one value and for Ed i have 3 values) For example ihave this :
Ea =
50.9982
Ed =
1.1777 19.0690 20.2442 8.5108
and i want the value '19.0690' i tried this ans=energie{1:n:3} but it give me
50.9982 1.1777 19.0690 20.2442 8.5108
energie{n}{2}(3)
afef
afef el 21 de Mayo de 2017
i trird it but i got this error message
"Cell contents reference from a non-cell array object."

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 10 de Abr. de 2017

Comentada:

el 21 de Mayo de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by