% Jon Newman
% Look at control performance

k = 1;
st = {};
sp = {};

% Pre-drug
fid = '2013_12_22_Smiley-24729_01';
[sp{k}, st{k}] = smoosh(fid);
bng = loadoc2([fid '.oc2.gz']);
k = k+1;
ko = 1;
% bng.time = (1:length(bng.time))/bng.meta.loop_rate_hz;


%%
close all
figure
dt_asdr = 1; % sec
xlimit = [0 24];

a(1) = subplot(411)
hold on
for i = 1:length(st)
    
    
    bin = 0:dt_asdr:max(sp{i}.time);
    asdr = hist(sp{i}.time,bin)/max(sp{i}.unit);
    bin = bin + 24*3600*(st{i} - st{ko});
    if i == 3
        da = bin(1);
    end
    stairs(bin/3600,asdr/dt_asdr,'k-');
    
    
end

plot([3 15],[max(bng.targ) max(bng.targ)],'r-')
ylim([0 20])
xlim(xlimit)
xlabel('Time (hrs)')
ylabel('FR (Hz/unit)')
grid on

dt_asdr = 60; % sec
a(2) = subplot(412)
hold on
for i = 1:length(st)
    
    
    bin = 0:dt_asdr:max(sp{i}.time);
    asdr = hist(sp{i}.time,bin)/max(sp{i}.unit);
    bin = bin + 24*3600*(st{i} - st{ko});
    if i == 3
        da = bin(1);
    end
    stairs(bin/3600,asdr/dt_asdr,'k-');
    
    
end

plot([3 15],[max(bng.targ) max(bng.targ)],'r-')
ylim([0 3])
xlim(xlimit)
xlabel('Time (hrs)')
ylabel('FR (Hz/unit)')
grid on

a(3) = subplot(413)
plot(bng.time/3600,bng.out2,'k');
xlabel('Time (hrs)')
ylabel('Control singal (%)')
ylim([-0.1 1.1])
xlim(xlimit)

a(4) = subplot(414)
bin = 0:1:max(bng.time);
transitions = [false diff(bng.out2) > 0];
plot(bng.time(transitions)/3600, [0 diff(bng.time(transitions))],'k')
xlabel('Time (hrs)')
ylabel('on/on transistion time (sec)')
% ylim([0 1.2])
xlim(xlimit)


linkaxes(a,'x')

%%
saveas(gca,'2013_12_29_Smiley-24729_summary_01')
export_fig('2013_12_29_Smiley-24729_summary_01','-pdf','-transparent',gcf)





