function [fitTime, critpoint] = findtime(der2data)
%This function traverses the second derivative of the data and finds the times
%that the linear regression curves should be fitted with.
fitTime(1) = 1; %First time is 0.0
[trash fitTime(2)] = max(der2data(1:100)); %Second time is first max from 0 to 1 sec.
[trash critpoint] = min(der2data(1:250)); %critical point is first min of filter from 0 to %2.5 sec.
%This is my alogrithm to find the third and fourth fitTimes. I traveled the data
%backwards from 9 sec. with a window of 25 data points in order to account for noisy
%derivatives and slight fluctuations. When I encountered a window that had an absolute
%max or a min enclosed, I used the max or min function to calculate the absolute max or
%min of that window. The fourth time is the first max to the left of the first minimum
%traveling backwards from 9 sec. If this time is below 7 sec. then the fourth point is
%automatically 9 sec. The third time is the second max from the start of the data at 0
%sec.
%Find first minimum from righthand side of derivative
min1 = 0;
i = 900;
while min1 == 0,
if der2data(i-25) < der2data(i),
j = i;
while der2data(j-25) < der2data(j),
j = j - 1;
end
[trash min1] = min(der2data(j-25:j));
end
i = i - 1;
end
%Find first maximum to the left of the minimum just found
min1 = min1 + j - 25;
i = min1;
if der2data(i-25) > der2data(i),
j = i;
while der2data(j-25) > der2data(j),
j = j - 1;
end
[trash max1] = max(der2data(j-25:j));
max1 = max1 + j - 25;
else
[trash max1] = max(der2data(i-25:i));
max1 = max1 + i - 25;
end
%Check to make sure the maximum is greater than 7 seconds
if max1 < 700,
max1 = 900;
end
fitTime(4) = max1;
%Find the second maximum from the start of the derivative and set it equal to the 3rd fit time
maxlow = critpoint;
if der2data(maxlow + 25) <= der2data(maxlow),
[trash maxlownew] = max(der2data(maxlow:maxlow + 25));
else
while der2data(maxlow + 25) > der2data(maxlow),
maxlow = maxlow + 1;
end
[trash maxlownew] = max(der2data(maxlow:maxlow + 25));
end
maxlow = maxlownew + maxlow;
fitTime(3) = maxlow;
fitTime = fitTime *.01;