cohgrampb.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. function [C,phi,S12,S1,S2,t,f,zerosp,confC,phistd,Cerr]=cohgrampb(data1,data2,movingwin,params,fscorr)
  2. % Multi-taper time-frequency coherence,cross-spectrum and individual spectra - two binned point processes
  3. %
  4. % Usage:
  5. %
  6. % [C,phi,S12,S1,S2,t,f,zerosp,confC,phistd,Cerr]=cohgrampb(data1,data2,movingwin,params,fscorr)
  7. % Input:
  8. % Note units have to be consistent. Thus, if movingwin is in seconds, Fs
  9. % has to be in Hz. see chronux.m for more information.
  10. %
  11. % data1 (binned point process data in form samples x trials) -- required
  12. % data2 (binned point process data in form samples x trials) -- required
  13. % movingwin (in the form [window winstep] -- required
  14. % params: structure with fields tapers, pad, Fs, fpass, err, trialave
  15. % - optional
  16. % tapers : precalculated tapers from dpss or in the one of the following
  17. % forms:
  18. % (1) A numeric vector [TW K] where TW is the
  19. % time-bandwidth product and K is the number of
  20. % tapers to be used (less than or equal to
  21. % 2TW-1).
  22. % (2) A numeric vector [W T p] where W is the
  23. % bandwidth, T is the duration of the data and p
  24. % is an integer such that 2TW-p tapers are used. In
  25. % this form there is no default i.e. to specify
  26. % the bandwidth, you have to specify T and p as
  27. % well. Note that the units of W and T have to be
  28. % consistent: if W is in Hz, T must be in seconds
  29. % and vice versa. Note that these units must also
  30. % be consistent with the units of params.Fs: W can
  31. % be in Hz if and only if params.Fs is in Hz.
  32. % The default is to use form 1 with TW=3 and K=5
  33. % Note that T has to be equal to movingwin(1).
  34. %
  35. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  36. % -1 corresponds to no padding, 0 corresponds to padding
  37. % to the next highest power of 2 etc.
  38. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  39. % to 512 points, if pad=1, we pad to 1024 points etc.
  40. % Defaults to 0.
  41. % Fs (sampling frequency) - optional. Default 1.
  42. % fpass (frequency band to be used in the calculation in the form
  43. % [fmin fmax])- optional.
  44. % Default all frequencies between 0 and Fs/2
  45. % err (error calculation [1 p] - Theoretical error bars; [2 p] - Jackknife error bars
  46. % [0 p] or 0 - no error bars) - optional. Default 0.
  47. % trialave (average over trials when 1, don't average when 0) -
  48. % optional. Default 0
  49. % fscorr (finite size corrections, 0 (don't use finite size corrections) or
  50. % 1 (use finite size corrections) - optional
  51. % (available only for spikes). Defaults 0.
  52. % Output:
  53. % C (magnitude of coherency time x frequencies x trials for trialave=0;
  54. % time x frequency for trialave=1)
  55. % phi (phase of coherency time x frequencies x trials for no trial averaging;
  56. % time x frequency for trialave=1)
  57. % S12 (cross spectrum - time x frequencies x trials for no trial averaging;
  58. % time x frequency for trialave=1)
  59. % S1 (spectrum 1 - time x frequencies x trials for no trial averaging;
  60. % time x frequency for trialave=1)
  61. % S2 (spectrum 2 - time x frequencies x trials for no trial averaging;
  62. % time x frequency for trialave=1)
  63. % t (time)
  64. % f (frequencies)
  65. % zerosp (1 for windows and trials where spikes were absent (in either channel),zero otherwise)
  66. % confC (confidence level for C at 1-p %) - only for err(1)>=1
  67. % phistd - jackknife/theoretical standard deviation for phi - Note that
  68. % phi + 2 phistd and phi -2 phistd will give 95% confidence bands for phi
  69. % - only for err(1)>=1
  70. % Cerr (Jackknife error bars for C - use only for Jackknife - err(1)=2)
  71. if nargin < 3; error('Need data1 and data2 and window parameters'); end;
  72. if nargin < 4; params=[]; end;
  73. if length(params.tapers)==3 & movingwin(1)~=params.tapers(2);
  74. error('Duration of data in params.tapers is inconsistent with movingwin(1), modify params.tapers(2) to proceed')
  75. end
  76. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  77. if nargin < 5 || isempty(fscorr); fscorr=0; end;
  78. if nargout > 8 && err(1)==0;
  79. error('When errors are desired, err(1) has to be non-zero.');
  80. end;
  81. if nargout > 10 && err(1)~=2;
  82. error('Cerr computed only for Jackknife. Correct inputs and run again');
  83. end;
  84. [N,Ch]=check_consistency(data1,data2);
  85. Nwin=round(Fs*movingwin(1)); % number of samples in window
  86. Nstep=round(movingwin(2)*Fs); % number of samples to step through
  87. nfft=max(2^(nextpow2(Nwin)+pad),Nwin);
  88. f=getfgrid(Fs,nfft,fpass);
  89. Nf=length(f);
  90. params.tapers=dpsschk(tapers,Nwin,Fs); % check tapers
  91. winstart=1:Nstep:N-Nwin+1;
  92. nw=length(winstart);
  93. if trialave;
  94. C=zeros(nw,Nf);
  95. S12=zeros(nw,Nf);
  96. S1=zeros(nw,Nf);
  97. S2=zeros(nw,Nf);
  98. phi=zeros(nw,Nf);
  99. Cerr=zeros(2,nw,Nf);
  100. % phierr=zeros(2,nw,Nf);
  101. phistd=zeros(nw,Nf);
  102. else
  103. C=zeros(nw,Nf,Ch);
  104. S12=zeros(nw,Nf,Ch);
  105. S1=zeros(nw,Nf,Ch);
  106. S2=zeros(nw,Nf,Ch);
  107. phi=zeros(nw,Nf,Ch);
  108. Cerr=zeros(2,nw,Nf,Ch);
  109. % phierr=zeros(2,nw,Nf,Ch);
  110. phistd=zeros(nw,Nf,Ch);
  111. end;
  112. zerosp=zeros(nw,Ch);
  113. for n=1:nw;
  114. indx=winstart(n):winstart(n)+Nwin-1;
  115. datawin1=data1(indx,:);datawin2=data2(indx,:);
  116. if nargout==11;
  117. [c,ph,s12,s1,s2,f,zsp,confc,phie,cerr]=coherencypb(datawin1,datawin2,params,fscorr);
  118. % phierr(1,n,:,:)=squeeze(phie(1,:,:));
  119. % phierr(2,n,:,:)=squeeze(phie(2,:,:));
  120. phistd(n,:,:)=phie;
  121. Cerr(1,n,:,:)=squeeze(cerr(1,:,:));
  122. Cerr(2,n,:,:)=squeeze(cerr(2,:,:));
  123. elseif nargout==10;
  124. [c,ph,s12,s1,s2,f,zsp,confc,phie]=coherencypb(datawin1,datawin2,params,fscorr);
  125. % phierr(1,n,:,:)=squeeze(phie(1,:,:));
  126. % phierr(2,n,:,:)=squeeze(phie(2,:,:));
  127. phistd(n,:,:)=phie;
  128. else
  129. [c,ph,s12,s1,s2,f,zsp]=coherencycpb(datawin1,datawin2,params,fscorr);
  130. end;
  131. C(n,:,:)=c;
  132. phi(n,:,:)=ph;
  133. S12(n,:,:)=s12;
  134. S1(n,:,:)=s1;
  135. S2(n,:,:)=s2;
  136. zerosp(n,:)=zsp;
  137. end;
  138. C=squeeze(C); phi=squeeze(phi);S12=squeeze(S12); S1=squeeze(S1); S2=squeeze(S2);zerosp=squeeze(zerosp);
  139. if nargout > 9; confC=confc; end;
  140. if nargout==11;Cerr=squeeze(Cerr);end;
  141. % if nargout==10; phierr=squeeze(phierr);end
  142. if nargout==10; phistd=squeeze(phistd);end
  143. winmid=winstart+round(Nwin/2);
  144. t=winmid/Fs;