%fcncode.m
%
%Purpose:   removes degradation effects of fluorescence (F), assuming the model
%           equation of $\frac{dF}{dt} = r(t) - k_dF$, where r(t) = rate of synthesis term and
%           $k_d$ = degradation constant = 0.0362/min
%
%Inputs: 1. time: time at which fluorescence values were measured
%        2. dataset: fluorescence values at t = time
%
%Outputs: 1. xxtime & yytime = interpolated values (time & data,
%                                                           respectively)
%         2. newtime: same as xxtime 
%         3. y_prime: derivative values at time xxtime
%         4. dytime: interpolated r(t) values at t = xxtime
%         5. istor: fluorescence values without degradation effects

function [xxtime,yytime,newtime,y_prime,dytime,istor] = fcncode(time,dataset)

%Hermite interpolation and derivative
%Interpolate using time vs. dataset and find derivatives at
%points xxtime
it0 = time(1);
it1 = time(end);
xxtime = it0:.01:it1;
yytime = pchip(time,dataset,xxtime);
pp=pchip(xxtime,yytime);
p_der = fnder(pp,1);
y_prime = ppval(p_der,xxtime);

%finds r(t)= rate of synthesis term and hermite interpolate (ie find derivative function without degradation)
%k_d = 0.0362/min is degradation constant determined from nonlinear least squares fitting of degradation tag data 
%60 is time conversion from /min to /hr
output = y_prime + 0.0362*60*yytime; %adds $k_dF$ to both sides of model equation to obtain r(t)

%interpolates to find r(t)
dpp = pchip(xxtime,output); 
dytime = pchip(xxtime,output,xxtime);
istor=[];

%integrate
ti = xxtime;
ti(1)=[];
for i=1:length(ti)
istor(end+1) = integral(@(x)ppval(dpp,x),0,ti(i));
end
istor = [0,istor];
istor = yytime(1) + istor; 
newtime = xxtime;

%%The above function is applied to each of the 17 datasets. Each dataset
%%represents the fluorescence values at a particular charge. The maximum
%%values of each istor is saved and plotted against the corresponding
%%charge. We see a linear relationship of $y= (-1.543*10^5)x + 3.814$.
