MATLAB Answers

S.M
0

How to get time for 50%, 90% and 100% of a graph?

Asked by S.M
on 18 Jul 2019
Latest activity Commented on by Star Strider
on 6 Aug 2019
Hi guys,
I have another question again. If you run the script which I have attached, you will get a graph and few calculations.
What I want to do is: I need to know after how many seconds the graph reaches 50% and 90% of its maximum.
I have come this far: My idea was it, to take the piece of the curve where the gradient is zero to calculate the average for 100%. From this value I would calculate the 50% and 90%. Because this values are calculated I won't find exactly them, so I'm searching for the next smaller and next bigger value. Now I would need the time, how long it took to get to 50% and to 90%.
I need to do this for 12 graphs to get more or less good average values. Can you please tell me if there is a better/easier way to get the result, or is my way ok? And how would you solve it for several graphs.
Thank you very much!
% Load cell array
load('c_test.mat');
% Take timetable from cell array
P1_100 = c{1}(100:end,1:1);
% Convert timetable to matrix
P1_100_T = timetable2table(P1_100);
P1_100_T = P1_100_T{:,2:2};
% Calculate the average for 100%
P1_100_av = mean(P1_100_T(:,:));
% Calculate 50% and 90%
P1_50 = P1_100_av*0.5;
P1_90 = P1_100_av*0.9;
% Load data for full curve
P1 = c{1}(:,1:1);
% Interpolate timetable and set the starttime at zero
dt = milliseconds(100);
IP1 = retime(P1,'regular','linear','TimeStep', dt);
StartTime = datetime('05.07.2019 00:00.000', 'InputFormat', 'dd.MM.yyyy mm:ss.SSS');
IP1.Properties.StartTime = StartTime;
scatter(IP1.Time, IP1.Volt);
% Convert interpolated timetable to matrix
IPM1 = timetable2table(IP1);
IPM1 = IPM1{:,2:2};
% Search for next smaller and next bigger value of 50%
P1_50_uG = IPM1(find(IPM1 < P1_50, 1, 'last'));
P1_50_oG = IPM1(find(IPM1 > P1_50, 1, 'first'));
% Search for next smaller and next bigger value of 90%
P1_90_uG = IPM1(find(IPM1 < P1_90, 1, 'last'));
P1_90_oG = IPM1(find(IPM1 > P1_90, 1, 'first'));

  1 Comment

I just noticed, that my code works well for 50% of the maximum value. The measurements in the range of 90% are jumping a bit, so that it shows me the upper value earlier than lower value and this is wrong.
Is there any other way to get the time how long did it take to reach 50% and 90% of the maximum?

Sign in to comment.

Products

1 Answer

Answer by Star Strider
on 19 Jul 2019
 Accepted Answer

Only some of your records follow the model you describe.
For those that do, this works (using the code you posted to get the ‘IP1.Time’ and ‘IP1.Volt’ vectors):
Tsec = second(IP1.Time); % Convert To Seconds
Volts = IP1.Volt; % Retrieve From Table
Voltsd = Volts - Volts(1); % Subtract Baseline Offset
VoltsL = Voltsd > 0.01;
Ix0 = find(Voltsd>0.01, 1, 'first');
fcn = @(b,x) b(1).*(1 - exp(b(2).*(x + b(3)))); % Model Equation
B = fminsearch(@(b) norm(Voltsd(VoltsL) - fcn(b,Tsec(VoltsL))), [1; -0.5; Tsec(Ix0);]); % Estimate Parameters
T50 = fzero(@(t) fcn(B,t) - 0.5*B(1), 1) % Time At 50% Of Max
T90 = fzero(@(t) fcn(B,t) - 0.9*B(1), 1) % Time At 90% Of Max
figure
plot(Tsec(VoltsL), Voltsd(VoltsL), '-')
hold on
plot(Tsec(VoltsL), fcn(B,Tsec(VoltsL)), '-r')
xl = min(xlim);
yl = min(ylim);
plot([xl T50], [1 1]*0.5*B(1),'--g', [1 1]*T50, [yl 0.5*B(1)],'--g')
plot([xl T90], [1 1]*0.9*B(1),'--c', [1 1]*T90, [yl 0.9*B(1)],'--c')
hold off
grid
xlabel('Time (s)')
ylabel('Volts')
text(T50, 0.5*B(1), sprintf('\\leftarrow 50%%: %4.1fV, %4.1fs', 0.5*B(1), T50), 'HorizontalAlignment','left', 'VerticalAlignment','middle')
text(T90, 0.9*B(1), sprintf('\\leftarrow 90%%: %4.1fV, %4.1fs', 0.9*B(1), T90), 'HorizontalAlignment','left', 'VerticalAlignment','middle')
Append my code to the code you posted to use it. I had to extract the times as seconds in order for the regression to work. It appears to g1ve good results, and estimates the time delay to onset (as ‘B(3)’) as well.
How to get time for 50%, 90% and 100% of a graph - 2019 07 19.png
Make appropriate changes to get the results you want.

  10 Comments

No worries!
Thank you for your compliments! Accepting my Answer and voting for it in addition are all the appreciation it is possible to express here, and I thank you for those! Also, yours is an interesting problem, and was fun to solve.
If it would be possible, I would give you more than one voting :)
The problem is interesting, yes, but my knowledge was not enough to solve it.
Once again, thank You so much. I wish You all the best!
Thank you again!
As always, my pleasure!

Sign in to comment.