0001 function [tosquares,quads,D]=nt_qca0(x,npcs,nsmooth,nquads)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 if nargin<4; nquads=1; end
0017 if nargin<3; nsmooth=1; end
0018 if nargin<2; npcs=[]; end
0019 [nsamples,nchans,ntrials]=size(x);
0020
0021
0022
0023 THRESH=10^-12;
0024 [topcs,pwr]=nt_pca0(x,[],[],THRESH);
0025 if isempty(npcs) || npcs>size(topcs,2); npcs=size(topcs,2); end
0026 topcs=topcs(:,1:npcs);
0027 x=nt_mmat(x,topcs);
0028
0029
0030 Cross-products are formed trial by trial to save space.
0031
0032
0033
0034 c0=zeros(npcs*(npcs+1)/2);
0035 xxx=zeros(nsamples-nsmooth+1,npcs*(npcs+1)/2);
0036 for k=1:ntrials
0037 xx=zeros(nsamples,npcs*(npcs+1)/2);
0038 ii=1;
0039 for jj=1:npcs
0040 for kk=1:jj
0041 xx(:,ii)=x(:,kk,k).*x(:,jj,k);
0042 ii=ii+1;
0043 end
0044 end
0045 xx=filter(ones(nsmooth,1)/nsmooth,1,xx);
0046 xx=xx(nsmooth:end,:,:);
0047 xxx=xxx+xx;
0048 c0=c0+xx'*xx;
0049 end
0050 c1=xxx'*xxx;
0051
0052
0053 [todss,pwr0,pwr1]=nt_dss0(c0,c1,[],0);
0054 tobest=todss(:,2);
0055
0056
0057 [tosquares,D]=nt_quad2square(tobest,'colwise');
0058 tosquares=topcs*tosquares;
0059
0060
0061 if nargout>1;
0062 nquads=min(nquads,npcs*(npcs+1)/2-1);
0063 quads=zeros(nsamples-nsmooth+1,nquads+1,ntrials);
0064 for k=1:ntrials
0065 xx=zeros(nsamples,npcs*(npcs+1)/2);
0066 ii=1;
0067 for jj=1:npcs
0068 for kk=1:jj
0069 xx(:,ii)=x(:,kk,k).*x(:,jj,k);
0070 ii=ii+1;
0071 end
0072 end
0073 xx=filter(ones(nsmooth,1)/nsmooth,1,xx);
0074 xx=xx(nsmooth:end,:,:);
0075 quads(:,:,k)=nt_mmat(xx,todss(:,1:nquads+1));
0076 end
0077 end
0078