0001 function [ccg,t,tau,c] = CCG(times,id,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 d = 2;
0088 duration = [];
0089 binSize = 0.01;
0090 nBins = [];
0091 smooth = 0;
0092 groups = [];
0093 mode = 'ccg';
0094 alpha = 0.05;
0095
0096
0097 if nargin < 2,
0098 error('Incorrect number of parameters (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0099 end
0100 if ~isdvector(times),
0101 error('Parameter ''times'' is not a real-valued vector (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0102 end
0103 if ~isdscalar(id) && ~isdvector(id),
0104 error('Parameter ''id'' is not a real-valued scalar or vector (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0105 end
0106 if ~isdscalar(id) && length(times) ~= length(id),
0107 error('Parameters ''times'' and ''id'' have different lengths (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0108 end
0109 id = id(:);
0110 times = times(:);
0111 totalTime = max(times)-min(times);
0112
0113
0114 for i = 1:2:length(varargin),
0115 if ~ischar(varargin{i}),
0116 error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help CCG">CCG</a>'' for details).']);
0117 end
0118 switch(lower(varargin{i})),
0119 case 'binsize',
0120 binSize = varargin{i+1};
0121 if ~isdscalar(binSize,'>0'),
0122 error('Incorrect value for property ''binSize'' (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0123 end
0124 case 'nbins',
0125 nBins = varargin{i+1};
0126 if ~isiscalar(nBins,'>0'),
0127 error('Incorrect value for property ''nBins'' (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0128 end
0129 case 'duration',
0130 duration = varargin{i+1};
0131 if ~isdscalar(duration,'>0'),
0132 error('Incorrect value for property ''duration'' (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0133 end
0134 case 'smooth',
0135 smooth = varargin{i+1};
0136 if ~isdscalar(smooth,'>=0'),
0137 error('Incorrect value for property ''smooth'' (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0138 end
0139 case 'groups',
0140 groups = varargin{i+1};
0141 if ~isempty(groups) && ~isdvector(groups) && length(times) ~= length(groups)
0142 error('Incorrect value for property ''groups'' (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0143 end
0144 case 'alpha',
0145 alpha = varargin{i+1};
0146 if ~isdscalar(alpha,'>0'),
0147 error('Incorrect value for property ''alpha'' (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0148 end
0149 case 'mode',
0150 mode = varargin{i+1};
0151 if ~isastring(mode,'ccg','ccv'),
0152 error('Incorrect value for property ''mode'' (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0153 end
0154 case 'totaltime',
0155 totalTime = varargin{i+1};
0156 if ~isdscalar(totalTime,'>0'),
0157 error('Incorrect value for property ''totaltime'' (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0158 end
0159 otherwise,
0160 error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help CCG">CCG</a>'' for details).']);
0161 end
0162 end
0163
0164
0165 if isempty(nBins),
0166 if isempty(duration),
0167 duration = d;
0168 end
0169 else
0170 if isempty(duration),
0171 duration = nBins*binSize;
0172 elseif duration ~= binSize*nBins,
0173 error('Incompatible ''duration'' and ''nBins'' parameters (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0174 end
0175 end
0176
0177 tau = [];
0178 c = [];
0179
0180
0181 if length(id) == 1,
0182 id = ones(length(times),1);
0183 nIDs = 1;
0184 else
0185 nIDs = length(unique(id));
0186 end
0187 if nIDs ~= max(id),
0188 error('Incorrect IDs (type ''help <a href="matlab:help CCG">CCG</a>'' for details).');
0189 end
0190 halfBins = round(duration/binSize/2);
0191 nBins = 2*halfBins+1;
0192 t = (-halfBins:halfBins)'*binSize;
0193
0194 if length(times) <= 1,
0195 return
0196 end
0197
0198
0199 [times,i] = sort(times);
0200 id = id(i);
0201 if ~isempty(groups),
0202 groups = groups(i);
0203 end
0204 counts = CCGEngine(times,id,binSize,halfBins);
0205
0206
0207 n = max(id);
0208 counts = reshape(counts,[nBins n n]);
0209 if n < nIDs,
0210 counts(nBins,nIDs,nIDs) = 0;
0211 end
0212
0213
0214 if ~isempty(groups),
0215 group1 = unique(id(groups == 1));
0216 group2 = unique(id(groups == 2));
0217 nGroup1 = length(group1);
0218 nGroup2 = length(group2);
0219 ccg = zeros(nBins,nGroup1,nGroup2);
0220 for i = 1:nGroup1,
0221 for j = 1:nGroup2,
0222 ccg(:,i,j) = Smooth(flipud(counts(:,group1(i),group2(j))),smooth);
0223 end
0224 end
0225 else
0226 ccg = zeros(nBins,nIDs,nIDs);
0227
0228 for g1 = 1:nIDs,
0229 for g2 = g1:nIDs,
0230 ccg(:,g1,g2) = Smooth(flipud(counts(:,g1,g2)),smooth);
0231 end
0232 end
0233
0234 for g1 = 1:nIDs,
0235 for g2 = 1:g1-1,
0236 ccg(:,g1,g2) = flipud(squeeze(ccg(:,g2,g1)));
0237 end
0238 end
0239 end
0240
0241
0242 if strcmp(mode,'ccv'),
0243
0244
0245 eventRate = zeros(nIDs,1);
0246 for i = 1:nIDs,
0247 eventRate(i) = sum(id==i)/totalTime;
0248 end
0249
0250
0251 ccv = zeros(size(ccg));
0252 tau = zeros(size(ccg,2),size(ccg,3));
0253 c = zeros(size(ccg,2),size(ccg,3));
0254
0255 nPairs = size(ccg,2)*size(ccg,3);
0256 disp(['# pairs: ' int2str(nPairs)]);
0257
0258 threshold = sqrt(2)*erfinv(1-(alpha/length(t)));
0259
0260 for i = 1:size(ccg,2),
0261 for j = 1:size(ccg,3),
0262
0263
0264 if ~isempty(groups),
0265 rate = eventRate(group1(i))*eventRate(group2(j));
0266 else
0267 rate = eventRate(i)*eventRate(j);
0268 end
0269 ccv(:,i,j) = sqrt((binSize*totalTime)/rate) * (ccg(:,i,j)/(binSize*totalTime)-rate);
0270
0271
0272 data = ccv(:,i,j);
0273 top = flipud(data(1:size(ccg,1),:));
0274 bottom = flipud(data(end-size(ccg,1)+1:end,:));
0275 data = [top;data;bottom];
0276 data = filter([1 1 1],3,data);
0277 n = size(data,1);
0278 d = n - size(ccg,1);
0279 start = d/2+1;
0280 stop = start + size(ccg,1) - 1;
0281 ccv(:,i,j) = data(start:stop);
0282
0283
0284 [~,maxIndex] = max(ccv(:,i,j));
0285 tau(i,j) = median(t(maxIndex));
0286 c(i,j) = median(ccv(maxIndex,i,j));
0287
0288
0289
0290
0291
0292
0293 if ~any(abs(ccv(:,i,j))>threshold),
0294 tau(i,j) = NaN;
0295 end
0296
0297 end
0298 end
0299
0300 nCorrelatedPairs = sum(~isnan(tau(:)));
0301 disp(['# significantly correlated pairs: ' int2str(nCorrelatedPairs)]);
0302
0303 ccg = ccv;
0304
0305 end