cohmatrixpb.m 4.2 KB

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