0001 function [a,b,c] = SyncHist(synchronized,indices,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 durations = [-0.5 0.5];
0080 nBins = 100;
0081 smooth = 0;
0082 type = 'linear';
0083 bins = [];
0084 a = [];
0085 b = [];
0086 c = [];
0087 mode = 'sum';
0088 error = 'std';
0089 
0090 
0091 if nargin < 2 | mod(length(varargin),2) ~= 0,
0092   error('Incorrect number of parameters (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0093 end
0094 
0095 if isempty(indices) || isempty(synchronized), return; end
0096 
0097 
0098 if size(indices,2) ~= 1,
0099     error('Parameter ''indices'' is not a vector (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0100 end
0101 if size(indices,1) ~= size(synchronized,1),
0102     error('Parameters ''synchronized'' and ''indices'' have different lengths (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0103 end
0104 
0105 
0106 for i = 1:2:length(varargin),
0107     if ~ischar(varargin{i}),
0108         error(['Parameter ' num2str(i+1) ' is not a property (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).']);
0109     end
0110     switch(lower(varargin{i})),
0111         case 'mode',
0112             mode = lower(varargin{i+1});
0113             if ~isastring(mode,'sum','mean','dist'),
0114                 error('Incorrect value for property ''mode'' (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0115             end
0116 
0117         case 'durations',
0118             durations = varargin{i+1};
0119             if ~isdvector(durations,'#2','<'),
0120                 error('Incorrect value for property ''durations'' (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0121             end
0122 
0123         case 'nbins',
0124             nBins = varargin{i+1};
0125             if ~isdscalar(nBins,'>0'),
0126                 error('Incorrect value for property ''nBins'' (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0127             end
0128 
0129         case 'smooth',
0130             smooth = varargin{i+1};
0131             if ~isdvector(smooth,'>=0') | length(smooth) > 2,
0132                 error('Incorrect value for property ''smooth'' (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0133             end
0134 
0135         case {'bins','hist'},
0136             bins = varargin{i+1};
0137             if ~isdvector(bins,'#3') | bins(1) > bins(2) | ~isiscalar(bins(3),'>0'),
0138                 error('Incorrect value for property ''bins'' (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0139             end
0140 
0141         case 'type',
0142             type = lower(varargin{i+1});
0143             if ~isastring(type,'linear','circular'),
0144                 error('Incorrect value for property ''type'' (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0145             end
0146 
0147         case 'error',
0148             error = lower(varargin{i+1});
0149             if ~isastring(error,'std','sem','95%'),
0150                 error('Incorrect value for property ''error'' (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0151             end
0152 
0153         otherwise,
0154             error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).']);
0155 
0156     end
0157 end
0158 
0159 
0160 switch(mode),
0161     case 'sum',
0162         if nargout > 2,
0163             error('Incorrect number of output parameters (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0164         end
0165     case 'mean',
0166         if nargout > 4,
0167             error('Incorrect number of output parameters (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0168         end
0169     case 'dist',
0170         if nargout > 3,
0171             error('Incorrect number of output parameters (type ''help <a href="matlab:help SyncHist">SyncHist</a>'' for details).');
0172         end
0173 end
0174 
0175 if size(synchronized,2) == 1,
0176     
0177     pointProcess = true;
0178     synchronized(:,2) = 1;
0179     if isempty(bins),
0180         bins = [0 max(synchronized(:,2)) 100];
0181     end
0182 else
0183     
0184     pointProcess = false;
0185     if isempty(bins),
0186         bins = [min(min(synchronized(:,2:end))) max(max(synchronized(:,2:end))) 100];
0187     end
0188 end
0189 
0190 
0191 nTrials = max(indices);
0192 start = durations(1);
0193 stop = durations(2);
0194 timeBinSize = (stop - start)/nBins;
0195 timeBins = (start:timeBinSize:stop-timeBinSize)+timeBinSize/2;
0196 binnedTime = Bin(synchronized(:,1),[start stop],nBins,'trim');
0197 
0198 if strcmp(type,'circular'),
0199     range = isradians(synchronized(:,2:end));
0200     for j = 2:size(synchronized,2),
0201         synchronized(:,j) = exp(i*synchronized(:,j));
0202     end
0203 end
0204 
0205 switch(mode),
0206 
0207     case 'sum',
0208         
0209         for j = 2:size(synchronized,2),
0210             a(:,j-1) = Smooth(Accumulate(binnedTime,synchronized(:,j),nBins),smooth);
0211         end
0212         b = timeBins;
0213         c = [];
0214     case 'mean',
0215         if pointProcess,
0216             
0217             for j = 2:size(synchronized,2),
0218                 a(:,j-1) = Smooth(Accumulate(binnedTime,synchronized(:,j),nBins),smooth)/(nTrials*timeBinSize);
0219             end
0220             b = []; 
0221         else
0222             
0223             n = Smooth(Accumulate(binnedTime,1,nBins),smooth);
0224             
0225             for j = 2:size(synchronized,2),
0226                 a(:,j-1) = Smooth(Accumulate(binnedTime,synchronized(:,j),nBins),smooth)./n;
0227             end
0228             if strcmp(type,'circular'),
0229                 b = []; 
0230             else
0231                 if strcmp(error,'std'),
0232                     
0233                     for j = 2:size(synchronized,2),
0234                         sq = Smooth(Accumulate(binnedTime,synchronized(:,j).^2,nBins),smooth)./n; 
0235                         b(:,j-1) = sqrt(sq-a(:,j-1).^2).*n./(n-1);
0236                     end
0237                 elseif strcmp(error,'sem'),
0238                     
0239                     for j = 2:size(synchronized,2),
0240                         sq = Smooth(Accumulate(binnedTime,synchronized(:,j).^2,nBins),smooth)./n; 
0241                         b(:,j-1) = sqrt(sq-a(:,j-1).^2).*n./(n-1)./sqrt(n);
0242                     end
0243                 elseif strcmp(error,'95%'),
0244                     
0245                     for j = 2:size(synchronized,2),
0246                         for i = 1:nBins,
0247                             b(i,j-1,1) = prctile(synchronized(binnedTime==i,2),5);
0248                             b(i,j-1,2) = prctile(synchronized(binnedTime==i,2),95);
0249                         end
0250                     end
0251                 end
0252             end
0253         end
0254         c = timeBins;
0255     case 'dist',
0256         if pointProcess,
0257             
0258             n = Accumulate([binnedTime indices],1);
0259             
0260             a = Smooth(Accumulate([n(:)+1 repmat((1:nBins)',nTrials,1)],1),smooth)/nTrials;
0261             b = timeBins;
0262             c = 0:max(max(n));
0263         else
0264             minValue = bins(1);
0265             maxValue = bins(2);
0266             nValueBins = bins(3);
0267             
0268             n = Accumulate(binnedTime,1,nBins);
0269             for j = 2:size(synchronized,2),
0270                 
0271                 binnedValues = Bin(synchronized(:,j),[minValue maxValue],nValueBins);
0272                 
0273                 a(:,:,j-1) = Smooth(Accumulate([binnedValues binnedTime],1,[nValueBins nBins]),smooth)./Smooth(repmat(n',nValueBins,1),smooth);
0274             end
0275             range = maxValue - minValue;
0276             binSize = range/nValueBins;
0277             b = timeBins;
0278             c = (minValue:range/nValueBins:maxValue-binSize)'+binSize/2;
0279         end
0280 end
0281 
0282 if strcmp(type,'circular'),
0283     a(:,2:end) = wrap(a(:,2:end),range);
0284 end