%This is a program to further characterize EOD spike relationship pooling
%all trials together
close all
clearvars
exp_date=input('please type in the experiment date : ','s');
load(strcat(exp_date,'_Stage1_output.mat'));
load(strcat(exp_date,'_Stage2_output.mat'));
% load(strcat(exp_date,'_Stage4_output.mat'));
% load(strcat(exp_date,'_Stage9_output.mat'));

[a,b]=size(uname);
w1=-0.5;% w1 second delay before each spike and w2 s delay after
w2=0.5;
res2=0.01;
interval=w1:res2:w2;

for tn=1:length(filename)

    for i=1:a
        if isempty(uname{i,tn})
            uname{i,tn}='NO';
        end
    end
end

k=1;
for tn=1:length(filename)
    for i=1:length(nuraw_count{tn})
        unitnames(k,:)=uname{i,tn};
        unitcounts(k)=nuraw_count{tn}(i);
        k=k+1;
    end
end

goodcells=unique(unitnames(:,2));
for w=1:length(goodcells)
    cellname=strcat('U',goodcells(w));
    index=strmatch(cellname,uname);
    [i,j]=ind2sub(size(uname),index);%i is trial number, j is the cell position in the matrix
    %for each cell, go through all the trials
    Theta_interp_pool{w}=[];
    Theta_crct_pool{w}=[];
     for k=1:length(index)
       
        Theta_interp_pool{w}=[Theta_interp_pool{w};Theta_interp{i(k),j(k)}];
        Theta_crct_pool{w}=[Theta_crct_pool{w};Theta_crct{j(k)}];
        
    end
    [a1,e]=histcounts(Theta_interp_pool{w},linspace(0,180,3),'Normalization','count');
    [a2,e]=histcounts(Theta_crct_pool{w},linspace(0,180,3),'Normalization','count');
    FR_Theta_pool_norm2{w}=a1./(a2/15);%this is rate per second
    dir_ratio(w)=FR_Theta_pool_norm2{w}(2)/FR_Theta_pool_norm2{w}(1)
    figure
    histogram('BinEdges',e,'BinCounts',FR_Theta_pool_norm2{w})
    title(strcat('U',goodcells(w)))
    %saveas(gcf,strcat('ThetaHistPool','_U',goodcells(w),'.pdf'))

end
%save(strcat(exp_date,'_Stage8_output2.mat'),'dir_ratio','FR_Theta_pool_norm2');