function [EstConc, d_net, SEN, SEL, LOD, STN, epsz] = FigmeriC(ConcCal, AbsCal, AbsUN)
%[EstConc,            ] = figmeric(ConcCal, AbsCal, AbsUN);
%Calculate analytical figures of merit based on Classical Multivariate Calibration
%   INPUT
%ConCal  : sp by 1 concentration vector where sp is the number of species 
%                                     in the one-component calibration samples
%AbsCal  : n by m absorbance matrix where n is the number of samples for calibration, 
%                                     and m is the number of wavelengths
%AbsUN   : su by m absorbance matrix where su is the number of samples with unknown concentrations, 
%                                     and m is the number of wavelengths
%
%   OUTPUT
%EstConc : su by sp estimated concentration matrix where su is the number of samples 
%                                      with unknown concentrations, 
%                                     and sp is the number of species which must have been involved
%                                      in calibration samples
%d_net   : su by sp scalar net analytical signal matrix where su is the number of samples 
%                                      with unknown concentrations, 
%                                     and sp is the number of species
%SEN     : 1 by sp sensitivity vector where sp is the number of species
%
%SEL     : 1 by sp selectivity vector where sp is the number of species
%
%LOD     : su by sp limit of detection matrix where su is the number of samples 
%                                      with unknown concentrations, 
%                                     and sp is the number of species
%STN     : su by sp signal to noise matrix where su is the number of samples 
%                                      with unknown concentrations, 
%                                     and sp is the number of species
%epsz    : su by 1 error in the data vector where su is the number of samples 
%                                      with unknown concentrations, 
%
%Created by Robert Rajko (rajko@sol.cc.u-szeged.hu) at Nov 15, 2001
%based on the following papers:
%Lorber (1986) Anal.Chem. 58:1167-1172
%Lorber, Faber, Kowalski (1997) Anal.Chem. 69:1620-1626

AbsCal = AbsCal';                     % transpose absorbance matrix of calibration
[m,n] = size(AbsCal);                 % m wavelengths _by_ n samples 
AbsUN = AbsUN';                       % transpose absorbance matrix of unknowns
su = size(AbsUN,2);                   % su samples with unknown concentration
sp = size(ConcCal);                   % sp is the number of species

for i_su = 1:su                       % i_su is the index from 1 to su (samples with unknown concentration)
    for i_sp = 1:sp                   % i_sp  is the index from 1 to sp (number of species (components))
        
        if i_sp == 1
            AbsCalMinusSpec = AbsCal(:,2:sp);
        elseif i_sp == sp
            AbsCalMinusSpec = AbsCal(:,1:sp-1);
        else
            AbsCalMinusSpec = [AbsCal(:,1:i_sp-1) AbsCal(:,i_sp+1:sp)];
        end
        
        AbsCalOrtSpec = (eye(m)-AbsCalMinusSpec*pinv(AbsCalMinusSpec))*AbsCal(:,i_sp);

        AbsUNOrtSpec(:,i_su) = (eye(m)-AbsCalMinusSpec*pinv(AbsCalMinusSpec))*AbsUN(:,i_su);

        if i_su == 1
            SEN(i_sp) = norm(AbsCalOrtSpec);     
                                      % SENSITIVITY
            SEL(i_sp) = norm(AbsCalOrtSpec)/norm(AbsCal(:,i_sp));
                                      % SELECTIVITY
        end                              

        EstConc(i_sp,i_su) = AbsCalOrtSpec'*AbsUN(:,i_su)/norm(AbsCalOrtSpec)^2;
                                      % ESTIMATED CONCENTRATION 

        epsz(i_su) = sqrt(AbsUN(:,i_su)'*(eye(m)-AbsCal*pinv(AbsCal))*AbsUN(:,i_su)/(m-n));
                                      % ERROR IN THE DATA

        d_net(i_sp,i_su) = norm(AbsUNOrtSpec(:,i_su));
                                      % SCALAR NET ANALYTICAL SIGNAL

        STN(i_sp,i_su) = d_net(i_sp,i_su)/epsz(i_su);
                                      % SIGNAL TO NOISE RATIO

        LOD(i_sp,i_su) = 1/(norm(AbsCalOrtSpec)/(3*epsz(i_su))-1);
                                      % LIMIT OF DETECTION
    end
end

EstConc = EstConc';                   % transpose estimated concentration matrix
d_net = d_net';                       % transpose scalar net analytical signal
LOD = LOD';                           % transpose limit of detection matrix 
STN = STN';                           % transpose signal to noise matrix 
epsz = epsz';                         % transpose error in data vector