clearvars -except dataSMU
close all

% --- USER INPUT ---

IMUX=10; % Mux to be analyzed
x1=0; %start fit of transient in seconds
x2=0.35; %end fit of transient in seconds
lPlotFit=0; % 1 if plots should be done
lWriteFitToFile=1;
lNorm=1; %0: no normalization; 1: normalization between 1 and 0; 2:normalization between 1 and offset/Imax
% ---

ft = fittype( 'a*exp(-b*x)+c*exp(-d*x)+e', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Lower = [0 0 0 0 -Inf];
opts.StartPoint = [0.7 350 0.08 73 0.18];
opts.Display = 'Off';
opts.MaxFunEvals = 6000;
opts.MaxIter = 4000;

data={};
ipp=0;
for i=1:length(dataSMU)
    d=dataSMU(i);
    if d.iMux==IMUX
        tr=d.trnsnts;
        all_b1=zeros(1,length(tr));
        all_b2=zeros(1,length(tr));
        for j=1:length(tr)
            ipp=ipp+1;
            x=tr(j).t;
            y=tr(j).id;
            ii=x>x1 & x<x2;
            x=x(ii);
            y=y(ii);
            
            if lNorm==1
                y=y-mean(y(end-10:end));
                y=y/y(1);
            elseif lNorm==2
                y=y/y(1);
            end
            
            %fit     
            [fitresult, gof] = fit( x, y, ft, opts );
            dataSMU(i).trnsnts(j).fitresult=fitresult;
            dataSMU(i).trnsnts(j).gof=gof;
            all_b1(1,j)=fitresult.b;
            all_b2(1,j)=fitresult.d;
            
            if  lPlotFit
                subplot(1,length(tr),j)
                plot(x,y,'o')
                hold all
                plot(fitresult)
                xlabel('time(s)')
                ylabel('I/I0')
            end
            
            data(1,ipp*3-2)={d.fileName};
            data(1,ipp*3-1)={['mux=' num2str(d.iMux)]};
            data(1,ipp*3-0)={['time=' num2str(d.time)]};
            data(2,ipp*3-2)={'t(s)'};
            data(2,ipp*3-1)={'Iexp(A)'};
            data(2,ipp*3)={'Ifit(A)'};
            nd=length(x);
            data(3:3+nd-1,ipp*3-2)=num2cell(x);
            data(3:3+nd-1,ipp*3-1)=num2cell(y);
            data(3:3+nd-1,ipp*3)=num2cell(fitresult.a*exp(-fitresult.b*x)+fitresult.c*exp(-fitresult.d*x)+fitresult.e);
        end
        
    else
        all_b1=[];
        all_b2=[];
    end
    all_b1=1./all_b1;
    dataSMU(i).mean_b1=mean(all_b1);
    dataSMU(i).std_b1=std(all_b1);
    all_b2=1./all_b2;
    dataSMU(i).mean_b2=mean(all_b2);
    dataSMU(i).std_b2=std(all_b2);   
    
end

if lWriteFitToFile
    xlswrite('transients',data)
end

ii=([dataSMU.iMux]==IMUX);
ta=[dataSMU(ii).time];
bm1=[dataSMU(ii).mean_b1];
bs1=[dataSMU(ii).std_b1];
bm2=[dataSMU(ii).mean_b2];
bs2=[dataSMU(ii).std_b2];

iincl=find([dataSMU.iMux]==IMUX);
n=length(ta);
data={};
data(1,1)={'fileName'};
for i=1:n
    ii=iincl(i); 
    data(i+1,1)={dataSMU(ii).fileName};
end
data(1,2)={'iMux'};
for i=1:n   
    ii=iincl(i); 
    data(i+1,2)={dataSMU(ii).iMux};
end
data(1,3)={'refDate'};
for i=1:n   
    ii=iincl(i);
    data(i+1,3)=dataSMU(ii).refDate;
end

data(1,4)={'time(hours)'};
data(2:n+1,4)=num2cell(ta);
data(1,5)={'tau1(s)'};
data(2:n+1,5)=num2cell(bm1);
data(1,6)={'std(tau1)(s)'};
data(2:n+1,6)=num2cell(bs1);
data(1,7)={'tau2(s)'};
data(2:n+1,7)=num2cell(bm2);
data(1,8)={'std(tau2)(s)'};
data(2:n+1,8)=num2cell(bs2);
xlswrite(['iMux_' num2str(IMUX) '_exp2fit'],data)


