mtspecgrampb.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. function [S,t,f,R,Serr]=mtspecgrampb(data,movingwin,params,fscorr)
  2. % Multi-taper time-frequency spectrum - binned point process
  3. %
  4. % Usage:
  5. %
  6. % [S,t,f,R,Serr]=mtspecgrampb(data,movingwin,params,fscorr)
  7. % Input:
  8. % data (in form samples x channels/trials or single vector) -- required
  9. % movingwin (in the form [window,winstep] i.e length of moving
  10. % window and step size.
  11. %
  12. % params: structure with fields tapers, pad, Fs, fpass, err, trialave
  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. % Note that T has to be equal to movingwin(1).
  32. %
  33. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  34. % -1 corresponds to no padding, 0 corresponds to padding
  35. % to the next highest power of 2 etc.
  36. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  37. % to 512 points, if pad=1, we pad to 1024 points etc.
  38. % Defaults to 0.
  39. % Fs (sampling frequency) - optional. Default 1.
  40. % fpass (frequency band to be used in the calculation in the form
  41. % [fmin fmax])- optional.
  42. % Default all frequencies between 0 and Fs/2
  43. % err (error calculation [1 p] - Theoretical error bars; [2 p] - Jackknife error bars
  44. % [0 p] or 0 - no error bars) - optional. Default 0.
  45. % trialave (average over trials/channnels when 1, don't average when 0) - optional. Default 0
  46. % fscorr (finite size corrections, 0 (don't use finite size corrections) or
  47. % 1 (use finite size corrections) - optional
  48. % (available only for spikes). Defaults 0.
  49. % Output:
  50. % S (spectrum in form time x frequency x channels/trials for trialave=0;
  51. % or as a function of frequency if trialave=1)
  52. % t (times)
  53. % f (frequencies)
  54. % R (rate)
  55. % Serr (error bars) - only for err(1)>=1
  56. if nargin < 2; error('Need data and window parameters'); end;
  57. if nargin < 3; params=[]; end;
  58. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  59. if length(params.tapers)==3 & movingwin(1)~=params.tapers(2);
  60. error('Duration of data in params.tapers is inconsistent with movingwin(1), modify params.tapers(2) to proceed')
  61. end
  62. if nargin < 4 || isempty(fscorr); fscorr=0; end;
  63. if nargout > 4 && err(1)==0;
  64. % error('Cannot compute errors with err(1)=0');
  65. error('When Serr is desired, err(1) has to be non-zero.');
  66. end;
  67. data=change_row_to_column(data);
  68. [N,Ch]=size(data);
  69. Nwin=round(Fs*movingwin(1)); % number of samples in window
  70. Nstep=round(movingwin(2)*Fs); % number of samples to step through
  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. winstart=1:Nstep:N-Nwin+1;
  75. nw=length(winstart);
  76. if trialave
  77. S = zeros(nw,Nf);
  78. R = zeros(nw,Nwin);
  79. if nargout==4; Serr=zeros(2,nw,Nf); end;
  80. else
  81. S = zeros(nw,Nf,Ch);
  82. R = zeros(nw,Nwin,Ch);
  83. if nargout==4; Serr=zeros(2,nw,Nf,Ch); end;
  84. end
  85. for n=1:nw;
  86. indx=winstart(n):winstart(n)+Nwin-1;
  87. datawin=data(indx,:);
  88. if nargout==5;
  89. [s,f,r,serr]=mtspectrumpb(datawin,params,fscorr);
  90. Serr(1,n,:,:)=squeeze(serr(1,:,:));
  91. Serr(2,n,:,:)=squeeze(serr(2,:,:));
  92. else
  93. [s,f,r]=mtspectrumpb(datawin,params,fscorr);
  94. end;
  95. S(n,:,:)=s;
  96. R(n,:)=r';
  97. end;
  98. winmid=winstart+round(Nwin/2);
  99. t=winmid/Fs;
  100. S=squeeze(S); R=squeeze(R); if nargout==5; Serr=squeeze(Serr);end