


Multi-taper segmented spectrum for a univariate continuous process
Usage:
[S,f,varS,C,Serr]=mtspectrumsegc(data,win,params,segave)
Input:
Note units have to be consistent. See chronux.m for more information.
data (single channel) -- required
win (duration of the segments) - required.
params: structure with fields tapers, pad, Fs, fpass, err, trialave
- optional
tapers (precalculated tapers from dpss, or in the form [NW K] e.g [3 5]) -- optional. If not
specified, use [NW K]=[3 5]
pad (padding factor for the FFT) - optional. Defaults to 0.
e.g. For N = 500, if PAD = 0, we pad the FFT
to 512 points; if PAD = 2, we pad the FFT
to 2048 points, etc.
Fs (sampling frequency) - optional. Default 1.
fpass (frequency band to be used in the calculation in the form
[fmin fmax])- optional.
Default all frequencies between 0 and Fs/2
err (error calculation [1 p] - Theoretical error bars; [2 p] - Jackknife error bars
[0 p] or 0 - no error bars) - optional. Default 0.
trialave - not used
segave - optional 0 for don't average over segments, 1 for average - default
1
Output:
S (spectrum in form frequency x segments if segave=0; in the form frequency if segave=1)
f (frequencies)
varS (variance of the log spectrum)
C (covariance matrix of the log spectrum - frequency x
frequency matrix)
Serr (error bars) only for err(1)>=1

0001 function [S,f,varS,C,Serr]=mtspectrumsegc(data,win,params,segave) 0002 % Multi-taper segmented spectrum for a univariate continuous process 0003 % 0004 % Usage: 0005 % 0006 % [S,f,varS,C,Serr]=mtspectrumsegc(data,win,params,segave) 0007 % Input: 0008 % Note units have to be consistent. See chronux.m for more information. 0009 % data (single channel) -- required 0010 % win (duration of the segments) - required. 0011 % params: structure with fields tapers, pad, Fs, fpass, err, trialave 0012 % - optional 0013 % tapers (precalculated tapers from dpss, or in the form [NW K] e.g [3 5]) -- optional. If not 0014 % specified, use [NW K]=[3 5] 0015 % pad (padding factor for the FFT) - optional. Defaults to 0. 0016 % e.g. For N = 500, if PAD = 0, we pad the FFT 0017 % to 512 points; if PAD = 2, we pad the FFT 0018 % to 2048 points, etc. 0019 % Fs (sampling frequency) - optional. Default 1. 0020 % fpass (frequency band to be used in the calculation in the form 0021 % [fmin fmax])- optional. 0022 % Default all frequencies between 0 and Fs/2 0023 % err (error calculation [1 p] - Theoretical error bars; [2 p] - Jackknife error bars 0024 % [0 p] or 0 - no error bars) - optional. Default 0. 0025 % trialave - not used 0026 % segave - optional 0 for don't average over segments, 1 for average - default 0027 % 1 0028 % Output: 0029 % S (spectrum in form frequency x segments if segave=0; in the form frequency if segave=1) 0030 % f (frequencies) 0031 % varS (variance of the log spectrum) 0032 % C (covariance matrix of the log spectrum - frequency x 0033 % frequency matrix) 0034 % Serr (error bars) only for err(1)>=1 0035 0036 if nargin < 2; error('Need data and segment information'); end; 0037 if size(data,2)~=1; error('works for only univariate time series'); end; 0038 if nargin < 3 ; params=[]; end; 0039 if nargin < 4 || isempty(segave); segave=1; end; 0040 [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params); clear trialave params 0041 if nargout==4 && err(1)==0; 0042 % Errors can't be computed if err(1)=0. Need to change params and run again. 0043 error('When Serr is desired, err(1) has to be non-zero.'); 0044 end; 0045 data=change_row_to_column(data); 0046 N=size(data,1); % length of segmented data 0047 dt=1/Fs; % sampling interval 0048 T=N*dt; % length of data in seconds 0049 E=0:win:T-win; % fictitious event triggers 0050 win=[0 win]; % use window length to define left and right limits of windows around triggers 0051 data=createdatamatc(data,E,Fs,win); % segmented data 0052 N=size(data,1); % length of segmented data 0053 nfft=2^(nextpow2(N)+pad); 0054 [f,findx]=getfgrid(Fs,nfft,fpass); 0055 tapers=dpsschk(tapers,N,Fs); % check tapers 0056 J=mtfftc(data,tapers,nfft,Fs); % compute tapered fourier transforms 0057 J=J(findx,:,:); % restrict to specified frequencies 0058 S=squeeze(mean(conj(J).*J,2)); % spectra of non-overlapping segments (average over tapers) 0059 lS=log(S); % log spectrum for non-overlapping segments 0060 C=cov(lS'); % covariance matrix of the log spectrum 0061 varS=diag(C); % variance of the log spectrum as a function of frequency 0062 if segave==1; S=squeeze(mean(S,2)); end; % mean of the spectrum averaged across segments 0063 if nargout==5; 0064 Serr=specerr(S,J,err,1); 0065 end;