cohgrampt.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. function [C,phi,S12,S1,S2,t,f,zerosp,confC,phistd,Cerr]=cohgrampt(data1,data2,movingwin,params,fscorr)
  2. % Multi-taper time-frequency coherence - two point processes given as times
  3. %
  4. % Usage:
  5. %
  6. % [C,phi,S12,S1,S2,t,f,zerosp,confC,phistd,Cerr]=cohgrampt(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 (structure array of spike times with dimension trials; also accepts 1d array of spike times) -- required
  12. % data2 (structure array of spike times with dimension trials; also accepts 1d array of spike times) -- 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) - optional. Default 0
  48. % fscorr (finite size corrections, 0 (don't use finite size corrections) or
  49. % 1 (use finite size corrections) - optional
  50. % (available only for spikes). Defaults 0.
  51. % Output:
  52. % C (magnitude of coherency time x frequencies x trials for trialave=0;
  53. % time x frequency for trialave=1)
  54. % phi (phase of coherency time x frequencies x trials for no trial averaging;
  55. % time x frequency for trialave=1)
  56. % S12 (cross spectrum - time x frequencies x trials for no trial averaging;
  57. % time x frequency for trialave=1)
  58. % S1 (spectrum 1 - time x frequencies x trials for no trial averaging;
  59. % time x frequency for trialave=1)
  60. % S2 (spectrum 2 - time x frequencies x trials for no trial averaging;
  61. % time x frequency for trialave=1)
  62. % t (time)
  63. % f (frequencies)
  64. % zerosp (1 for windows and trials where spikes were absent (in either channel),zero otherwise)
  65. % confC (confidence level for C at 1-p %) - only for err(1)>=1
  66. % phistd - theoretical/jackknife (depending on err(1)=1/err(1)=2) standard deviation for phi
  67. % Note that phi + 2 phistd and phi - 2 phistd will give 95% confidence
  68. % bands for phi - only for err(1)>=1
  69. % Cerr (Jackknife error bars for C - use only for Jackknife - err(1)=2)
  70. if nargin < 3; error('Need data1 and data2 and window parameters'); end;
  71. if nargin < 4; params=[]; end;
  72. if length(params.tapers)==3 & movingwin(1)~=params.tapers(2);
  73. error('Duration of data in params.tapers is inconsistent with movingwin(1), modify params.tapers(2) to proceed')
  74. end
  75. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  76. if nargin < 5 || isempty(fscorr); fscorr=0; end;
  77. if nargout > 10 && err(1)~=2;
  78. error('Cerr computed only for Jackknife. Correct inputs and run again');
  79. end;
  80. if nargout > 8 && err(1)==0;
  81. error('Errors computed only if err(1) is not equal to zero');
  82. end;
  83. [N,Ch]=check_consistency(data1,data2);
  84. [mintime1,maxtime1]=minmaxsptimes(data1);
  85. [mintime2,maxtime2]=minmaxsptimes(data2);
  86. mintime=min(mintime1,mintime2);
  87. maxtime=max(maxtime1,maxtime2);
  88. tn=mintime+movingwin(1)/2:movingwin(2):maxtime-movingwin(1)/2;
  89. Nwin=round(Fs*movingwin(1)); % number of samples in window
  90. % Nstep=round(movingwin(2)*Fs); % number of samples to step through
  91. nfft=max(2^(nextpow2(Nwin)+pad),Nwin);
  92. f=getfgrid(Fs,nfft,fpass); Nf=length(f);
  93. params.tapers=dpsschk(tapers,Nwin,Fs); % check tapers
  94. nw=length(tn);
  95. if trialave;
  96. C=zeros(nw,Nf);
  97. S12=zeros(nw,Nf);
  98. S1=zeros(nw,Nf);
  99. S2=zeros(nw,Nf);
  100. phi=zeros(nw,Nf);
  101. Cerr=zeros(2,nw,Nf);
  102. % phierr=zeros(2,nw,Nf);
  103. phistd=zeros(nw,Nf);
  104. else
  105. C=zeros(nw,Nf,Ch);
  106. S12=zeros(nw,Nf,Ch);
  107. S1=zeros(nw,Nf,Ch);
  108. S2=zeros(nw,Nf,Ch);
  109. phi=zeros(nw,Nf,Ch);
  110. Cerr=zeros(2,nw,Nf,Ch);
  111. % phierr=zeros(2,nw,Nf,Ch);
  112. phistd=zeros(nw,Nf,Ch);
  113. end;
  114. zerosp=zeros(nw,Ch);
  115. for n=1:nw;
  116. t=linspace(tn(n)-movingwin(1)/2,tn(n)+movingwin(1)/2,Nwin);
  117. datawin1=extractdatapt(data1,[t(1) t(end)]);datawin2=extractdatapt(data2,[t(1) t(end)]);
  118. if nargout==11;
  119. [c,ph,s12,s1,s2,f,zsp,confc,phie,cerr]=coherencypt(datawin1,datawin2,params,fscorr,t);
  120. % phierr(1,n,:,:)=squeeze(phie(1,:,:));
  121. % phierr(2,n,:,:)=squeeze(phie(2,:,:));
  122. phistd(n,:,:)=phie;
  123. Cerr(1,n,:,:)=squeeze(cerr(1,:,:));
  124. Cerr(2,n,:,:)=squeeze(cerr(2,:,:));
  125. elseif nargout==10;
  126. [c,ph,s12,s1,s2,f,zsp,confc,phie]=coherencypt(datawin1,datawin2,params,fscorr,t);
  127. % phierr(1,n,:,:)=squeeze(phie(1,:,:));
  128. % phierr(2,n,:,:)=squeeze(phie(2,:,:));
  129. phistd(n,:,:)=phie;
  130. else
  131. [c,ph,s12,s1,s2,f,zsp]=coherencypt(datawin1,datawin2,params,fscorr,t);
  132. end;
  133. C(n,:,:)=c;
  134. phi(n,:,:)=ph;
  135. S12(n,:,:)=s12;
  136. S1(n,:,:)=s1;
  137. S2(n,:,:)=s2;
  138. zerosp(n,:)=zsp;
  139. end;
  140. t=tn;
  141. C=squeeze(C); phi=squeeze(phi);S12=squeeze(S12); S1=squeeze(S1); S2=squeeze(S2);zerosp=squeeze(zerosp);
  142. if nargout > 9; confC=confc; end;
  143. if nargout==11;Cerr=squeeze(Cerr);end;
  144. % if nargout==10; phierr=squeeze(phierr);end
  145. if nargout==10; phistd=squeeze(phistd);end