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 if size(y,1)~=size(x,1); error('!'); end
0026 if size(y,3)~=size(x,3); error('!'); end
0027 n=size(x,2)+size(y,2);
0028 C=zeros(n,n,length(shifts));
0029 for iPage=size(x,3)
0030 for iLag=1:length(shifts)
0031 [xx,yy]=nt_relshift(x(:,:,iPage),y(:,:,iPage),shifts(iLag));
0032 if ~numel(xx); error('xx empty after shifting'); end
0033 if ~numel(yy); error('yy empty after shifting'); end
0034 if demeanflag
0035 xx=nt_demean(xx); yy=nt_demean(yy);
0036 end
0037 C(:,:,iLag)=C(:,:,iLag) +[xx,yy]'*[xx,yy];
0038 end
0039 end
0040 m=size(x,2);
0041 tw=size(x,1)*size(x,3);
0042 else
0043 if isnumeric(y); error('!'); end
0044
0045
0046 n=size(x{1},2)+size(y{1},2);
0047 C=zeros(n,n,length(shifts));
0048 tw=0;
0049 for iCell=1:length(x);
0050
0051 C=C+nt_cov_lags(x{iCell},y{iCell},shifts);
0052 tw=tw+size(x{iCell},1);
0053 end
0054 m=size(x{1},2);
0055 end
0056