% CROSS-CORRELATION GRID (MATRIX) PLOT: (v9g_2014)of unit pairs on Y-axis(Reference units)& X-axis(Target units);
% AUTHOR: Georgina Fenton (Feb 2011-Nov2012;  revised April/May 2014 - script suggested by Dr Rob Mason)

% NOTES: 
% SCRIPT Default values: +/- 1000ms; 1ms bin width; +/-200ms end_shoulders for computation.
%(1) use cursor to left click on (Grey-scale/Colour-scale) grid pixels to reveal individual x-correlogram plots
%(2) use cursor to right click on (Grey-scale/Colour-scale) grid pixels to reveal a separate permanent individual x-correlogram plots
%(3) Xcorr plots: red_line = mean (firing rate / 0(zero) z-score); black_line = +/- 95% confidence levels; green_line = Half Peak Width;
%(4) EXCEL file provides xCorr peak height & half-height width values

% Provides a Bar plot of K-index (correlation height) vs. half-heigth width(HHW) for two MEA placement regions (R1 & R2, e.g. mPFC/hippocampus)
% with user-defined pre-/post-event times

% GRID_MATRIX INTERPRETATION:
% K-index = magnitude of correlation
% HHW = reflects spike timing correlation "jitter" variance - narow / broad /sckewed profiles
% Grid-matrix plot: ROI 1 & 2 = "triangular components" reflecting  intra-region correlations 
%                :  ROI 3 = "square component" reflecting  inter-region (ROI 1-ROI 2) correlations
% unit-pairs correlation offset distribution plot

% NEW TWEAKS (May 2014)
%Plot (histogram) of number of units (Y-axis) at each peak offset (X-axis +/- (ms)) with unit pair ID
%Peak offset value recorded in EXCEL & descriptive statistics CCH panel 
%Number of correlated vs. uncorrelated unit pairs in ROIs 1,2 & 3


function OpenXCorrGrid


%Data File Load - *.mat file
filename=input('Specify Filename:','s')
filenameext=strcat(filename,'.mat');
data = dir(filenameext);
load(data.name);


%TW = Time window - default 1000ms
disp('Specify Time Window (s; Default = +/- 1s):');
    tw = input('');
    if isempty(tw)==1;
        tw=1;
    end
    tw=tw*1000;

%Time(1,1)=start time    
disp('Specify START Time (s):')
    Time(1,1)=input('')
    if isempty(Time(1,1))==1;
        error('You must enter a start time')
    end

%Time(1,2)=end time    
disp('Specify END Time (s):')
    Time(1,2)=input('')
    if isempty(Time(1,2))==1;
        error('You must enter an end time')
    end
 
%znorm = perform zscore analysis? 1=yes,2=no    
zscorenorm=input('Perform Z Score Normalisation? Y/N [NO if - low spike counts, i.e. MEA culture data]:','s')
% If user chooses to search for optimal model order...
if zscorenorm == 'Y' || zscorenorm == 'y';                                         % If user chooses to search for optimal model order...
    znorm=1;
elseif zscorenorm == 'N' || zscorenorm == 'n';
    znorm=2; 
    else error('Incorrect input')
end

disp('Specify the "Shoulder Size (+/- ms)" (usually = 200ms):')                         % basal computation using 200ms means using shoulder at 800-1000ms with default +/-1s               %
    shoulder=input('')
    if isempty(shoulder)==1
            shoulder=200
    end



%autonorm = perform normalise grid values to autocorrelation; 1=yes,2=no    
autonorm=input('Normalise correlation grid to auto correlation values? Y/N [usally = YES]:','s')
if autonorm == 'Y' || autonorm == 'y';                                         % If user chooses to search for optimal model order...
    autonorm=1;
   
elseif autonorm == 'N' || autonorm == 'n';
    autonorm=2;
else error('Incorrect input')
end

%imagecolour = greyscale or color grid
greycol=input('View GRID plot in Colour (C) or Greyscale (G):','s')
if greycol == 'C' || greycol == 'c';                                         % If user chooses to search for optimal model order...
    imagecolour=1;
elseif greycol == 'G' || greycol == 'g';
    imagecolour=2;
else error('Incorrect input')
end
  
%Regions of Interest (ROIs)  
disp('Specify 1st ROI (region of interest), e.g. 8 = first 8 units (sig01-08):')
    ROI=input('')
    if isempty(ROI)==1;
        error('You must enter 1st region of interest')
    end

%max PW50 - DEFAULT=400ms (+/-200ms); use to exclude spurious/silly outliers
disp('Specify maximum PW50; (Default = 400ms; i.e. = +/-200ms):');
    PW50 = input('');
    if isempty(PW50)==1;
        PW50=400;
    end

%%DO NOT EDIT%%%    
[correl]=multiple_correlation(data,tw,Time,znorm,shoulder,imagecolour,autonorm,ROI,PW50) 
    
 
    