% SUPPLEMENTARY MATERIAL "Source S2": Demultiplexing experimentally-acquired spectra.
%
% Example source code for “Method for assessing the reliability of
% molecular diagnostics based on multiplexed SERS-coded nanoparticles.”
% (Manuscript #PONE-D-13-01484)
% Load reference spectra
s440 = load('s440reference.asc')';
bkg = load('bkg.asc')';
% Build matrix (called "R") of reference spectra (S440, known
% background)and polynomial terms (x^3, x^2, x, constant)
R(:,1) = s440;
R(:,2) = bkg;
R(:,3) = (1:length(s440)).^3;
R(:,4) = (1:length(s440)).^2;
R(:,5) = (1:length(s440));
R(:,6) = 1;
% Load data to be processed
data = load('trial_n.asc');
% Pre-allocate space for least-squares outputs
residuals440 = zeros(num_trials,num_points);
concentration_errors440 = zeros(num_trials,num_points);
for i = 1:length(data)
[x,flag] = lsqr(R,data(i,:));
residuals(i,1:length(s440)) = test-R*x;
RFE(i) = norm(data(i,:) - squeeze(residuals(i,:)))/norm(test);
NonFlavorComponents = x(2,1)*R(:,2) ...
+ x(3,1)*R(:,3) ...
+ x(4,1)*R(:,4) ...
+ x(5,1)*R(:,5) ...
+ x(6,1)*R(:,6);
SRI(i) = norm(data(i,:) - squeeze(residuals(i,:)) - NonFlavorComponents)/norm(data(i,:) - NonFlavorComponents);
%In this particular example, we are measuring a sample known to
% have a concentration equal to the reference sample and therefore
% we subtract '1' (the relative actual concentration) from the
% computed weight and divide by 1.
conc_error_S440(i,j) = (x(1,1)-1)/1;
end