cohmatrixpt.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. function [C,phi,S12,f,zerosp,confC,phistd,Cerr]=cohmatrixpt(data,params,fscorr)
  2. % Multi-taper coherency matrix - point process times
  3. %
  4. % Usage:
  5. %
  6. % [C,phi,S12,f,zerosp,confC,phistd,Cerr]=cohmatrixpt(data,params,fscorr)
  7. % Input:
  8. % data (structure array of spike times with dimension 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 - theoretical/jackknife (depending on err(1)=1/err(1)=2) standard deviation for phi
  52. % Note that phi + 2 phistd and phi - 2 phistd will give 95% confidence
  53. % bands for phi - only for err(1)>=1
  54. % Cerr (Jackknife error bars for C - use only for Jackknife - err(1)=2)
  55. if isstruct(data)
  56. Ch=length(data);
  57. if Ch==1; error('Need at least 2 channels'); end;
  58. else
  59. error('Need at least two channels of data in the a structural array');
  60. end;
  61. if nargin < 2; params=[]; end;
  62. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  63. clear trialave params
  64. if nargin < 3 || isempty(fscorr); fscorr=0; end;
  65. [mintime,maxtime]=minmaxsptimes(data);
  66. dt=1/Fs; % sampling time
  67. t=mintime:dt:maxtime+dt; % time grid for prolates
  68. N=length(t); % number of points in grid for dpss
  69. nfft=max(2^(nextpow2(N)+pad),N); % number of points in fft of prolates
  70. [f,findx]=getfgrid(Fs,nfft,fpass); % get frequency grid for evaluation
  71. tapers=dpsschk(tapers,N,Fs); % check tapers
  72. [J,Msp,Nsp]=mtfftpt(data,tapers,nfft,t,f,findx); % mt fft for point process times
  73. C1=size(J,3);
  74. zerosp=zeros(1,C1); % initialize the zerosp variable
  75. zerosp(Nsp==0)=0; % set the zerosp variable
  76. if err(1)==0;
  77. [C,phi,S12]=cohmathelper(J,err);
  78. elseif err(1)==1;
  79. if fscorr==0;
  80. [C,phi,S12,confC,phistd]=cohmathelper(J,err);
  81. else
  82. [C,phi,S12,confC,phistd]=cohmathelper(J,err,Nsp);
  83. end;
  84. elseif err(1)==2;
  85. if fscorr==0;
  86. [C,phi,S12,confC,phistd,Cerr]=cohmathelper(J,err);
  87. else
  88. [C,phi,S12,confC,phistd,Cerr]=cohmathelper(J,err,Nsp);
  89. end;
  90. end
  91. clear Msp