0001 function [C,phi,S12,confC,phierr,Cerr]=cohmathelper(J,err,Nsp)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 errtype=err(1);
0020 trialave=0;
0021 [nf,K,Ch]=size(J);
0022 for ch1=1:Ch;
0023 J1=squeeze(J(:,:,ch1));
0024 C(1:nf,ch1,ch1)=1;
0025 phi(1:nf,ch1,ch1)=0;
0026 if errtype==2;
0027 phierr(1:nf,ch1,ch1)=0;
0028 Cerr(1:2,1:nf,ch1,ch1)=0;
0029 elseif errtype==1
0030 phierr(1:nf,ch1,ch1)=0;
0031 end;
0032 s1=squeeze(mean(conj(J1).*J1,2));
0033 for ch2=1:ch1-1;
0034 J2=squeeze(J(:,:,ch2));
0035 s12=squeeze(mean(conj(J1).*J2,2));
0036 s2=squeeze(mean(conj(J2).*J2,2));
0037 C12=s12./sqrt(s1.*s2);
0038 C(:,ch1,ch2)=abs(C12);
0039 C(:,ch2,ch1)=C(:,ch1,ch2);
0040 phi(:,ch1,ch2)=angle(C12);
0041 phi(:,ch2,ch1)=phi(:,ch1,ch2);
0042 S12(:,ch1,ch2)=s12;
0043 S12(:,ch2,ch1)=S12(:,ch1,ch2);
0044 if errtype==2
0045 if nargin<3;
0046 [conf,phie,Ce]=coherr(abs(C12),J1,J2,err,trialave);
0047 else
0048 [conf,phie,Ce]=coherr(abs(C12),J1,J2,err,trialave,Nsp(ch1),Nsp(ch2));
0049 end
0050 confC(ch1,ch2)=conf;
0051 phierr(:,ch1,ch2)=phie;
0052 Cerr(1,:,ch1,ch2)=Ce(1,:);
0053 Cerr(2,:,ch1,ch2)=Ce(2,:);
0054 confC(ch2,ch1)=conf;
0055 phierr(:,ch2,ch1)=phie;
0056 Cerr(:,:,ch2,ch1)=Ce;
0057 elseif errtype==1
0058 if nargin<3;
0059 [conf,phie]=coherr(abs(C12),J1,J2,err,trialave);
0060 else
0061 [conf,phie]=coherr(abs(C12),J1,J2,err,trialave,Nsp(ch1),Nsp(ch2));
0062 end
0063 confC(ch1,ch2)=conf;
0064 phierr(:,ch1,ch2)=phie;
0065 confC(ch2,ch1)=conf;
0066 phierr(:,ch2,ch1)=phie;
0067 end;
0068 end;
0069 end;