clear all;
% fid_label = 'trouble22467';
% load('C:\Users\USER\Documents\mingfaifong\2013-01-21_trouble22467_12-14DIV_MEA_CNQX\trouble22467_12-14DIV.mat');
% fid_label = 'usain22493';
load('C:\Users\USER\Documents\mingfaifong\2013-01-24_usain22493_12-14DIV_MEA_CNQX\usain22493_12-14DIV.mat');
% fid_label = 'wingnut22468';
% load('C:\Users\USER\Documents\mingfaifong\2013-02-03_wingnut22468_10-12DIV_MEA_CNQX\wingnut22468_10-12DIV.mat');
% fid_label = 'xinhai22493';
% load('C:\Users\USER\Documents\mingfaifong\2013-02-08_xinhai22493_11-13DIV_MEA_CNQX\xinhai22493_11-13DIV.mat');
% fid_label = 'atlas22493';
% load('C:\Users\USER\Documents\mingfaifong\2013-03-01_atlas22493_10-12DIV_MEA_CNQX\atlas22493_10-12DIV.mat');
% fid_label = 'dojo22493';
% load('C:\Users\USER\Documents\mingfaifong\2013-03-19_dojo22493_11-13DIV_MEA_CNQX\dojo22493_11-13DIV.mat');


%%
time = sortrows(time);
time = time(time>-3*3600 & time<24*3600);

dt = 0.01;
f_smooth = 101;
bin = [-3*3600:dt:24*3600];
ASDR = histc(time,bin);
smoothASDR = smooth(ASDR,f_smooth);





%% 
time = sortrows(time);
time = time(time>-3*3600 & time<24*3600);
ISI = diff(time);

dt = 0.01;
f_smooth = 101;
bin = [-3*3600:dt:24*3600];
ASDR = histc(time,bin);
smoothASDR = smooth(ASDR,f_smooth);
diffASDR = diff(ASDR);

hold off;
% stairs(bin,ASDR,'-k');
hold on;
% stairs(bin,smoothASDR,'-r','LineWidth',2);

% xlim([-3165 -3105]);
% xlim([0 3600]+3600*5);
% ylim([-0.01 20]);

x = find(smoothASDR<0.01);
% plot(bin(x),ones(size(bin(x))),'.','MarkerSize',20);

y = x(diff(x)>1/dt);
% plot(bin(y),2*ones(size(bin(y))),'*g');

z = [];
for i = 1:length(y)
    if max(diffASDR(y(i):y(5/dt)))>10/dt
        keyboard;
        z = [z y(i)];
    end
end

%%
x = diff(ASDR);
ind = find(x>1/dt);

smoothISI = diff(smoothASDR);
figure;
hist(smoothISI,[-7:0.0001:7]);
xlim([-.001 .001]);

%% logscale frequency of ASDR values

time = sortrows(time);
time = time(time>-3*3600 & time<24*3600);
ISI = diff(time);

dt = 0.01;
binlin = [-3*3600:dt:24*3600];
binlog = horzcat(0,10.^[-5:1:20]);

ASDR = histc(time,binlin);
histASDR = histc(ASDR,binlog);
bar(1:length(binlog),histASDR);
% set(gca,'xscale','log');
xlim([0 length(binlog)+1]);
set(gca,'XTick',[1:length(binlog)],'XTickLabel',binlog);

%% ChR2 stimulated cultures
% fid_label = 'trouble22468';
% load('C:\Users\USER\Documents\mingfaifong\2013-01-21_trouble22468_12-14DIV_MEA_CNQX-ChR2-stim-CL\trouble22468_12-14DIV.mat');
% fid_label = 'usain22471';
% load('C:\Users\USER\Documents\mingfaifong\2013-01-24_usain22471_12-14DIV_MEA_CNQX-ChR2-stim-CL\usain22471_12-14DIV.mat');
% fid_label = 'wingnut21332';
% load('C:\Users\USER\Documents\mingfaifong\2013-02-03_wingnut21332_10-13DIV_MEA_CNQX-ChR2-stim-CL\wingnut21332_10-12DIV.mat');
% fid_label = 'xinhai22471';
% load('C:\Users\USER\Documents\mingfaifong\2013-02-08_xinhai22471_11-13DIV_MEA_CNQX-ChR2-stim-CL\xinhai22471_11-13DIV.mat');
% fid_label = 'zarathustra21332';
% load('C:\Users\USER\Documents\mingfaifong\2013-02-19_zarathustra21332_12-14DIV_MEA_CNQX-ChR2-stim-CL\zarathustra21332_12-14DIV.mat');
% fid_label = 'barbie22468';
% load('C:\Users\USER\Documents\mingfaifong\2013-03-08_barbie22468_11-13DIV_MEA_CNQX-ChR2-stim-2x-CL\barbie22468_11-13DIV.mat');
% fid_label = 'dojo22471';
% load('C:\Users\USER\Documents\mingfaifong\2013-03-19_dojo22471_11-13DIV_MEA_CNQX-ChR2-stim-2x-CL\dojo22471_11-13DIV.mat');
% fid_label = 'jujuI5388';
% load('C:\Users\USER\Documents\mingfaifong\2012-10-28_jujuI5388_11-13DIV_MEA_CNQX-ChR2-stim-CL\jujuI5388_11-13DIV.mat');
