




clear
close all

% two files with std addition data: A434_stdd.csv, and A578_stdd.csv

% first column: -1 = blank, the rest indicates uL of dye added

A = readtable('A578_stdadd.csv', 'headerlines', 1);

% extract data from table to arrays
uL = A{:,1};
I = A{:,2};
Iiso = A{:,3};
Ibase = A{:,4};

% find indicies for blank
iBlank = uL == -1;

% get blank intensities
Iblank = mean(I(iBlank));
Iblankiso = mean(Iiso(iBlank));
Iblankbase = mean(Ibase(iBlank));

% get sample intensities
I = I(~iBlank); % get intensities that aren't blanks
Iiso = Iiso(~iBlank);
Ibase = Ibase(~iBlank);

% Calculate absorbance
A = log10(Iblank ./ I);
Aiso = log10(Iblankiso ./ Iiso);
Abase = log10(Iblankbase ./ Ibase);

% Correct absorbance for baseline
Acorr = A - Abase;
Aisocorr = Aiso - Abase;

% fit polynomial to data
c = polyfit(Aiso, A, 1);
ccorr = polyfit(Aisocorr, Acorr,1);

% make some plots

figure
hold on; box on;
plot(Abase, 'ko');
ylabel('A760');
xlabel('Sample number');
title('Light stability test');

% plot A vs Aiso
figure
hold on; box on;
plot(Aiso, A, 'ko');
plot(Aiso, polyval(c, Aiso), 'k');
xlabel('Aiso');
ylabel('A');

% plot Acorr vs Aiso
figure
hold on; box on;
plot(Aisocorr, Acorr, 'ko');
plot(Aisocorr, polyval(ccorr, Aisocorr), 'k');
xlabel('Aiso corrected');
ylabel('A corrected');

% Look at the residuals
Aresid = Acorr - polyval(ccorr, Aisocorr);

figure
hold on; box on;
plot(Aisocorr, Aresid, 'ko');
xlabel('Aiso corrected');
ylabel('Residual (Acorr - Afit)');


%% Calculate residuals and R^2 using different sections of the data

% indicies where absorbance < 1.
iuse = Acorr < 1;

X = Aisocorr(iuse);
Y = Acorr(iuse);


% calculate residual
c_ = polyfit(X, Y, 1);
resid_ = Y - polyval(c_,X);

% calculate stats for residual
disp(['Mean resid = ',num2str(mean(resid_(resid_<0.003)))]);
disp(['Std resid = ',num2str(std(resid_(resid_<0.003)))]);

figure
hold on; box on;
plot(Y, resid_, 'ko');
plot(Y, zeros(size(Y)), 'k');
ylabel('residual Abs');
xlabel('A corrected');
























