cohgramcpt.m 7.1 KB

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