uispecerr.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. function [Serrp,Serrj]=uispecerr(S,J,err,trialave,numsp)
  2. % Function to compute lower and upper confidence intervals on the spectrum
  3. % Usage: [Serrp,Serrj]=uispecerr(S,J,err,trialave,numsp)
  4. % Outputs: Serrp (Serrp(1,...) - lower confidence level, Serrp(2,...) upper
  5. % confidence level), similarly for Serrj
  6. %
  7. % Inputs:
  8. % S - spectrum
  9. % J - tapered fourier transforms
  10. % err - [errtype p] (errtype=1 - asymptotic estimates; errchk=2 - Jackknife estimates;
  11. % p - p value for error estimates)
  12. % trialave - 0: no averaging over trials/channels
  13. % 1 : perform trial averaging
  14. % numsp - number of spikes in each channel. specify only when finite
  15. % size correction required (and of course, only for point
  16. % process data)
  17. %
  18. % Outputs:
  19. % Serrp - population error estimates. Only for err(1)>=1.
  20. % Serrj - jackknife error estimates. Computed only for err(1)>=2, otherwise
  21. % set to zero
  22. if nargin < 4; error('Need at least 4 input arguments'); end;
  23. 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;
  24. [nf,K,C]=size(J);
  25. errchk=err(1);
  26. p=err(2);
  27. pp=1-p/2;
  28. qq=1-pp;
  29. if trialave
  30. dim=K*C;
  31. C=1;
  32. dof=2*dim;
  33. if nargin==5; dof = fix(1/(1/dof + 1/(2*sum(numsp)))); end
  34. J=reshape(J,nf,dim);
  35. else
  36. dim=K;
  37. dof=2*dim*ones(1,C);
  38. for ch=1:C;
  39. if nargin==5; dof(ch) = fix(1/(1/dof + 1/(2*numsp(ch)))); end
  40. end;
  41. end;
  42. Serrp=zeros(2,nf,C);
  43. Serrj=zeros(2,nf,C);
  44. if errchk>1=;
  45. Qp=chi2inv(pp,dof);
  46. Qq=chi2inv(qq,dof);
  47. Serrp(1,:,:)=dof(ones(nf,1),:).*S./Qp(ones(nf,1),:);
  48. Serrp(2,:,:)=dof(ones(nf,1),:).*S./Qq(ones(nf,1),:);
  49. elseif errchk>=2;
  50. tcrit=tinv(pp,dim-1);
  51. for k=1:dim;
  52. indices=setdiff(1:dim,k);
  53. Jjk=J(:,indices,:); % 1-drop projection
  54. eJjk=squeeze(sum(Jjk.*conj(Jjk),2));
  55. Sjk(k,:,:)=eJjk/(dim-1); % 1-drop spectrum
  56. end;
  57. sigma=sqrt(dim-1)*squeeze(std(log(Sjk),1,1)); if C==1; sigma=sigma'; end;
  58. conf=repmat(tcrit,nf,C).*sigma;
  59. conf=squeeze(conf);
  60. Serrj(1,:,:)=S.*exp(-conf); Serrj(2,:,:)=S.*exp(conf);
  61. end;
  62. Serrp=squeeze(Serrp);
  63. Serrj=squeeze(Serrj);