Find automatically data of interest
Show older comments
Hello everyone,
I havew a question about how to improve my code.
I have a time-Temperature profile that is a bit noisy: my goal is to automatically find the heating, cooling and isotheramal portions by only looking at the first derivative of the temperature with respect to the time. Unluckily, the code is not perfect but I don't know how to improve it: can anyone help me?
Attached you can fine the test data and the code I use together with the subroutines.
close all
clear
tic
load 'conv_ageing_atm5_m60p80_01to2048s_bif_74165.txt'
conv_ageing_atm5_m60p80_01to2048s_bif_74165;
t=conv_ageing_atm5_m60p80_01to2048s_bif_74165(1:end,2);
T=conv_ageing_atm5_m60p80_01to2048s_bif_74165(1:end,3);
Cp=conv_ageing_atm5_m60p80_01to2048s_bif_74165(1:end,4);
time=conv_ageing_atm5_m60p80_01to2048s_bif_74165(1:end,5);
un=[1:1:length(T)];
ratew=(gradient(T)./gradient(time));
rate=smoothdata(ratew,'movmedian',30);
rate=nearest(rate);
flag=zeros(length(T),1);
for i=1:length(T)
if rate(i)>0;
flag(i)=1;
else if rate(i)==0;
flag(i)=0;
else if rate(i)<0;
flag(i)=-1;
end
end
end
end
[y]=remove_outliers(flag);
figure(1)
plot(un,flag,'o')
hold on
plot(un,y,'ro')
[Aw,Bw,Cw]=group_data(y);%Aw=heating/Bw=isother/Cw=cooling
[Vw]=organize_vector(Aw);%heating
[Uw]=organize_vector(Bw);%isotherm
[Ww]=organize_vector(Cw);%cooling
formatSpec = 'You have %d heating scans\n';
fprintf(formatSpec,length(Vw))
formatSpec2 = 'You have %d isotherms\n';
fprintf(formatSpec2,length(Uw))
formatSpec3 = 'You have %d cooling scans\n';
fprintf(formatSpec3,length(Ww))
toc
5 Comments
dpb
on 25 Oct 2022
for i=1:length(T)
if rate(i)>0;
flag(i)=1;
else if rate(i)==0;
flag(i)=0;
else if rate(i)<0;
flag(i)=-1;
end
end
end
end
Use elseif command instead of nesting another if inside. The above is correctly written as
for i=1:length(T)
if rate(i)>0;
flag(i)=1;
elseif rate(i)==0;
flag(i)=0;
elseif rate(i)<0;
flag(i)=-1;
end
end
Then, instead of if...elseif...end use
flag=sign(rate);
[y]=remove_outliers(flag);
Once you've converted to sign alone, there will be no outliers. If you're going to do that, probably would need to do it at least before there and most likely before the smoothing/filtering that will also have knocked any real outliers down/out.
Jan
on 25 Oct 2022
Please mention, what you consider as "the code is not perfect".
Daniele Sonaglioni
on 25 Oct 2022
Daniele Sonaglioni
on 25 Oct 2022
dpb
on 25 Oct 2022
Answers (1)
Jan
on 25 Oct 2022
Some methods are "ugly":
load 'conv_ageing_atm5_m60p80_01to2048s_bif_74165.txt'
Loading data directly into the workspace creates variables dynamically. This impedes Matlab's JIT acceleration and the debugging. Prefer to load data into a specific variable:
Data = load('conv_ageing_atm5_m60p80_01to2048s_bif_74165.txt');
This line is a waste of time only:
conv_ageing_atm5_m60p80_01to2048s_bif_74165;
Use a less painful name for variables. Hiding important information in the name of variables is a programming anti-pattern:
value = Data.conv_ageing_atm5_m60p80_01to2048s_bif_74165;
Use ":" instead of "1:end", because it is faster:
% Replace: t = conv_ageing_atm5_m60p80_01to2048s_bif_74165(1:end,2); by
t = value(:, 2);
Replace the code:
flag=zeros(length(T),1);
for i=1:length(T)
if rate(i)>0;
flag(i)=1;
else if rate(i)==0;
flag(i)=0;
else if rate(i)<0;
flag(i)=-1;
end
end
end
end
by
flag = sign(rate);
[] is Matlab's operator for a concatenation of arrays. In
un=[1:1:length(T)];
you concatenate the vector 1:length(T) with nothing. Cleaner and faster:
un = 1:length(T);
1 Comment
Daniele Sonaglioni
on 25 Oct 2022
Categories
Find more on Smoothing and Denoising in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!