cohmatrixc.m 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. function [C,phi,S12,f,confC,phistd,Cerr]=cohmatrixc(data,params)
  2. % Multi-taper coherency,cross-spectral matrix - continuous process
  3. %
  4. % Usage:
  5. %
  6. % [C,phi,S12,f,confC,phistd,Cerr]=cohmatrixc(data,params)
  7. % Input:
  8. % Note units have to be consistent. See chronux.m for more information.
  9. % data (in form samples x channels) -- required
  10. % params: structure with fields tapers, pad, Fs, fpass, err
  11. % - optional
  12. % tapers : precalculated tapers from dpss or in the one of the following
  13. % forms:
  14. % (1) A numeric vector [TW K] where TW is the
  15. % time-bandwidth product and K is the number of
  16. % tapers to be used (less than or equal to
  17. % 2TW-1).
  18. % (2) A numeric vector [W T p] where W is the
  19. % bandwidth, T is the duration of the data and p
  20. % is an integer such that 2TW-p tapers are used. In
  21. % this form there is no default i.e. to specify
  22. % the bandwidth, you have to specify T and p as
  23. % well. Note that the units of W and T have to be
  24. % consistent: if W is in Hz, T must be in seconds
  25. % and vice versa. Note that these units must also
  26. % be consistent with the units of params.Fs: W can
  27. % be in Hz if and only if params.Fs is in Hz.
  28. % The default is to use form 1 with TW=3 and K=5
  29. %
  30. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  31. % -1 corresponds to no padding, 0 corresponds to padding
  32. % to the next highest power of 2 etc.
  33. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  34. % to 512 points, if pad=1, we pad to 1024 points etc.
  35. % Defaults to 0.
  36. % Fs (sampling frequency) - optional. Default 1.
  37. % fpass (frequency band to be used in the calculation in the form
  38. % [fmin fmax])- optional.
  39. % Default all frequencies between 0 and Fs/2
  40. % err (error calculation [1 p] - Theoretical error bars; [2 p] - Jackknife error bars
  41. % [0 p] or 0 - no error bars) - optional. Default 0.
  42. % Output:
  43. % C (magnitude of coherency frequency x channels x channels)
  44. % phi (phase of coherency frequency x channels x channels)
  45. % S12 (cross-spectral matrix frequency x channels x channels)
  46. % f (frequencies)
  47. % confC (confidence level for C at 1-p %) - only for err(1)>=1
  48. % phistd - theoretical/jackknife (depending on err(1)=1/err(1)=2) standard deviation for phi
  49. % Note that phi + 2 phistd and phi - 2 phistd will give 95% confidence
  50. % bands for phi - only for err(1)>=1
  51. % Cerr (Jackknife error bars for C - use only for Jackknife - err(1)=2)
  52. if nargin < 1; error('need data'); end;
  53. if nargin < 2; params=[]; end;
  54. [N,Ch]=size(data);
  55. if Ch==1; error('Need at least two channels of data'); end;
  56. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  57. clear trialave params
  58. if nargout > 6 && err(1)~=2;
  59. error('Cerr computed only for Jackknife. Correct inputs and run again');
  60. end;
  61. if nargout >= 4 && err(1)==0;
  62. % Errors computed only if err(1) is nonzero. Need to change params and run again.
  63. error('When errors are desired, err(1) has to be non-zero.');
  64. end;
  65. nfft=max(2^(nextpow2(N)+pad),N);
  66. [f,findx]=getfgrid(Fs,nfft,fpass);
  67. tapers=dpsschk(tapers,N,Fs); % check tapers
  68. J=mtfftc(data,tapers,nfft,Fs);
  69. J=J(findx,:,:);
  70. if err(1)==0;
  71. [C,phi,S12]=cohmathelper(J,err);
  72. elseif err(1)==1;
  73. [C,phi,S12,confC,phistd]=cohmathelper(J,err);
  74. elseif err(1)==2;
  75. [C,phi,S12,confC,phistd,Cerr]=cohmathelper(J,err);
  76. end