load('C:\Users\USER\Documents\mingfaifong\buster359\buster359_FullRecNoWaveformNoClean.mat');
fid_prefix = 'buster359';

[e1 e2] = get_events(fid_prefix);
%time = spktimes(logical(cleanspk));
treatment = 'CNQX';
dt = 0.001;

title1 = [fid_prefix ' before ' treatment];
title2 = [fid_prefix ' after ' treatment];

%pre.time = sortrows(time(time<e1(2))-e1(1));
pre.time = sortrows(time(time<e1(2)));
%pre.time = time;
pre.ISI = diff(pre.time);
pre.bins = 0.5*dt:dt:max(pre.ISI(pre.ISI<60));
pre.hist = hist(pre.ISI,pre.bins);

%CNQX.time = sortrows(time(time>=e1(3))-e1(1));
CNQX.time = sortrows(time(time>=e1(2)));
CNQX.ISI = diff(CNQX.time);
CNQX.bins = 0.5*dt:dt:max(CNQX.ISI(CNQX.ISI<60));
CNQX.hist = hist(CNQX.ISI,CNQX.bins);

figure('units','normalized','outerposition',[0 0 2 1])
subplot(2,3,1);
bar(pre.bins,pre.hist);
set(gca,'yscale','log');
set(gca,'TickDir','out');
grid on; xlim([-0.1 10]);
xlabel('Interspike Interval (seconds)'); ylabel('Counts');
title([title1,10,'ISI Distribution']);

subplot(2,3,2);
bar(pre.bins,pre.hist.*pre.bins);
grid on; xlim([-0.1 10]);
 %ylim([0 100]);
set(gca,'TickDir','out');
xlabel('Interspike Interval (seconds)'); ylabel('Counts * ISI');
title([title1,10,'ISI Distribution',10,'(normalized to ISI length)']);

subplot(2,3,3);
bar(pre.bins,pre.hist.*pre.bins);
grid on; xlim([-0.001 0.1]);
ylim([0 75]);
set(gca,'TickDir','out');
xlabel('Interspike Interval (seconds)'); ylabel('Counts * ISI');
title([title1,10,'ISI Distribution',10,'(normalized to ISI length)']);


subplot(2,3,4);
bar(CNQX.bins,CNQX.hist);
set(gca,'yscale','log');
set(gca,'TickDir','out');
grid on;
xlim([-0.1 10]);
xlabel('Interspike Interval (seconds)'); ylabel('Counts');
title([title2,10,'ISI Distribution']);

subplot(2,3,5);
bar(CNQX.bins,CNQX.bins.*CNQX.hist);
grid on;
xlim([-0.1 10]);
set(gca,'TickDir','out');
xlabel('Interspike Interval (seconds)'); ylabel('Counts * ISI');
title([title2,10,'ISI Distribution',10,'(normalized to ISI length)']);

subplot(2,3,6);
bar(CNQX.bins,CNQX.bins.*CNQX.hist);
grid on; xlim([-0.001 0.1]);
ylim([0 200]);
set(gca,'TickDir','out');
xlabel('Interspike Interval (seconds)'); ylabel('Counts * ISI');
title([title2,10,'ISI Distribution (zoom)',10,'(normalized to ISI length)']);


%% save stuff
    print('-depsc',[fid_prefix '_ISI_distributions_bin1ms.eps']);
    screen2png(gcf,[fid_prefix '_ISI_distributions_bin1ms.png'],'C:\Users\USER\Documents\mingfaifong\');
    saveas(gcf,[fid_prefix '_ISI_distributions_bin1ms.fig'],'fig');