cohmathelper.m 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. function [C,phi,S12,confC,phierr,Cerr]=cohmathelper(J,err,Nsp)
  2. % Helper function called by coherency matrix computations.
  3. %
  4. % Usage: [C,phi,S12,confC,phierr,Cerr]=cohmathelper(J,err,Nsp)
  5. % Inputs:
  6. % J : Fourier transforms of data
  7. % err : [0 p] or 0 for no errors; [1 p] for theoretical confidence level,
  8. % [2 p] for Jackknife (p - p value)
  9. % Nsp : pass the number of spikes in each channel if finite size corrections are desired
  10. %
  11. % Outputs:
  12. %
  13. % C : coherence
  14. % phi : phase of coherency
  15. % S12 : cross spectral matrix
  16. % confC : confidence level for coherency - only for err(1)>=1
  17. % phierr - standard deviation for phi (note that the routine gives phierr as phierr(1,...)
  18. % and phierr(2,...) in order to incorporate Jackknife (eventually).
  19. % Currently phierr(1,...)=phierr(2,...). Note that phi + 2 phierr(1,...) and phi -2
  20. % phierr(2,...) will give 95% confidence bands for phi - only for err(1)>=1
  21. % Cerr : error bars for coherency (only for Jackknife estimates)-only for err(1)=2
  22. %
  23. errtype=err(1);
  24. trialave=0;
  25. [nf,K,Ch]=size(J);
  26. clear K
  27. confC=zeros(Ch,Ch);
  28. C=zeros(nf,Ch,Ch);
  29. S12=zeros(nf,Ch,Ch);
  30. phi=zeros(nf,Ch,Ch);
  31. phierr=zeros(2,nf,Ch,Ch);
  32. if errtype==2; Cerr=zeros(2,nf,Ch,Ch);end;
  33. for ch1=1:Ch;
  34. J1=squeeze(J(:,:,ch1));
  35. C(1:nf,ch1,ch1)=1;
  36. phi(1:nf,ch1,ch1)=0;
  37. % if errtype==2;
  38. % phierr(1:nf,ch1,ch1)=0;
  39. % Cerr(1:2,1:nf,ch1,ch1)=0;
  40. % elseif errtype==1
  41. % phierr(1:2,1:nf,ch1,ch1)=0;
  42. % end;
  43. s1=squeeze(mean(conj(J1).*J1,2));
  44. for ch2=1:ch1-1;
  45. J2=squeeze(J(:,:,ch2));
  46. s12=squeeze(mean(conj(J1).*J2,2));
  47. s2=squeeze(mean(conj(J2).*J2,2));
  48. C12=s12./sqrt(s1.*s2);
  49. C(:,ch1,ch2)=abs(C12);
  50. C(:,ch2,ch1)=C(:,ch1,ch2);
  51. phi(:,ch1,ch2)=angle(C12);
  52. phi(:,ch2,ch1)=phi(:,ch1,ch2);
  53. S12(:,ch1,ch2)=s12;
  54. S12(:,ch2,ch1)=S12(:,ch1,ch2);
  55. if errtype==2
  56. if nargin<3;
  57. [conf,phie,Ce]=coherr(abs(C12),J1,J2,err,trialave);
  58. else
  59. [conf,phie,Ce]=coherr(abs(C12),J1,J2,err,trialave,Nsp(ch1),Nsp(ch2));
  60. end
  61. confC(ch1,ch2)=conf;
  62. phierr(1,:,ch1,ch2)=phie;phierr(2,:,ch1,ch2)=phie;
  63. Cerr(1,:,ch1,ch2)=Ce(1,:);
  64. Cerr(2,:,ch1,ch2)=Ce(2,:);
  65. confC(ch2,ch1)=conf;
  66. phierr(1,:,ch2,ch1)=phie;phierr(2,:,ch2,ch1)=phie;
  67. Cerr(:,:,ch2,ch1)=Ce;
  68. elseif errtype==1
  69. if nargin<3;
  70. [conf,phie]=coherr(abs(C12),J1,J2,err,trialave);
  71. else
  72. [conf,phie]=coherr(abs(C12),J1,J2,err,trialave,Nsp(ch1),Nsp(ch2));
  73. end
  74. confC(ch1,ch2)=conf;
  75. phierr(1,:,ch1,ch2)=phie;phierr(2,:,ch1,ch2)=phie;
  76. confC(ch2,ch1)=conf;
  77. phierr(1,:,ch2,ch1)=phie;phierr(2,:,ch2,ch1)=phie;
  78. end;
  79. end;
  80. end;