mtspectrumsegpt.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. function [S,f,R,varS,zerosp,C,Serr]=mtspectrumsegpt(data,win,params,segave,fscorr)
  2. % Multi-taper segmented spectrum for a univariate binned point process
  3. %
  4. % Usage:
  5. %
  6. % [S,f,R,varS,zerosp,C,Serr]=mtspectrumsegpt(data,win,params,segave,fscorr)
  7. % Input:
  8. % Note units have to be consistent. See chronux.m for more information.
  9. % data (structure array of one channel of spike times;
  10. % also accepts 1d vector of spike times) -- required
  11. % win (duration of the segments) - required.
  12. % params: structure with fields tapers, pad, Fs, fpass, err
  13. % - optional
  14. % tapers : precalculated tapers from dpss or in the one of the following
  15. % forms:
  16. % (1) A numeric vector [TW K] where TW is the
  17. % time-bandwidth product and K is the number of
  18. % tapers to be used (less than or equal to
  19. % 2TW-1).
  20. % (2) A numeric vector [W T p] where W is the
  21. % bandwidth, T is the duration of the data and p
  22. % is an integer such that 2TW-p tapers are used. In
  23. % this form there is no default i.e. to specify
  24. % the bandwidth, you have to specify T and p as
  25. % well. Note that the units of W and T have to be
  26. % consistent: if W is in Hz, T must be in seconds
  27. % and vice versa. Note that these units must also
  28. % be consistent with the units of params.Fs: W can
  29. % be in Hz if and only if params.Fs is in Hz.
  30. % The default is to use form 1 with TW=3 and K=5
  31. %
  32. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  33. % -1 corresponds to no padding, 0 corresponds to padding
  34. % to the next highest power of 2 etc.
  35. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  36. % to 512 points, if pad=1, we pad to 1024 points etc.
  37. % Defaults to 0.
  38. % Fs (sampling frequency) - optional. Default 1.
  39. % fpass (frequency band to be used in the calculation in the form
  40. % [fmin fmax])- optional.
  41. % Default all frequencies between 0 and Fs/2
  42. % err (error calculation [1 p] - Theoretical error bars; [2 p] - Jackknife error bars
  43. % [0 p] or 0 - no error bars) - optional. Default 0.
  44. % segave - (0 for don't average over segments, 1 for average) - optional - default 1
  45. % fscorr (finite size corrections, 0 (don't use finite size corrections) or
  46. % 1 (use finite size corrections) - optional
  47. % (available only for spikes). Defaults 0.
  48. % Output:
  49. % S (spectrum in form frequency x segments if segave=0; function of frequency if segave=1)
  50. % f (frequencies)
  51. % R (spike rate)
  52. % varS (variance of the spectrum as a function of frequency)
  53. % zerosp (0 for segments in which spikes were found, 1 for segments
  54. % C (covariance matrix of the log spectrum - frequency x
  55. % frequency matrix)
  56. % Serr (error bars) - only if err(1)>=1
  57. if nargin < 2; error('Need data and segment information'); end;
  58. if nargin < 3; params=[]; end;
  59. if nargin < 4 || isempty(segave); segave=1; end;
  60. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  61. clear params trialave
  62. if nargin < 5 || isempty(fscorr); fscorr=0;end;
  63. if nargout > 4 && err(1)==0; error('cannot compute error bars with err(1)=0; change params and run again'); end;
  64. dtmp=change_row_to_column(data);
  65. T=max(dtmp); % total length of data
  66. minT=min(dtmp);
  67. E=minT:win:T-win; % fictitious event triggers
  68. win=[0 win]; % use window length to define left and right limits of windows around triggers
  69. dtmp=createdatamatpt(dtmp,E,win); % create segmented data set
  70. [mintime,maxtime]=minmaxsptimes(dtmp);
  71. dt=1/Fs; % sampling time
  72. t=mintime-dt:dt:maxtime+dt; % time grid for prolates
  73. N=length(t); % number of points in grid for dpss
  74. nfft=max(2^(nextpow2(N)+pad),N); % number of points in fft of prolates
  75. [f,findx]=getfgrid(Fs,nfft,fpass); % get frequency grid for evaluation
  76. tapers=dpsschk(tapers,N,Fs); % check tapers
  77. [J,Msp,Nsp]=mtfftpt(dtmp,tapers,nfft,t,f,findx);% mt fft for point process times
  78. R=Msp*Fs;
  79. S=squeeze(mean(conj(J).*J,2)); % spectra of non-overlapping segments (averaged over tapers)
  80. if segave==1; SS=squeeze(mean(S,2));R=mean(R);else;SS=S;end;% mean of the spectrum averaged across segments
  81. if nargout > 3
  82. lS=log(SS); % log spectrum for nonoverlapping segments
  83. % varS=var(lS,1,2); % variance of log spectrum
  84. varS=var(lS',1)';% variance of the log spectrum R13
  85. if nargout > 4
  86. zerosp=zeros(1,size(data,2));
  87. zerosp(Nsp==0)=1;
  88. if nargout > 5
  89. C=cov(lS'); % covariance matrix of the log spectrum
  90. if nargout==7;
  91. if fscorr==1;
  92. Serr=specerr(SS,J,err,segave,Nsp);
  93. else
  94. Serr=specerr(SS,J,err,segave);
  95. end;
  96. end;
  97. end;
  98. end;
  99. end;
  100. S=SS;