%% load spike data
clear all;
close all;

dirID = 'C:\Users\USER\Documents\mingfaifong\2013-01-21_trouble22468_12-14DIV_MEA_CNQX-ChR2-stim-CL\';
fid.spont = 'trouble22478_12DIV_nodrug_02';
fid.cnqxstim = 'trouble22468_13DIV_40uM-CNQX-ChR2-stim-CL_03';
fid.prefix = 'trouble22468';

spk.spont = loadspk([dirID fid.spont '.spk']);
spk.cnqxstim = loadspk([dirID fid.cnqxstim '.spk']);

%% pick burst time
subplot(4,1,1);
plot(spk.spont.time,spk.spont.channel+0.15*randn(size(spk.spont.channel)),'.k');
subplot(4,1,2);
plot(spk.cnqxstim.time,spk.cnqxstim.channel+0.15*randn(size(spk.cnqxstim.channel)),'.k');

bin_spont = 0:0.01:max(spk.spont.time);
bin_cnqxstim = 0:0.01:max(spk.cnqxstim.time);
asdr_spont = histc(spk.spont.time,bin_spont);
asdr_cnqxstim = histc(spk.cnqxstim.time,bin_cnqxstim);

subplot(4,1,3);
stairs(bin_spont,asdr_spont,'-k');
subplot(4,1,4);
stairs(bin_cnqxstim,asdr_cnqxstim,'-k');

%% load raw data at burst time of interest
t1_spont = 53.87-1;
t1_cnqxstim = 419.08-1;

[r_spont t_spont] = loadraw([dirID fid.spont '.spkflt'],[t1_spont t1_spont+5]);
[r_cnqxstim t_cnqxstim] = loadraw([dirID fid.cnqxstim '.spkflt'],[t1_cnqxstim t1_cnqxstim+5]);

% v_max = abs(min([max(max(r_spont)) max(max(r_cnqxstim))]));

%% plot raw by channel
t_offset = 0.002;
t_min = 1-0.2;
t_max = 2-0.2;
v_max = 150;

close all;
figure;

for i = 1:64
    if i<10
        ch = ['0' num2str(i)];
    else
        ch = num2str(i);
    end
    subplot(2,1,1);
    hold off;
    plot(t_spont-min(t_spont),r_spont(i,:)/10^-6,'-k','LineWidth',0.1);
%     title(['channel ',num2str(i) ' (spontaneous)']);
    % v_max = max(abs([r_spont(i,:) r_cnqxstim(i,:)]/10^-6))+5;
    xlim([t_min t_max]);
    ylim([-v_max v_max]);
%     ylabel('Voltage (uV)','FontSize',14)
    box off;
    set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
    set(gca,'TickDir','out','TickLength',[0.01 0.0025],...
        'FontSize',6,'FontName','Arial');

    subplot(2,1,2);
    hold off;
    plot(t_cnqxstim-min(t_cnqxstim)-t_offset,r_cnqxstim(i,:)/10^-6,'-','LineWidth',0.1,'Color',[29 118 187]/255);
%     title(['channel ',num2str(i) ' (CNQX+stimulation)']);
    set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
    xlim([t_min t_max]);
    ylim([-v_max v_max]);
%     xlabel('Time (sec)','FontSize',14); ylabel('Voltage (uV)','FontSize',14)
    box off;
    set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
    set(gca,'TickDir','out','TickLength',[0.01 0.0025],...
        'FontSize',6,'FontName','Arial');

    set(gcf,'Position',[360 708 299 214]);
    export_fig(gcf,[dirID 'raw data traces\' fid.prefix '_raw_ch' ch '.pdf'],'-pdf','-transparent','-r300');
end;

%% plot rasters

figure;
subplot(2,1,1);
plot(spk.spont.time-min(t_spont),spk.spont.channel+0.15*randn(size(spk.spont.channel)),'.k','MarkerSize',2);
title('spontaneous burst');
xlim([t_min t_max]);
ylim([0 64]);
set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
subplot(2,1,2);
plot(spk.cnqxstim.time-min(t_cnqxstim)-t_offset,spk.cnqxstim.channel+0.15*randn(size(spk.cnqxstim.channel)),'.k','MarkerSize',2);
title('activity evoked by ChR2 during 40uM CNQX application');
xlim([t_min t_max]);
ylim([0 64]);
set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
export_fig([dirID 'raw data traces\' fid.prefix '_raw_raster_y-jitter.png'],'-png');
export_fig(gcf,[dirID 'raw data traces\' fid.prefix '_raw_raster_y-jitter.pdf'],'-pdf','-transparent','-r300');

%% find most active units
[y(1,:) y(2,:)] = hist(spk.spont.channel,[2:63]);
sortrows(y')