x1 = loadspk('xinhai22471_11DIV_nodrug_02.spk');
x2 = loadspk('xinhai22471_12DIV_40uM-CNQX-ChR2-stim-CL_01.spk');
% x3 = loadspk('xinhai22471_13DIV_wash4.spk');

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

timestamps = [0 max(x2.time)];

save xinhai22471_11-13DIV time channel unit timestamps;


%%

load xinhai22471_11-13DIV.mat

dt = 3600;
bin = [ceil(min(time)/dt)*dt:dt:floor(max(time)/dt)*dt];
asdr = histc(time,bin);

hold off;
stairs(bin/3600,asdr/dt,'-k','LineWidth',2);
hold on;
y_lim = ylim;
timestamp_label = (timestamps+120)/3600;
plot([timestamps(1) timestamps(1)]/3600,[0 y_lim(2)],'--r');
text(timestamp_label(1),y_lim(2),'CNQX (stimulation begins 5min later)','HorizontalAlignment','left','VerticalAlignment','top','Color',[1 0 0]);
plot([timestamps(2) timestamps(2)]/3600,[0 y_lim(2)],'--r');
text(timestamp_label(2),y_lim(2),'wash','HorizontalAlignment','left','VerticalAlignment','top','Color',[1 0 0]);

text(bin(1)/3600,y_lim(2),['bin size = ' num2str(dt) 'sec'],'HorizontalAlignment','left','VerticalAlignment','bottom','Color',[0 0 0]);

plot([timestamps(1)+300 timestamps(2)]/3600,[25.8860 25.8860],'-g');

xlim([bin(1) bin(end)]/3600);
set(gca,'XTick',[-20:2:28],'TickDir','out','TickLength',[0.0025 0.025]);
box off;

ylabel('Array-wide firing rate (Hz)','FontSize',14);
xlabel('Time (hours)','FontSize',14);