
%% load and save data
x1 = loadspk('kesey22471_12DIV_nodrug_01-2013-05-20-21-03-54.spk');
x2 = loadspk('kesey22471_13DIV_100uM-CTZ_01-2013-05-21-15-28-53.spk');
x3 = loadspk('kesey22471_14DIV_wash4_01.spk');

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

badchannels = [1 8 33 57 64];
time = time(~ismember(channel,badchannels));
unit = unit(~ismember(channel,badchannels));
channel = channel(~ismember(channel,badchannels));

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

save kesey22471_12-14DIV time channel unit timestamps;


%% plot asdr

% load kesey22471_12-14DIV.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),'100uM CTZ','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(-12,y_lim(2),['bin size = ' num2str(dt) 'sec'],'HorizontalAlignment','left','VerticalAlignment','bottom','Color',[0 0 0]);
xlim([-12 60]);
set(gca,'XTick',[-12:2:60],'TickDir','out','TickLength',[0.0025 0.025]);
box off;

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

%% save files
cultureID = 'kesey22471_12-14DIV';
dirID = 'C:\Users\USER\Documents\mingfaifong\2013-05-20_kesey22471_12-14DIV_MEA_CTZ\';
export_fig([dirID cultureID '_ASDR-bin' num2str(dt) 'sec'],'-png','-transparent');
saveas(gca,[dirID cultureID '_ASDR-bin' num2str(dt) 'sec'],'fig');