mtspectrumsegc.m 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function [S,f,varS,C,Serr]=mtspectrumsegc(data,win,params,segave)
  2. % Multi-taper segmented spectrum for a univariate continuous process
  3. %
  4. % Usage:
  5. %
  6. % [S,f,varS,C,Serr]=mtspectrumsegc(data,win,params,segave)
  7. % Input:
  8. % Note units have to be consistent. See chronux.m for more information.
  9. % data (single channel) -- required
  10. % win (duration of the segments) - required.
  11. % params: structure with fields tapers, pad, Fs, fpass, err, trialave
  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. % trialave - not used
  44. % segave - optional 0 for don't average over segments, 1 for average - default
  45. % 1
  46. % Output:
  47. % S (spectrum in form frequency x segments if segave=0; in the form frequency if segave=1)
  48. % f (frequencies)
  49. % varS (variance of the log spectrum)
  50. % C (covariance matrix of the log spectrum - frequency x
  51. % frequency matrix)
  52. % Serr (error bars) only for err(1)>=1
  53. if nargin < 2; error('Need data and segment information'); end;
  54. data=change_row_to_column(data);
  55. if size(data,2)~=1; error('works for only univariate time series'); end;
  56. if nargin < 3 ; params=[]; end;
  57. if nargin < 4 || isempty(segave); segave=1; end;
  58. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params); clear trialave params
  59. if nargout==4 && err(1)==0;
  60. % Errors can't be computed if err(1)=0. Need to change params and run again.
  61. error('When Serr is desired, err(1) has to be non-zero.');
  62. end;
  63. N=size(data,1); % length of segmented data
  64. dt=1/Fs; % sampling interval
  65. T=N*dt; % length of data in seconds
  66. E=0:win:T-win; % fictitious event triggers
  67. win=[0 win]; % use window length to define left and right limits of windows around triggers
  68. data=createdatamatc(data,E,Fs,win); % segmented data
  69. N=size(data,1); % length of segmented data
  70. nfft=max(2^(nextpow2(N)+pad),N);
  71. [f,findx]=getfgrid(Fs,nfft,fpass);
  72. tapers=dpsschk(tapers,N,Fs); % check tapers
  73. J=mtfftc(data,tapers,nfft,Fs); % compute tapered fourier transforms
  74. J=J(findx,:,:); % restrict to specified frequencies
  75. S=squeeze(mean(conj(J).*J,2)); % spectra of non-overlapping segments (average over tapers)
  76. if segave==1; SS=squeeze(mean(S,2)); else; SS=S;end; % mean of the spectrum averaged across segments
  77. if nargout > 2
  78. lS=log(S); % log spectrum for nonoverlapping segments
  79. varS=var(lS',1)'; % variance of log spectrum
  80. % varS=var(lS',1)';% variance of the log spectrum R13
  81. if nargout > 3
  82. C=cov(lS'); % covariance matrix of the log spectrum
  83. if nargout==5;
  84. Serr=specerr(SS,J,err,segave);
  85. end;
  86. end;
  87. end;
  88. S=SS;