specerr.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. function Serr=specerr(S,J,err,trialave,numsp)
  2. % Function to compute lower and upper confidence intervals on the spectrum
  3. % Usage: Serr=specerr(S,J,err,trialave,numsp)
  4. % Outputs: Serr (Serr(1,...) - lower confidence level, Serr(2,...) upper confidence level)
  5. %
  6. % Inputs:
  7. % S - spectrum
  8. % J - 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. % numsp - number of spikes in each channel. specify only when finite
  14. % size correction required (and of course, only for point
  15. % process data)
  16. %
  17. % Outputs:
  18. % Serr - error estimates. Only for err(1)>=1. If err=[1 p] or [2 p] Serr(...,1) and Serr(...,2)
  19. % contain the lower and upper error bars with the specified method.
  20. if nargin < 4; error('Need at least 4 input arguments'); end;
  21. if err(1)==0; error('Need err=[1 p] or [2 p] for error bar calculation. Make sure you are not asking for the output of Serr'); end;
  22. [nf,K,C]=size(J);
  23. errchk=err(1);
  24. p=err(2);
  25. pp=1-p/2;
  26. qq=1-pp;
  27. if trialave
  28. dim=K*C;
  29. C=1;
  30. dof=2*dim;
  31. if nargin==5; dof = fix(1/(1/dof + 1/(2*sum(numsp)))); end
  32. J=reshape(J,nf,dim);
  33. else
  34. dim=K;
  35. dof=2*dim*ones(1,C);
  36. for ch=1:C;
  37. if nargin==5; dof(ch) = fix(1/(1/dof + 1/(2*numsp(ch)))); end
  38. end;
  39. end;
  40. Serr=zeros(2,nf,C);
  41. if errchk==1;
  42. Qp=chi2inv(pp,dof);
  43. Qq=chi2inv(qq,dof);
  44. Serr(1,:,:)=dof(ones(nf,1),:).*S./Qp(ones(nf,1),:);
  45. Serr(2,:,:)=dof(ones(nf,1),:).*S./Qq(ones(nf,1),:);
  46. elseif errchk==2;
  47. tcrit=tinv(pp,dim-1);
  48. for k=1:dim;
  49. indices=setdiff(1:dim,k);
  50. Jjk=J(:,indices,:); % 1-drop projection
  51. eJjk=squeeze(sum(Jjk.*conj(Jjk),2));
  52. Sjk(k,:,:)=eJjk/(dim-1); % 1-drop spectrum
  53. end;
  54. sigma=sqrt(dim-1)*squeeze(std(log(Sjk),1,1)); if C==1; sigma=sigma'; end;
  55. conf=repmat(tcrit,nf,C).*sigma;
  56. conf=squeeze(conf);
  57. Serr(1,:,:)=S.*exp(-conf); Serr(2,:,:)=S.*exp(conf);
  58. end;
  59. Serr=squeeze(Serr);