mtspectrumpb.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. function [S,f,R,Serr]=mtspectrumpb(data,params,fscorr)
  2. % Multi-taper spectrum - binned point process
  3. %
  4. % Usage:
  5. %
  6. % [S,f,R,Serr]=mtspectrumpb(data,params,fscorr)
  7. % Input:
  8. % data (in form samples x channels/trials or a single vector) -- required
  9. % params: structure with fields tapers, pad, Fs, fpass, err, trialave
  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. % trialave (average over channels/trials when 1, don't average when 0) - optional. Default 0
  42. % fscorr (finite size corrections, 0 (don't use finite size corrections) or
  43. % 1 (use finite size corrections) - optional
  44. % (available only for spikes). Defaults 0.
  45. % Output:
  46. % S (spectrum in form frequency x channels/trials if trialave=0;
  47. % as a function of frequency if trialave=1)
  48. % f (frequencies)
  49. % R (spike rate)
  50. % Serr (error bars) - only for err(1)>=1
  51. if nargin < 1; error('Need data'); end;
  52. if nargin < 2; params=[]; end;
  53. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  54. clear params
  55. if nargout > 3 && err(1)==0;
  56. % Cannot compute error bars with err(1)=0. Need to change params and run again
  57. error('When Serr is desired, err(1) has to be non-zero.');
  58. end;
  59. if nargin < 3 || isempty(fscorr); fscorr=0;end;
  60. data=change_row_to_column(data);
  61. N=size(data,1);
  62. nfft=max(2^(nextpow2(N)+pad),N);
  63. [f,findx]=getfgrid(Fs,nfft,fpass);
  64. tapers=dpsschk(tapers,N,Fs); % check tapers
  65. [J,Msp,Nsp]=mtfftpb(data,tapers,nfft);
  66. J=J(findx,:,:);
  67. S=squeeze(mean(conj(J).*J,2));
  68. if trialave; S=squeeze(mean(S,2)); Msp=mean(Msp);end;
  69. R=Msp*Fs;
  70. if nargout==4;
  71. if fscorr==1;
  72. Serr=specerr(S,J,err,trialave,Nsp);
  73. else
  74. Serr=specerr(S,J,err,trialave);
  75. end;
  76. end;