%% load data

dirID = 'C:\Users\USER\Documents\mingfaifong\2013-01-30_vivy_MEA-analysis\';
fid = 'vivy21369_12DIV_wash4_02';
loadspk([dirID fid '.spk']);


time = ans.time;
channel = ans.channel;
meta = ans.meta;
waveform = ans.waveform;
threshold = ans.threshold;

if exist('ans.unit','var')
    unit = ans.unit;
    save([dirID fid '.mat'],'time','channel','unit','meta','threshold');
else
    save([dirID fid '.mat'],'time','channel','meta','threshold');
end

%% generate asdr
clear t asdr
dt = 3600;
bins = ceil(min(time)/dt)*dt:dt:floor(max(time)/dt)*dt;
asdr = zeros(6,length(bins));

w(1,:) = [3 4 5 12 13 20 21 28 29];
w(2,:) = [7 14 15 16 22 23 24 30 31];
w(3,:) = [38 39 40 46 47 48 55 56 63];
w(4,:) = [36 37 44 45 52 53 60 61 62];
w(5,:) = [34 35 41 42 43 49 50 51 58];
w(6,:) = [2 9 10 17 18 19 25 26 27];

for i = 1:6
    %t{i} = time(clean==1 & ismember(channel,w(i,:))==1);
    t{i} = time(ismember(channel,w(i,:))==1);
    %c{i} = channel(clean==1 & ismember(channel,w(i,:))==1);
    asdr(i,:) = histc(t{i},bins)/dt;
end

%% plot stuff

%21367
cond = {'control','TTX+CTZ','bicuculline','TTX','CTZ','CNQX'};

%21369
cond = {'CNQX+stim','TTX+CTZ','control','APV','TTX','CNQX'};


for i = 1:6
    subplot(6,1,i); hold off;
    %hold on;
    %stairs(bins/3600-max(bins)/3600,asdr(i,:),'-k');
    stairs(bins/3600,asdr(i,:),'-k','LineWidth',2);
    %plot(bins(1:end-1)/3600+.5,asdr(i,1:end-1),'-ok','LineWidth',2,'MarkerSize',5);
    %xlim([0 24]);
    %ylim([0 1000]);
    %set(gca,'YScale','log')
    %set(gca,'XTick',0:4:max(time)/3600);
    ylabel('ASDR (Hz)');
end

xlabel('Time (hours)');

for i = 1:6
    subplot(6,1,i);
    xlim([-36 48]);
    ylim([0 20]);
    set(gca,'XTick',-36:4:48,'TickDir','out','TickLength',[0.0025 0.025])
    y_text = ylim;
    hold on;
    plot([0 0],[0 y_text(2)],':r');
    text(0.25,y_text(2)*.95,cond{i},'HorizontalAlignment','left','VerticalAlignment','top','Color','r');
    plot([24 24],[0 y_text(2)],':r');
    text(24.25,y_text(2)*.95,'wash','HorizontalAlignment','left','VerticalAlignment','top','Color','r');
end

subplot(6,1,1);
hold on;
plot([0 24],[5.9729 5.9729],'-g');

%% rasterplots

timepoints = [-2,2,12,20,26];

for i = 1:6
    for j = 1:length(timepoints);
        subplot(6,length(timepoints),