mtspectrumsegpb.m 5.0 KB

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