%% load files
x1 = loadspk('xeko20629_19DIV_pre-drug_01.spk');
x2 = loadspk('xeko20629_20DIV_20uM-CNQX_01.spk');

%% combine
time = [x1.time - max(x1.time); x2.time];
channel = [x1.channel; x2.channel];

%% raster, asdr

f1 = figure(1);
f2 = figure(2);

time = time(channel>0);
channel = channel(channel>0);

x_low = -6;
x_high = 24.5;

% targetFR = 4.6822;
snipWidth = 20/60;
snipCent = [-3 0 6 12 18 24];
avgFR_3hr = 12.8136;

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

figure(2);
subplot(4,6,1); plot(time/3600,channel+.15*randn(size(channel)),'.k','MarkerSize',3); xlim([snipCent(1)-snipWidth snipCent(1)+snipWidth]);
title('3hr before CNQX','FontSize',14);
ylabel('Channel','FontSize',14);
subplot(4,6,2); plot(time/3600,channel+.15*randn(size(channel)),'.k','MarkerSize',3); xlim([snipCent(2)-snipWidth snipCent(2)+snipWidth]);
title('10min before and after CNQX','FontSize',14);
subplot(4,6,3); plot(time/3600,channel+.15*randn(size(channel)),'.k','MarkerSize',3); xlim([snipCent(3)-snipWidth snipCent(3)+snipWidth]);
title('6hr after CNQX','FontSize',14);
subplot(4,6,4); plot(time/3600,channel+.15*randn(size(channel)),'.k','MarkerSize',3); xlim([snipCent(4)-snipWidth snipCent(4)+snipWidth]);
title('12hr after CNQX','FontSize',14);
subplot(4,6,5); plot(time/3600,channel+.15*randn(size(channel)),'.k','MarkerSize',3); xlim([snipCent(5)-snipWidth snipCent(5)+snipWidth]);
title('18hr after CNQX','FontSize',14);
subplot(4,6,6); plot(time/3600,channel+.15*randn(size(channel)),'.k','MarkerSize',3); xlim([snipCent(6)-snipWidth snipCent(6)+snipWidth]);
title('24hr after CNQX','FontSize',14);

dt = 1;
bin = floor(min(time)/3600)*3600:dt:ceil(max(time)/3600)*3600;
%bin = min(time):dt:max(time);
asdr = histc(time,bin);
figure(1); subplot(4,1,2)
plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',0.25);
hold on;
plot([0 0],[0 max(asdr/dt)],'--','Color',[0.6 0.6 0.6]);
text(-0.01,max(asdr/dt),'added 20uM CNQX','Color',[0.6 0.6 0.6],'VerticalAlignment','top','HorizontalAlignment','right')
plot([5/60 24+5/60],[avgFR_3hr avgFR_3hr],':r','LineWidth',1);
hold off;
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/channel)',10,'[' num2str(dt) '-sec bins]'],'FontSize',14);

figure(2);
subplot(4,6,7); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',0.25); xlim([snipCent(1)-snipWidth snipCent(1)+snipWidth]);
ylabel(['Array-Wide Firing',10,'Rate (Hz/channel)',10,'[' num2str(dt) '-sec bins]'],'FontSize',14);
subplot(4,6,8); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',0.25); xlim([snipCent(2)-snipWidth snipCent(2)+snipWidth]); hold on; plot([0 0],[0 max(asdr/dt)],'--','Color',[0.6 0.6 0.6]); hold off;
subplot(4,6,9); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',0.25); xlim([snipCent(3)-snipWidth snipCent(3)+snipWidth]);
subplot(4,6,10); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',0.25); xlim([snipCent(4)-snipWidth snipCent(4)+snipWidth]);
subplot(4,6,11); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',0.25); xlim([snipCent(5)-snipWidth snipCent(5)+snipWidth]);
subplot(4,6,12); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',0.25); xlim([snipCent(6)-snipWidth snipCent(6)+snipWidth]);


