% Jon Newman
% Look at control performance

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

% Pre-drug
fid = '2013_12_21_Smiley-24729_01';
[sp{k} st{k}] = smoosh(fid);
k = k+1;
ko = 1;

fid = '2013_12_21_Smiley-24729_02';
[sp{k} st{k}] = smoosh(fid);
k = k+1;
% 
fid = '2013_12_21_Smiley-24729_03';
[sp{k} st{k}] = smoosh(fid);
bng = loadoc2([fid '.oc2']);
ko = k;
k = k+1;
% 
fid = '2013_12_21_Smiley-24729_04';
[sp{k} st{k}] = smoosh(fid);
bng1 = loadoc2([fid '.oc2.gz']);
bng.time = [bng.time, bng1.time + 24*3600*(st{k} - st{ko})];
bng.out2 = [bng.out2 , bng1.out2 ];
k = k+1;

fid = '2013_12_21_Smiley-24729_05';
[sp{k} st{k}] = smoosh(fid);
bng1 = loadoc2([fid '.oc2.gz']);
bng.time = [bng.time, bng1.time + 24*3600*(st{k} - st{ko})];
bng.out2 = [bng.out2, bng1.out2];
k = k+1;

fid = '2013_12_21_Smiley-24729_06';
[sp{k} st{k}] = smoosh(fid);
bng1 = loadoc2([fid '.oc2.gz']);
bng.time = [bng.time, bng1.time + 24*3600*(st{k} - st{ko})];
bng.out2 = [bng.out2 , bng1.out2 ];
k = k+1;

fid = '2013_12_21_Smiley-24729_wash1-2_07';
[sp{k} st{k}] = smoosh(fid);
k = k+1;

fid = '2013_12_21_Smiley-24729_wash3-4_08';
[sp{k} st{k}] = smoosh(fid);
k = k+1;


fid = '2013_12_21_Smiley-24729_wash5-6_09';
[sp{k} st{k}] = smoosh(fid);
k = k+1;
% fid = '2013_12_20_Smiley-24728_05';
% [sp{k} st{k}] = smoosh(fid);
% bng = loadoc2([fid '.oc2.gz']);

% k = k+1;



%%
close all
figure
dt_asdr = 1; % sec
xlimit = [-1 10];

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([1/6 4],[max(bng.targ) max(bng.targ)],'r-')
ylim([0 200])
xlim(xlimit)
xlabel('Time (hrs)')
ylabel('FR (Hz/unit)')
grid on

dt_asdr = 300; % 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([1/6 4],[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')

%%
export_fig('2013_12_21_Smiley-24729_summary_01','-pdf','-transparent',gcf)





