% Non-etalon_n - n-component analyzis imitation with bootstrap
% run after pre_ne.m

% bootstrap loop for concentrations retrieve
concentration = zeros(nB,nc); % bootstrap concentrations
curves_matrix = zeros(nx,nc);  %?

discrepancy = 10e6;
for iB = 1:nB
    for ic = 1:nc
        Dy=zeros(nx,ni(ic));
%         freqsB = FrInt{ic,1};
%         absintsB = FrInt{ic,2};
%         sigma = FrInt{ic,3};
        for j=1:ni(ic)
           Dy(:,j)=(FrInt{ic,2}(iB,j)/(2.5*FrInt{ic,3}(j)))* ...
               exp(-(0.5/FrInt{ic,3}(j)^2)*(x0-FrInt{ic,1}(iB,j)).^2);
        end %j
        y=(sum(Dy'))';
        curves_matrix(:,ic) = y';
    end % ic
    inv_curves_matrix = pinv(curves_matrix);
    concentration(iB,:) = inv_curves_matrix*exp_curve;
    % improvement
    theor_curveB = curves_matrix*concentration(iB,:)';
    discr = (theor_curveB-exp_curve).^2;
    discr_sum = sum(discr);
    if discr_sum < discrepancy
        discrepancy = discr_sum;
        i_best = iB;
        c_bestB = concentration(iB,:);
        discr_bestB = discr;
        inv_curves_matrix_best = inv_curves_matrix;
        theor_curveB_best = theor_curveB;
%        plot(x0,discr_bestB)
    end %if
end % iB

% statistics on concentration
c1c2 = zeros(nc,4);
c1c2(:,1) = mean(concentration);
c1c2(:,2) = std(concentration);
c1c2(:,3) = min(concentration);
c1c2(:,4) = max(concentration);

%     hist(concentration(:,1))
% 
%     hist(concentration(:,2))
