cohgramc.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. function [C,phi,S12,S1,S2,t,f,confC,phistd,Cerr]=cohgramc(data1,data2,movingwin,params)
  2. % Multi-taper time-frequency coherence,cross-spectrum and individual spectra - continuous processes
  3. %
  4. % Usage:
  5. %
  6. % [C,phi,S12,S1,S2,t,f,confC,phistd,Cerr]=cohgramc(data1,data2,movingwin,params)
  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 (in form samples x trials) -- required
  12. % data2 (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) - optional. Default 0
  48. % Output:
  49. % C (magnitude of coherency time x frequencies x trials for trialave=0;
  50. % time x frequency for trialave=1)
  51. % phi (phase of coherency time x frequencies x trials for no trial averaging;
  52. % time x frequency for trialave=1)
  53. % S12 (cross spectrum - time x frequencies x trials for no trial averaging;
  54. % time x frequency for trialave=1)
  55. % S1 (spectrum 1 - time x frequencies x trials for no trial averaging;
  56. % time x frequency for trialave=1)
  57. % S2 (spectrum 2 - time x frequencies x trials for no trial averaging;
  58. % time x frequency for trialave=1)
  59. % t (time)
  60. % f (frequencies)
  61. % confC (confidence level for C at 1-p %) - only for err(1)>=1
  62. % phistd - theoretical/jackknife (depending on err(1)=1/err(1)=2) standard deviation for phi
  63. % Note that phi + 2 phistd and phi - 2 phistd will give 95% confidence
  64. % bands for phi - only for err(1)>=1
  65. % Cerr (Jackknife error bars for C - use only for Jackknife - err(1)=2)
  66. if nargin < 3; error('Need data1 and data2 and window parameters'); end;
  67. if nargin < 4; params=[];end;
  68. if ~isempty(params) && length(params.tapers)==3 && movingwin(1)~=params.tapers(2);
  69. error('Duration of data in params.tapers is inconsistent with movingwin(1), modify params.tapers(2) to proceed')
  70. end
  71. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  72. if nargout > 9 && err(1)~=2;
  73. error('Cerr computed only for Jackknife. Correct inputs and run again');
  74. end;
  75. if nargout > 7 && err(1)==0;
  76. % Errors computed only if err(1) is nonzero. Need to change params and run again.
  77. error('When errors are desired, err(1) has to be non-zero.');
  78. end;
  79. [N,Ch]=check_consistency(data1,data2);
  80. Nwin=round(Fs*movingwin(1)); % number of samples in window
  81. Nstep=round(movingwin(2)*Fs); % number of samples to step through
  82. nfft=max(2^(nextpow2(Nwin)+pad),Nwin);
  83. f=getfgrid(Fs,nfft,fpass);
  84. Nf=length(f);
  85. params.tapers=dpsschk(tapers,Nwin,Fs); % check tapers
  86. winstart=1:Nstep:N-Nwin+1;
  87. nw=length(winstart);
  88. if trialave;
  89. C=zeros(nw,Nf);
  90. S12=zeros(nw,Nf);
  91. S1=zeros(nw,Nf);
  92. S2=zeros(nw,Nf);
  93. phi=zeros(nw,Nf);
  94. Cerr=zeros(2,nw,Nf);
  95. % phierr=zeros(2,nw,Nf);
  96. phistd=zeros(nw,Nf);
  97. else
  98. C=zeros(nw,Nf,Ch);
  99. S12=zeros(nw,Nf,Ch);
  100. S1=zeros(nw,Nf,Ch);
  101. S2=zeros(nw,Nf,Ch);
  102. phi=zeros(nw,Nf,Ch);
  103. Cerr=zeros(2,nw,Nf,Ch);
  104. % phierr=zeros(2,nw,Nf,Ch);
  105. phistd=zeros(nw,Nf,Ch);
  106. end;
  107. for n=1:nw;
  108. indx=winstart(n):winstart(n)+Nwin-1;
  109. datawin1=data1(indx,:);datawin2=data2(indx,:);
  110. if nargout==10;
  111. [c,ph,s12,s1,s2,f,confc,phie,cerr]=coherencyc(datawin1,datawin2,params);
  112. % phierr(1,n,:,:)=squeeze(phie(1,:,:));
  113. % phierr(2,n,:,:)=squeeze(phie(2,:,:));
  114. phistd(n,:,:)=phie;
  115. Cerr(1,n,:,:)=squeeze(cerr(1,:,:));
  116. Cerr(2,n,:,:)=squeeze(cerr(2,:,:));
  117. elseif nargout==9;
  118. [c,ph,s12,s1,s2,f,confc,phie]=coherencyc(datawin1,datawin2,params);
  119. % phierr(1,n,:,:)=squeeze(phie(1,:,:));
  120. % phierr(2,n,:,:)=squeeze(phie(2,:,:));
  121. phistd(n,:,:)=phie;
  122. else
  123. [c,ph,s12,s1,s2,f]=coherencyc(datawin1,datawin2,params);
  124. end;
  125. C(n,:,:)=c;
  126. S12(n,:,:)=s12;
  127. S1(n,:,:)=s1;
  128. S2(n,:,:)=s2;
  129. phi(n,:,:)=ph;
  130. end;
  131. C=squeeze(C); phi=squeeze(phi);S12=squeeze(S12); S1=squeeze(S1); S2=squeeze(S2);
  132. if nargout > 8; confC=confc; end;
  133. if nargout==10;Cerr=squeeze(Cerr);end;
  134. % if nargout>=9; phierr=squeeze(phierr);end
  135. if nargout>=9; phistd=squeeze(phistd);end
  136. winmid=winstart+round(Nwin/2);
  137. t=winmid/Fs;