cohgramcpb.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. function [C,phi,S12,S1,S2,t,f,zerosp,confC,phistd,Cerr]=cohgramcpb(data1,data2,movingwin,params,fscorr)
  2. % Multi-taper time-frequency coherence,cross-spectrum and individual spectra
  3. % continuous process and binned point process
  4. %
  5. % Usage:
  6. %
  7. % [C,phi,S12,S1,S2,t,f,zerosp,confC,phistd,Cerr]=cohgramcpb(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 (binned point process data in form samples x trials) -- required
  14. % movingwin (in the form [window winstep] -- required
  15. % params: structure with fields tapers, pad, Fs, fpass, err, trialave
  16. % - optional
  17. % tapers : precalculated tapers from dpss or in the one of the following
  18. % forms:
  19. % (1) A numeric vector [TW K] where TW is the
  20. % time-bandwidth product and K is the number of
  21. % tapers to be used (less than or equal to
  22. % 2TW-1).
  23. % (2) A numeric vector [W T p] where W is the
  24. % bandwidth, T is the duration of the data and p
  25. % is an integer such that 2TW-p tapers are used. In
  26. % this form there is no default i.e. to specify
  27. % the bandwidth, you have to specify T and p as
  28. % well. Note that the units of W and T have to be
  29. % consistent: if W is in Hz, T must be in seconds
  30. % and vice versa. Note that these units must also
  31. % be consistent with the units of params.Fs: W can
  32. % be in Hz if and only if params.Fs is in Hz.
  33. % The default is to use form 1 with TW=3 and K=5
  34. % Note that T has to be equal to movingwin(1).
  35. %
  36. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  37. % -1 corresponds to no padding, 0 corresponds to padding
  38. % to the next highest power of 2 etc.
  39. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  40. % to 512 points, if pad=1, we pad to 1024 points etc.
  41. % Defaults to 0.
  42. % Fs (sampling frequency) - optional. Default 1.
  43. % fpass (frequency band to be used in the calculation in the form
  44. % [fmin fmax])- optional.
  45. % Default all frequencies between 0 and Fs/2
  46. % err (error calculation [1 p] - Theoretical error bars; [2 p] - Jackknife error bars
  47. % [0 p] or 0 - no error bars) - optional. Default 0.
  48. % trialave (average over trials when 1, don't average when 0) - 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 no spikes were found, 0 otherwise: dimensions time x trials)
  66. % confC (confidence level for C at 1-p %) - only for err(1)>=1
  67. % phistd - theoretical/jackknife (depending on err(1)=1/err(1)=2) standard deviation for phi
  68. % Note that phi + 2 phistd and phi - 2 phistd will give 95% confidence
  69. % bands for phi - 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 < 3; error('Need data1 and data2 and window parameters'); end;
  73. if nargin < 4; params=[]; end;
  74. if length(params.tapers)==3 & movingwin(1)~=params.tapers(2);
  75. error('Duration of data in params.tapers is inconsistent with movingwin(1), modify params.tapers(2) to proceed')
  76. end
  77. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  78. if nargin < 5 || isempty(fscorr); fscorr=0; end;
  79. if nargout > 10 && err(1)~=2;
  80. error('Cerr computed only for Jackknife. Correct inputs and run again');
  81. end;
  82. if nargout > 8 && err(1)==0;
  83. % Errors computed only if err(1) is non-zero. Need to change params & run again.
  84. error('When errors are desired, err(1) has to be non-zero.');
  85. end;
  86. [N,Ch]=check_consistency(data1,data2);
  87. Nwin=round(Fs*movingwin(1)); % number of samples in window
  88. Nstep=round(movingwin(2)*Fs); % number of samples to step through
  89. nfft=max(2^(nextpow2(Nwin)+pad),Nwin);
  90. f=getfgrid(Fs,nfft,fpass);
  91. 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. datawin1=data1(indx,:);datawin2=data2(indx,:);
  118. if nargout==11;
  119. [c,ph,s12,s1,s2,f,zsp,confc,phie,cerr]=coherencycpb(datawin1,datawin2,params,fscorr);
  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]=coherencycpb(datawin1,datawin2,params,fscorr);
  127. % phierr(1,n,:,:)=squeeze(phie(1,:,:));
  128. % phierr(2,n,:,:)=squeeze(phie(2,:,:));
  129. phistd(n,:,:)=phie;
  130. else
  131. [c,s12,s1,s2,ph,f,zsp]=coherencycpb(datawin1,datawin2,params,fscorr);
  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. C=squeeze(C); phi=squeeze(phi); S12=squeeze(S12); S1=squeeze(S1); S2=squeeze(S2); zerosp=squeeze(zerosp);
  141. if nargout > 9 ; confC=confc; end;
  142. if nargout==11;Cerr=squeeze(Cerr);end;
  143. %if nargout>=10; phierr=squeeze(phierr);end
  144. if nargout>=10; phistd=squeeze(phistd);end
  145. winmid=winstart+round(Nwin/2);
  146. t=winmid/Fs;