% Jon Newman

%% file id
fids = {
    'J:/newmonster/2012_07_02_Xeko-1-minute-clamps/2012_07_02_Xeko-20709_one-min-clamp_07';
    'J:/newmonster/2012_10_11-Herb-I5387-1-minute-clamps/2012_10_11_Herb-I5387_one-min-clamp_02';
        'J:/newmonster/2012_10_22_Herb-I5386-1-minute-clamps/2012_10_22_Herb-I5386_one-min-clamp_10';
    'Z:/jon/2013_02_26_Yam-22565_1-minute-clamp/2013-02-26_yam_22565_one-minute-clamp_06';
    'Z:/jon/2012_11_21_Mandelbrot-21332-1-minute-clamps/2012_11_21_Mandelbrot_21332_one-min-clamp_12';
    };


col = [ [0 100 255];
    [255 205 41]]/255;

col2 = [
    [220 000 000];
    [255 102 000];
    [255 205 041];
    [000 170 000];
    [000 170 212];
    [000 100 255];
    [085 000 212];
    [102 102 102]
    ]/255;
sym1 = {'o','o','o','o','o','o'};

% close all
% figure('units','centimeter','position',[40 5 15 20]);
%
% % Average FR
% ax1 = axes('units','centimeter','position',[5 10 2.5 2.5]);
% hold on;
% set(gca,'TickDir','out','FontName','Arial','FontSize',10,'TickLength',[0.025 0.025]);
% plot([0 10],[0 10],'k:')
% axis([0 10 0 10])
% ylabel('Acheived Rate')
%
% % Average residual
% ax2 = axes('units','centimeter','position',[5 7 2.5 2.5]);
% set(gca,'YAxisLocation','left','TickDir','out','FontName','Arial','FontSize',10,'TickLength',[0.025 0.025])
% ylabel('Residual')
%
% % Average CTL
% ax3 = axes('units','centimeter','position',[5 4 2.5 2.5]);
% hold on;
% set(gca,'TickDir','out','FontName','Arial','FontSize',10,'TickLength',[0.025 0.025])
% axis([0 10 0 1])
% xlabel('Target Rate')
% ylabel('u_1 and u_2')

RT = nan(length(fids),13); % rise-time
TAU = nan(length(fids),13); % time-constant
ST = nan(length(fids),13); % settling time
% ctl_2 = nan(length(fids),11);

dfr_r = 0.1;
dfr_s = 0.5;
sw = 1;
st_time = 5; % second
loop_dt = 0.01;

for f = 1:length(fids)
    
    fid = fids{f};
    load(fid);
    
    % Determine rise-time measures
    for i = 1:length(avg_perf.steps)
        
        targ = avg_perf.steps(i);
        
        b = ts.t{i} > 0;
        t = ts.t{i}(b);
        tru_fr = ts.fr{i}(b);
        
        
        
        b = ts.t{i} > 0;
        rt_i = find(abs(tru_fr - targ) < dfr_r & t > 0.1,1,'first');
        [~, mt_i] = max(ts.fr{i});
        
        if ~isempty(rt_i)
            RT(f,i) = loop_dt*rt_i;
        end
        
        sm_fr = smooth(ts.fr{i}(b),sw);
        if size(sm_fr,1) ~= size(t,1)
            sm_fr = sm_fr';
        end
        
        
        ind_in_bound = find(abs(sm_fr - targ) < dfr_s); % & t > 2);
        ind_out_bound = find(abs(sm_fr - targ) > dfr_s);
        
        if  isempty(ind_in_bound)
            continue;
        elseif size(ind_in_bound,2) > 1
            ind_in_bound = ind_in_bound';
            ind_out_bound = ind_out_bound';
        end
        
        ind_enter_bound = [ind_in_bound(1);  ind_in_bound(find(diff(ind_in_bound) > 1)+1)];
        ind_exit_bound = [ind_out_bound(1); ind_out_bound(find(diff(ind_out_bound) > 1)+1)];
        
        
        if ind_exit_bound(1) < ind_enter_bound(1)
            ind_exit_bound(1) = [];
        end
        if length(ind_exit_bound) ~= length(ind_enter_bound)
            ind_exit_bound = [ind_exit_bound; length(sm_fr)];
        end
            
%         close all
%         plot(sm_fr)
%         hold on
%         plot(ind_enter_bound,sm_fr(ind_enter_bound),'g*')
%         plot(ind_exit_bound ,sm_fr(ind_exit_bound ),'r*')
%         
        
        
        
                t_in_bound = ind_exit_bound-ind_enter_bound;
                ind_settled = ind_enter_bound(find(t_in_bound > st_time/loop_dt,1,'first'));
        
        %         if length(ind_enter_bound) == 1
        %             ind_settled = 1;
        %         else
        %             ind_settled = find(diff(ind_enter_bound) > st_time/loop_dt);
        %         end
        %
        %         if isempty(ind_settled) && ind_enter_bound(end) < 60 - st_time
        %             ind_enter_bound = length(ind_enter_bound);
        %         end
        
        
        
        
                if ~isempty(ind_settled)
        
%                     close all
%                     plot(sm_fr)
%                     hold on
%                     plot(ind_settled,sm_fr(ind_settled),'r*')
                    ST(f,i) = loop_dt*ind_settled;
                end
        
    end
    
end

figure


hold on
plot(0:12,ST')
plot(0:12,mean(ST),'k-','Linewidth',2)






