0001 function [C,tw,m]=nt_cov_lags(x,y,shifts,demeanflag)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 if nargin<2; error('!'); end
0020 if nargin<3 || isempty(shifts); shifts=[0]; end
0021 if nargin<4 || isempty(demeanflag); demeanflag=1; end
0022
0023 if isnumeric(x)
0024
0025
0026 if size(y,1)~=size(x,1); error('!'); end
0027 if size(y,3)~=size(x,3); error('!'); end
0028 n=size(x,2)+size(y,2);
0029 C=zeros(n,n,length(shifts));
0030 for iPage=1:size(x,3)
0031 for iLag=1:length(shifts)
0032 [xx,yy]=nt_relshift(x(:,:,iPage),y(:,:,iPage),shifts(iLag));
0033 if ~numel(xx); error('xx empty after shifting'); end
0034 if ~numel(yy); error('yy empty after shifting'); end
0035 if demeanflag
0036 xx=nt_demean(xx); yy=nt_demean(yy);
0037 end
0038 C(:,:,iLag)=C(:,:,iLag) +[xx,yy]'*[xx,yy];
0039 end
0040 end
0041 m=size(x,2);
0042 tw=size(x,1)*size(x,3);
0043
0044 else
0045
0046 if isnumeric(y); error('!'); end
0047
0048
0049 n=size(x{1},2)+size(y{1},2);
0050 C=zeros(n,n,length(shifts));
0051 tw=0;
0052 for iCell=1:length(x);
0053
0054 C=C+nt_cov_lags(x{iCell},y{iCell},shifts);
0055 tw=tw+size(x{iCell},1);
0056 end
0057 m=size(x{1},2);
0058 end
0059