dt = 60;
bin = floor(min(time)/3600)*3600:dt:ceil(max(time)/3600)*3600;
asdr = histc(time,bin);
figure(1); subplot(4,1,3)
plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',1);
hold on;
plot([0 0],[0 max(asdr/dt)],'--','Color',[0.6 0.6 0.6]);
plot([5/60 24+5/60],[avgFR_3hr avgFR_3hr],':r','LineWidth',1);
text(5/60,avgFR_3hr,'hypothetical target firing rate','Color','r','VerticalAlignment','bottom','HorizontalAlignment','left')
hold off;
%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]);
ylabel(['Array-Wide Firing',10,'Rate (Hz/channel)',10,'[' num2str(dt) '-sec bins]'],'FontSize',14);

figure(2);
subplot(4,6,13); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',1.5); xlim([snipCent(1)-snipWidth snipCent(1)+snipWidth]);
ylabel(['Array-Wide Firing',10,'Rate (Hz/channel)',10,'[' num2str(dt) '-sec bins]'],'FontSize',14);
subplot(4,6,14); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',1.5); xlim([snipCent(2)-snipWidth snipCent(2)+snipWidth]); hold on; plot([0 0],[0 max(asdr/dt)],'--','Color',[0.6 0.6 0.6]); hold off;
subplot(4,6,15); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',1.5); xlim([snipCent(3)-snipWidth snipCent(3)+snipWidth]);
subplot(4,6,16); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',1.5); xlim([snipCent(4)-snipWidth snipCent(4)+snipWidth]);
subplot(4,6,17); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',1.5); xlim([snipCent(5)-snipWidth snipCent(5)+snipWidth]);
subplot(4,6,18); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',1.5); xlim([snipCent(6)-snipWidth snipCent(6)+snipWidth]);


dt = 3600;
bin = floor(min(time)/3600)*3600:dt:ceil(max(time)/3600)*3600;
asdr = histc(time,bin);
figure(1); subplot(4,1,4)
plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',3);
hold on;
plot([0 0],[0 targetFR*1.5],'--','Color',[0.6 0.6 0.6]);
plot([5/60 24+5/60],[avgFR_3hr avgFR_3hr],':r','LineWidth',1);
hold off;
%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]);
ylabel(['Array-Wide Firing',10,'Rate (Hz/channel)',10,'[' num2str(dt) '-sec bins]'],'FontSize',14);
xlabel('Time (hours)','FontSize',14);

figure(2);
subplot(4,6,19); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',3); xlim([snipCent(1)-snipWidth snipCent(1)+snipWidth]);
ylabel(['Array-Wide Firing',10,'Rate (Hz/channel)',10,'[' num2str(dt) '-sec bins]'],'FontSize',14);
xlabel('Time (hours)','FontSize',14);
subplot(4,6,20); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',3); xlim([snipCent(2)-snipWidth snipCent(2)+snipWidth]); hold on; plot([0 0],[0 max(asdr/dt)],'--','Color',[0.6 0.6 0.6]); hold off;
xlabel('Time (hours)','FontSize',14);
subplot(4,6,21); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',3); xlim([snipCent(3)-snipWidth snipCent(3)+snipWidth]);
xlabel('Time (hours)','FontSize',14);
subplot(4,6,22); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',3); xlim([snipCent(4)-snipWidth snipCent(4)+snipWidth]);
xlabel('Time (hours)','FontSize',14);
subplot(4,6,23); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',3); xlim([snipCent(5)-snipWidth snipCent(5)+snipWidth]);
xlabel('Time (hours)','FontSize',14);
subplot(4,6,24); plot((bin+dt)/3600,asdr/dt,'-k','LineWidth',3); xlim([snipCent(6)-snipWidth snipCent(6)+snipWidth]);
xlabel('Time (hours)','FontSize',14);


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

for i = 1:24
    figure(2); subplot(4,6,i)
    set(gca,'TickDir','out','TickLength',[0.002 0.025]);
    if i<=6
        ylim([0 64]);
    elseif i<=12
        ylim([0 300]);
    elseif i<=18
        ylim([0 18]);
    else
        ylim([0 15]);
    end
end
