fid = 'herbI5374_12DIV_pre-drug_03';
dirID = 'C:\Users\Lab\Documents\2012-10-03_herbI5374_12-14DIV_MEA_CNQX-ChR2-stim-CL\';

t_end = loadspk([dirID fid '.spk'],'last');
loadspk([dirID fid '.spk'],[0 t_end],[]);

time = ans.time;
channel = ans.channel;
unit = ans.unit;
%waveform = ans.waveform;
meta = ans.meta;

%save([dirID fid '.mat'],'time','channel','unit','waveform','meta');
save([dirID fid '.mat'],'time','channel','unit','meta');

%% concatenate

dirID = 'C:\Users\Lab\Documents\2012-10-03_herbI5374_12-14DIV_MEA_CNQX-ChR2-stim-CL\';
tmp = load([dirID 'herbI5374_12DIV_pre-drug_02']);
time = tmp.time - max(tmp.time);
channel = tmp.channel;
unit = tmp.unit;
tmp = load([dirID 'herbI5374_12DIV_pre-drug_01']);
time = [tmp.time - max(tmp.time)+min(time); time];
channel = [tmp.channel; channel];
unit = [tmp.unit; unit];

tmp = load([dirID 'herbI5374_13DIV_40uM-CNQX_ChR2-stim-CL_01']);
t_addCNQX = 0;
t_startBB = 10*60;
time = [time; tmp.time];
channel = [channel; tmp.channel];
unit = [unit; tmp.unit];

tmp = load([dirID 'herbI5374_13DIV_40uM-CNQX_ChR2-stim-CL_raw_02']);
% t_endBB = max(tmp.time)+max(time)+5*3600;
time = [time; tmp.time+max(time)];
channel = [channel; tmp.channel];
unit = [unit; tmp.unit];

tmp = load([dirID 'herbI5374_13DIV_wash1and2_02']);
t_wash12 = max(time);
time = [time; tmp.time+max(time)];
channel = [channel; tmp.channel];
unit = [unit; tmp.unit];

tmp = load([dirID 'herbI5374_13DIV_wash3and4_01']);
tmp.wash34 = max(time);
time = [time; tmp.time+max(time)];
channel = [channel; tmp.channel];
unit = [unit; tmp.unit];

tmp = load([dirID 'herbI5374_13DIV_wash5and6_01']);
tmp.wash56 = max(time);
time = [time; tmp.time+max(time)];
channel = [channel; tmp.channel];
unit = [unit; tmp.unit];

tmp = load([dirID 'herbI5374_13DIV_wash7and8_01']);
tmp.wash78 = max(time);
time = [time; tmp.time+max(time)];
channel = [channel; tmp.channel];
unit = [unit; tmp.unit];

tmp = load([dirID 'herbI5374_13DIV_wash7and8_02']);
time = [time; tmp.time+max(time)];
channel = [channel; tmp.channel];
unit = [unit; tmp.unit];

tmp = load([dirID 'herbI5374_13DIV_wash7and8_03']);
time = [time; tmp.time+max(time)];
channel = [channel; tmp.channel];
unit = [unit; tmp.unit];

save herbI5374_12-13DIV_CNQX-ChR2-stim.mat time channel unit t*

%% asdr
load('herbI5374_12-13DIV_CNQX-ChR2-stim.mat','time','channel','unit');

dt = 60;
bin = floor(min(time)/dt)*dt:dt:ceil(max(time)/dt)*dt;
asdr = histc(time,bin)/dt;
hold off;
plot([t_startBB t_startBB+24*3600+5*60]/3600,[31.4049 31.4049],'-r','LineWidth',3);
hold on;
stairs(bin/3600,asdr,'-k');
plot([t_addCNQX t_wash12]/3600,[ceil(max(asdr)/10)*10 max(asdr)],'b*');
xlim([min(time) max(time)]/3600);
ylim([0 ceil(max(asdr)/10)*10+10]);
set(gca,'TickDir','out','TickLength',[0.0025 0.0025],'XTick',[-6:2:max(time)/3600]);
xlabel('Time (hours)');
ylabel('ASDR (Hz)');
box off;
export_fig(['C:\Users\Lab\Documents\2012-10-03_herbI5374_12-14DIV_MEA_CNQX-ChR2-stim-CL\herbI5374_12-13DIV_ASDR-1min-bin.png'],'-png');

%%
dt = 1;
bin = floor(min(time)/dt)*dt:dt:ceil(max(time)/dt)*dt;
asdr = histc(time,bin)/dt;
hold off;
plot([t_startBB t_startBB+24*3600+5*60]/3600,[31.4049 31.4049],'-r','LineWidth',3);
hold on;
stairs(bin/3600,asdr,'-k');
plot([t_addCNQX t_wash12]/3600,[ceil(max(asdr)/10)*10 max(asdr)],'b*');
xlim([min(time) max(time)]/3600);
ylim([0 ceil(max(asdr)/10)*10+10]);
set(gca,'TickDir','out','TickLength',[0.0025 0.0025],'XTick',[-6:2:max(time)/3600]);
xlabel('Time (hours)');
ylabel('ASDR (Hz)');
box off;
export_fig(['C:\Users\Lab\Documents\2012-10-03_herbI5374_12-14DIV_MEA_CNQX-ChR2-stim-CL\herbI5374_12-13DIV_ASDR-1sec-bin.png'],'-png');

%%
dt = 10*60;
bin = floor(min(time)/dt)*dt:dt:ceil(max(time)/dt)*dt;
asdr = histc(time,bin)/dt;
hold off;
plot([t_startBB t_startBB+24*3600+5*60]/3600,[31.4049 31.4049],'-r','LineWidth',3);
hold on;
stairs(bin/3600,asdr,'-k');
plot([t_addCNQX t_wash12]/3600,[ceil(max(asdr)/10)*10 max(asdr)],'b*');
xlim([min(time) max(time)]/3600);
ylim([0 ceil(max(asdr)/10)*10+10]);
set(gca,'TickDir','out','TickLength',[0.0025 0.0025],'XTick',[-6:2:max(time)/3600],'LineWidth',2);
xlabel('Time (hours)');
ylabel('ASDR (Hz)');
box off;
export_fig(['C:\Users\Lab\Documents\2012-10-03_herbI5374_12-14DIV_MEA_CNQX-ChR2-stim-CL\herbI5374_12-13DIV_ASDR-10min-bin.png'],'-png');

%%
dt = 1;
bin = floor(min(time)/dt)*dt:dt:ceil(max(time)/dt)*dt;
asdr = histc(time,bin)/dt;
hold off;
plot([t_startBB t_startBB+24*3600+5*60]/3600,[31.4049 31.4049]+4000,'-r','LineWidth',3);
hold on;
stairs(bin/3600,asdr,'-k');
plot([t_addCNQX t_wash12]/3600,[ceil(max(asdr)/10)*10 max(asdr)],'b*');
xlim([min(time) max(time)]/3600);
ylim([0 4100]);
set(gca,'TickDir','out','TickLength',[0.0025 0.0025]) %,'XTick',[-6:2:max(time)/3600]);
xlabel('Time (hours)');
ylabel('ASDR (Hz)');
box off;
%export_fig(['C:\Users\Lab\Documents\2012-10-03_herbI5374_12-14DIV_MEA_CNQX-ChR2-stim-CL\herbI5374_12-13DIV_ASDR-1sec-bin.png'],'-png');
