coherr.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. function [confC,phistd,Cerr]=coherr(C,J1,J2,err,trialave,numsp1,numsp2)
  2. % Function to compute lower and upper confidence intervals on the coherency
  3. % given the tapered fourier transforms, errchk, trialave.
  4. %
  5. % Usage: [confC,phistd,Cerr]=coherr(C,J1,J2,err,trialave,numsp1,numsp2)
  6. % Inputs:
  7. % C - coherence
  8. % J1,J2 - tapered fourier transforms
  9. % err - [errtype p] (errtype=1 - asymptotic estimates; errchk=2 - Jackknife estimates;
  10. % p - p value for error estimates)
  11. % trialave - 0: no averaging over trials/channels
  12. % 1 : perform trial averaging
  13. % numsp1 - number of spikes for data1. supply only if finite size corrections are required
  14. % numsp2 - number of spikes for data2. supply only if finite size corrections are required
  15. %
  16. % Outputs:
  17. % confC - confidence level for C - only for err(1)>=1
  18. % phistd - theoretical or jackknife standard deviation for phi for err(1)=1 and err(1)=2
  19. % respectively. returns zero if coherence is 1
  20. % Cerr - Jacknife error bars for C - only for err(1)=2
  21. % Jackknife uses the following transform of the coherence
  22. % z=sqrt(2*dim-2)atanh(C). Asymptotically (and for Gaussian data) var(z)=1.
  23. if nargin < 5; error('Need at least 5 input arguments'); end;
  24. if err(1)==0; error('Need err=[1 p] or [2 p] for error bar calculation'); end;
  25. if nargout==4 && err(1)==1; error('Cerr contains Jackknife errors: only computed when err(1) is 2'); end;
  26. [nf,K,Ch]=size(J1);
  27. errchk=err(1);
  28. p=err(2);
  29. pp=1-p/2;
  30. %
  31. % Find the number of degrees of freedom
  32. %
  33. if trialave;
  34. dim=K*Ch;
  35. dof=2*dim;
  36. dof1=dof;
  37. dof2=dof;
  38. Ch=1;
  39. if nargin>=6 && ~isempty(numsp1)
  40. totspikes1=sum(numsp1);
  41. dof1=fix(2*totspikes1*dof/(2*totspikes1+dof));
  42. end
  43. if nargin==7 && ~isempty(numsp2);
  44. totspikes2=sum(numsp2);
  45. dof2=fix(2*totspikes2*dof/(2*totspikes2+dof));
  46. end;
  47. dof=min(dof1,dof2);
  48. J1=reshape(J1,nf,dim);
  49. J2=reshape(J2,nf,dim);
  50. else
  51. dim=K;
  52. dof=2*dim;
  53. dof1=dof;
  54. dof2=dof;
  55. for ch=1:Ch;
  56. if nargin>=6 && ~isempty(numsp1);
  57. totspikes1=numsp1(ch);
  58. dof1=fix(2*totspikes1*dof/(2*totspikes1+dof));
  59. end;
  60. if nargin==7 && ~isempty(numsp2);
  61. totspikes2=numsp2(ch);
  62. dof2=fix(2*totspikes2*dof/(2*totspikes2+dof));
  63. end;
  64. dof(ch)=min(dof1,dof2);
  65. end;
  66. end;
  67. %
  68. % variance of the phase
  69. %
  70. %
  71. % Old code is the next few lines - new code is in the if statement below
  72. % beginning line 87
  73. %
  74. % if isempty(find((C-1).^2 < 10^-16));
  75. % phierr = sqrt((2./dof(ones(nf,1),:)).*(1./(C.^2) - 1));
  76. % else
  77. % phierr = zeros(nf,Ch);
  78. % end
  79. %
  80. % theoretical, asymptotic confidence level
  81. %
  82. if dof <= 2
  83. confC = 1;
  84. else
  85. df = 1./((dof/2)-1);
  86. confC = sqrt(1 - p.^df);
  87. end;
  88. %
  89. % Phase standard deviation (theoretical and jackknife) and jackknife
  90. % confidence intervals for C
  91. %
  92. if errchk==1;
  93. totnum=nf*Ch;
  94. phistd=zeros(totnum,1);
  95. CC=reshape(C,[totnum,1]);
  96. indx=find(abs(CC-1)>=1.e-16);
  97. dof=repmat(dof,[nf,1]);
  98. dof=reshape(dof,[totnum 1]);
  99. phistd(indx)= sqrt((2./dof(indx).*(1./(C(indx).^2) - 1)));
  100. phistd=reshape(phistd,[nf Ch]);
  101. elseif errchk==2;
  102. tcrit=tinv(pp,dof-1);
  103. for k=1:dim; % dim is the number of 'independent' estimates
  104. indxk=setdiff(1:dim,k);
  105. J1k=J1(:,indxk,:);
  106. J2k=J2(:,indxk,:);
  107. eJ1k=squeeze(sum(J1k.*conj(J1k),2));
  108. eJ2k=squeeze(sum(J2k.*conj(J2k),2));
  109. eJ12k=squeeze(sum(conj(J1k).*J2k,2));
  110. Cxyk=eJ12k./sqrt(eJ1k.*eJ2k);
  111. absCxyk=abs(Cxyk);
  112. atanhCxyk(k,:,:)=sqrt(2*dim-2)*atanh(absCxyk); % 1-drop estimate of z
  113. phasefactorxyk(k,:,:)=Cxyk./absCxyk;
  114. % indxk=setdiff(1:dim,k);
  115. % J1jk=J1(:,indxk,:);
  116. % J2jk=J2(:,indxk,:);
  117. % eJ1jk=squeeze(sum(J1jk.*conj(J1jk),2));
  118. % eJ2jk=squeeze(sum(J2jk.*conj(J2jk),2));
  119. % eJ12jk=squeeze(sum(conj(J1jk).*J2jk,2));
  120. % atanhCxyjk(k,:,:)=sqrt(2*dim-2)*atanh(abs(eJ12jk)./sqrt(eJ1jk.*eJ2jk));
  121. end;
  122. atanhC=sqrt(2*dim-2)*atanh(C); % z
  123. sigma12=sqrt(dim-1)*squeeze(std(atanhCxyk,1,1)); % Jackknife estimate std(z)=sqrt(dim-1)*std of 1-drop estimates
  124. % sigma12=sqrt(dim-1)*squeeze(std(atanhCxyjk,1,1));
  125. if Ch==1; sigma12=sigma12'; end;
  126. Cu=atanhC+tcrit(ones(nf,1),:).*sigma12;
  127. Cl=atanhC-tcrit(ones(nf,1),:).*sigma12;
  128. %Cerr(1,:,:) = tanh(Cl/sqrt(2*dim-2));
  129. Cerr(1,:,:) = max(tanh(Cl/sqrt(2*dim-2)),0); % This ensures that the lower confidence band remains positive
  130. Cerr(2,:,:) = tanh(Cu/sqrt(2*dim-2));
  131. %phistd=(2*dim-2)*(1-abs(squeeze(mean(phasefactorxyk))));
  132. phistd = sqrt( (2*dim-2)*(1-abs(squeeze(mean(phasefactorxyk)))) );
  133. if trialave; phistd=phistd'; end;
  134. end
  135. % ncrit=norminv(pp);
  136. % phierr=zeros([2 size(phistd)]);
  137. % phierr(1,:,:)=phi-ncrit*phistd; phierr(2,:,:)=phi+ncrit*phistd;