%% load files
x1 = loadspk('sushi19309_12DIV_pre-drug_01.spk');
x2 = loadspk('sushi19309_13DIV_40uM-CNQX_01.spk');
x3 = loadspk('sushi19309_14DIV_postpatch_wash3.spk');
x4 = loadspk('sushi19309_14DIV_postpatch_wash4.spk');

%% combine
time = [x1.time - max(x1.time); ...
    x2.time; ...
    x3.time + max(x2.time) + 3*3600; ...
    x4.time + max(x3.time) + max(x2.time) + 3*3600]; ...
unit = [x1.unit; x2.unit; x3.unit; x4.unit];

%% raster, asdr

time = time(unit>0);
unit = unit(unit>0);

x_low = -12;
x_high = 48;

subplot(4,1,1)
plot(time/3600,unit+.15*randn(size(unit)),'.k','MarkerSize',3);
%axis([min(time)/3600 max(time)/3600 0 max(unit)]);
axis([x_low x_high 0.5 max(unit)+0.5]);
set(gca,'XTick',[x_low:1:x_high]);
hold on;
plot(0,max(unit),'*r');
plot(max(x2.time)/3600,max(unit),'*r');
plot(max(x2.time)/3600+3,max(unit),'k*');
plot((max(x2.time) + max(x3.time))/3600+3,max(unit),'k*');
hold off
ylabel('Unit','FontSize',14);

dt = 1;
bin = min(time):dt:max(time);
asdr = histc(time,bin);
subplot(4,1,2)
plot((bin+dt)/3600,asdr/dt,'-k');
%axis([min(time)/3600 max(time)/3600 0 ceil(10*max(asdr))/10]);
axis([x_low x_high 0 ceil(10*max(asdr)/dt)/10]);
set(gca,'XTick',[x_low:1:x_high]);
hold on;
plot(0,ceil(10*max(asdr))/10,'*r');
plot(max(x2.time)/3600,ceil(10*max(asdr/dt))/10,'*r');
plot(max(x2.time)/3600+3,ceil(10*max(asdr/dt))/10,'k*');
plot((max(x2.time) + max(x3.time))/3600+3,ceil(10*max(asdr/dt))/10,'k*');
hold off
ylabel(['Array-Wide Firing',10,'Rate (Hz/unit)',10,'[' num2str(dt) '-sec bins]'],'FontSize',14);


dt = 60;
bin = min(time):dt:max(time);
asdr = histc(time,bin);
subplot(4,1,3)
plot((bin+dt)/3600,asdr/dt,'-k');
%axis([min(time)/3600 max(time)/3600 0 ceil(10*max(asdr/dt))/10/10]);
axis([x_low x_high 0 ceil(10*max(asdr)/dt)/10]);
set(gca,'XTick',[x_low:1:x_high]);
hold on;
plot(0,ceil(10*max(asdr/dt))/10,'*r');
plot(max(x2.time)/3600,ceil(10*max(asdr/dt))/10,'*r');
plot(max(x2.time)/3600+3,ceil(10*max(asdr/dt))/10,'k*');
plot((max(x2.time) + max(x3.time))/3600+3,ceil(10*max(asdr/dt))/10,'k*');
hold off
ylabel(['Array-Wide Firing',10,'Rate (Hz/unit)',10,'[' num2str(dt) '-sec bins]'],'FontSize',14);

dt = 3600;
bin = min(time):dt:max(time);
asdr = histc(time,bin);
subplot(4,1,4)
plot((bin+dt)/3600,asdr/dt,'-k');
%axis([min(time)/3600 max(time)/3600 0 ceil(10*max(asdr/dt))/10/10]);
axis([x_low x_high 0 ceil(10*max(asdr)/dt)/10]);
set(gca,'XTick',[x_low:1:x_high]);
hold on;
plot(0,ceil(10*max(asdr/dt))/10,'*r');
plot(max(x2.time)/3600,ceil(10*max(asdr/dt))/10,'*r');
plot(max(x2.time)/3600+3,ceil(10*max(asdr/dt))/10,'k*');
plot((max(x2.time) + max(x3.time))/3600+3,ceil(10*max(asdr/dt))/10,'k*');
hold off
ylabel(['Array-Wide Firing',10,'Rate (Hz/unit)',10,'[' num2str(dt) '-sec bins]'],'FontSize',14);
xlabel('Time (hours)','FontSize',14);

for i = 1:4
    subplot(4,1,i)
    set(gca,'TickDir','out','TickLength',[0.002 0.025]);
end
