mtspecgrampt.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. function [S,t,f,R,Serr]=mtspecgrampt(data,movingwin,params,fscorr)
  2. % Multi-taper time-frequency spectrum - point process times
  3. %
  4. % Usage:
  5. %
  6. % [S,t,f,R,Serr]=mtspecgrampt(data,movingwin,params,fscorr)
  7. % Input:
  8. % data (structure array of spike times with dimension channels/trials;
  9. % also accepts 1d array of spike times) -- required
  10. % movingwin (in the form [window,winstep] i.e length of moving
  11. % window and step size.
  12. %
  13. % params: structure with fields tapers, pad, Fs, fpass, err, trialave
  14. % - optional
  15. % tapers : precalculated tapers from dpss or in the one of the following
  16. % forms:
  17. % (1) A numeric vector [TW K] where TW is the
  18. % time-bandwidth product and K is the number of
  19. % tapers to be used (less than or equal to
  20. % 2TW-1).
  21. % (2) A numeric vector [W T p] where W is the
  22. % bandwidth, T is the duration of the data and p
  23. % is an integer such that 2TW-p tapers are used. In
  24. % this form there is no default i.e. to specify
  25. % the bandwidth, you have to specify T and p as
  26. % well. Note that the units of W and T have to be
  27. % consistent: if W is in Hz, T must be in seconds
  28. % and vice versa. Note that these units must also
  29. % be consistent with the units of params.Fs: W can
  30. % be in Hz if and only if params.Fs is in Hz.
  31. % The default is to use form 1 with TW=3 and K=5
  32. % Note that T has to be equal to movingwin(1).
  33. %
  34. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  35. % -1 corresponds to no padding, 0 corresponds to padding
  36. % to the next highest power of 2 etc.
  37. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  38. % to 512 points, if pad=1, we pad to 1024 points etc.
  39. % Defaults to 0.
  40. % Fs (sampling frequency) - optional. Default 1.
  41. % fpass (frequency band to be used in the calculation in the form
  42. % [fmin fmax])- optional.
  43. % Default all frequencies between 0 and Fs/2
  44. % err (error calculation [1 p] - Theoretical error bars; [2 p] - Jackknife error bars
  45. % [0 p] or 0 - no error bars) - optional. Default 0.
  46. % trialave (average over trials/channels when 1, don't average when 0) - optional. Default 0
  47. % fscorr (finite size corrections, 0 (don't use finite size corrections) or
  48. % 1 (use finite size corrections) - optional
  49. % (available only for spikes). Defaults 0.
  50. %
  51. % Output:
  52. % S (spectrogram with dimensions time x frequency x channels/trials if trialave=0;
  53. % dimensions time x frequency if trialave=1)
  54. % t (times)
  55. % f (frequencies)
  56. %
  57. % Serr (error bars) - only if err(1)>=1
  58. if nargin < 2; error('Need data and window parameters'); end;
  59. if nargin < 3; params=[]; end;
  60. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  61. if length(params.tapers)==3 & movingwin(1)~=params.tapers(2);
  62. error('Duration of data in params.tapers is inconsistent with movingwin(1), modify params.tapers(2) to proceed')
  63. end
  64. data=change_row_to_column(data);
  65. if isstruct(data); Ch=length(data); end;
  66. if nargin < 4 || isempty(fscorr); fscorr=0; end;
  67. if nargout > 4 && err(1)==0; error('Cannot compute errors with err(1)=0'); end;
  68. [mintime,maxtime]=minmaxsptimes(data);
  69. tn=(mintime+movingwin(1)/2:movingwin(2):maxtime-movingwin(1)/2);
  70. Nwin=round(Fs*movingwin(1)); % number of samples in window
  71. nfft=max(2^(nextpow2(Nwin)+pad),Nwin);
  72. f=getfgrid(Fs,nfft,fpass); Nf=length(f);
  73. params.tapers=dpsschk(tapers,Nwin,Fs); % check tapers
  74. nw=length(tn);
  75. if trialave
  76. S = zeros(nw,Nf);
  77. R = zeros(nw,1);
  78. if nargout==4; Serr=zeros(2,nw,Nf); end;
  79. else
  80. S = zeros(nw,Nf,Ch);
  81. R = zeros(nw,Ch);
  82. if nargout==4; Serr=zeros(2,nw,Nf,Ch); end;
  83. end
  84. for n=1:nw;
  85. t=linspace(tn(n)-movingwin(1)/2,tn(n)+movingwin(1)/2,Nwin);
  86. datawin=extractdatapt(data,[t(1) t(end)]);
  87. if nargout==5;
  88. [s,f,r,serr]=mtspectrumpt(datawin,params,fscorr,t);
  89. Serr(1,n,:,:)=squeeze(serr(1,:,:));
  90. Serr(2,n,:,:)=squeeze(serr(2,:,:));
  91. else
  92. [s,f,r]=mtspectrumpt(datawin,params,fscorr,t);
  93. end;
  94. S(n,:,:)=s;
  95. R(n,:)=r;
  96. end;
  97. t=tn;
  98. S=squeeze(S); R=squeeze(R); if nargout==5; Serr=squeeze(Serr);end