# mtspecgramc

## PURPOSE

Multi-taper time-frequency spectrum - continuous process

## SYNOPSIS

function [S,t,f,Serr]=mtspecgramc(data,movingwin,params)

## DESCRIPTION

     Multi-taper time-frequency spectrum - continuous process

     Usage:
     [S,t,f,Serr]=mtspecgramc(data,movingwin,params)
     Input:
     Note units have to be consistent. Thus, if movingwin is in seconds, Fs
     has to be in Hz. see chronux.m for more information.
           data        (in form samples x channels/trials) -- required
           movingwin         (in the form [window winstep] i.e length of moving
                                                     window and step size)
                                                     Note that units here have
                                                     to be consistent with
                                                     units of Fs - required
           params: structure with fields tapers, pad, Fs, fpass, err, trialave
           - optional
               tapers : precalculated tapers from dpss or in the one of the following
                        forms:
                        (1) A numeric vector [TW K] where TW is the
                            time-bandwidth product and K is the number of
                            tapers to be used (less than or equal to
                            2TW-1).
                        (2) A numeric vector [W T p] where W is the
                            bandwidth, T is the duration of the data and p
                            is an integer such that 2TW-p tapers are used. In
                            this form there is no default i.e. to specify
                            the bandwidth, you have to specify T and p as
                            well. Note that the units of W and T have to be
                            consistent: if W is in Hz, T must be in seconds
                            and vice versa. Note that these units must also
                            be consistent with the units of params.Fs: W can
                            be in Hz if and only if params.Fs is in Hz.
                            The default is to use form 1 with TW=3 and K=5
                         Note that T has to be equal to movingwin(1).

                pad            (padding factor for the FFT) - optional (can take values -1,0,1,2...).
                        -1 corresponds to no padding, 0 corresponds to padding
                        to the next highest power of 2 etc.
                           e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
                           to 512 points, if pad=1, we pad to 1024 points etc.
                           Defaults to 0.
               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 (average over trials/channels when 1, don't average when 0) - optional. Default 0
     Output:
           S       (spectrum in form time x frequency x channels/trials if trialave=0;
                   in the form time x frequency if trialave=1)
           t       (times)
           f       (frequencies)
           Serr    (error bars) only for err(1)>=1

## CROSS-REFERENCE INFORMATION

This function calls:

- mtspectrumc Multi-taper spectrum - continuous process
- change_row_to_column Helper routine to transform 1d arrays into column vectors that are needed
- dpsschk Helper function to calculate tapers and, if precalculated tapers are supplied,
- getfgrid Helper function that gets the frequency grid associated with a given fft based computation
- getparams Helper function to convert structure params to variables used by the

This function is called by:

- mtspecgramtrigc Multi-taper event triggered time-frequency spectrum - continuous process
- specscope record and plot audio spectrogram
- specscopepp
- testscript function testscript(pname,direction,movingwin,segave,params,fscorr)
- testspecgram cd 'C:\Documents and Settings\Admin\Desktop\';
- acoustic_features_MB Usage:
- classify_spectra CLASSIFY_SPECTRA M-file for classify_spectra.fig
- wave_browser WAVE_BROWSER M-file for wave_browser.fig

## SOURCE CODE

    0001 function [S,t,f,Serr]=mtspecgramc(data,movingwin,params)
    0002 % Multi-taper time-frequency spectrum - continuous process
    0003 %
    0004 % Usage:
    0005 % [S,t,f,Serr]=mtspecgramc(data,movingwin,params)
    0006 % Input:
    0007 % Note units have to be consistent. Thus, if movingwin is in seconds, Fs
    0008 % has to be in Hz. see chronux.m for more information.
    0009 %       data        (in form samples x channels/trials) -- required
    0010 %       movingwin         (in the form [window winstep] i.e length of moving
    0011 %                                                 window and step size)
    0012 %                                                 Note that units here have
    0013 %                                                 to be consistent with
    0014 %                                                 units of Fs - required
    0015 %       params: structure with fields tapers, pad, Fs, fpass, err, trialave
    0016 %       - optional
    0017 %           tapers : precalculated tapers from dpss or in the one of the following
    0018 %                    forms:
    0019 %                    (1) A numeric vector [TW K] where TW is the
    0020 %                        time-bandwidth product and K is the number of
    0021 %                        tapers to be used (less than or equal to
    0022 %                        2TW-1).
    0023 %                    (2) A numeric vector [W T p] where W is the
    0024 %                        bandwidth, T is the duration of the data and p
    0025 %                        is an integer such that 2TW-p tapers are used. In
    0026 %                        this form there is no default i.e. to specify
    0027 %                        the bandwidth, you have to specify T and p as
    0028 %                        well. Note that the units of W and T have to be
    0029 %                        consistent: if W is in Hz, T must be in seconds
    0030 %                        and vice versa. Note that these units must also
    0031 %                        be consistent with the units of params.Fs: W can
    0032 %                        be in Hz if and only if params.Fs is in Hz.
    0033 %                        The default is to use form 1 with TW=3 and K=5
    0034 %                     Note that T has to be equal to movingwin(1).
    0035 %
    0036 %            pad            (padding factor for the FFT) - optional (can take values -1,0,1,2...).
    0037 %                    -1 corresponds to no padding, 0 corresponds to padding
    0038 %                    to the next highest power of 2 etc.
    0039 %                       e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
    0040 %                       to 512 points, if pad=1, we pad to 1024 points etc.
    0041 %                       Defaults to 0.
    0042 %           Fs   (sampling frequency) - optional. Default 1.
    0043 %           fpass    (frequency band to be used in the calculation in the form
    0044 %                                   [fmin fmax])- optional.
    0045 %                                   Default all frequencies between 0 and Fs/2
    0046 %           err  (error calculation [1 p] - Theoretical error bars; [2 p] - Jackknife error bars
    0047 %                                   [0 p] or 0 - no error bars) - optional. Default 0.
    0048 %           trialave (average over trials/channels when 1, don't average when 0) - optional. Default 0
    0049 % Output:
    0050 %       S       (spectrum in form time x frequency x channels/trials if trialave=0;
    0051 %               in the form time x frequency if trialave=1)
    0052 %       t       (times)
    0053 %       f       (frequencies)
    0054 %       Serr    (error bars) only for err(1)>=1
    0055
    0056 if nargin < 2; error('Need data and window parameters'); end;
    0057 if nargin < 3; params=[]; end;
    0058
    0059 [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
    0060 if length(params.tapers)==3 & movingwin(1)~=params.tapers(2);
    0061     error('Duration of data in params.tapers is inconsistent with movingwin(1), modify params.tapers(2) to proceed')
    0062 end
    0063
    0064 if nargout > 3 && err(1)==0;
    0065 %   Cannot compute error bars with err(1)=0. change params and run again.
    0066     error('When Serr is desired, err(1) has to be non-zero.');
    0067 end;
    0068 data=change_row_to_column(data);
    0069 [N,Ch]=size(data);
    0070 Nwin=round(Fs*movingwin(1)); % number of samples in window
    0071 Nstep=round(movingwin(2)*Fs); % number of samples to step through
    0072 nfft=max(2^(nextpow2(Nwin)+pad),Nwin);
    0073 f=getfgrid(Fs,nfft,fpass); Nf=length(f);
    0074 params.tapers=dpsschk(tapers,Nwin,Fs); % check tapers
    0075
    0076 winstart=1:Nstep:N-Nwin+1;
    0077 nw=length(winstart);
    0078
    0079 if trialave
    0080     S = zeros(nw,Nf);
    0081     if nargout==4; Serr=zeros(2,nw,Nf); end;
    0082 else
    0083     S = zeros(nw,Nf,Ch);
    0084     if nargout==4; Serr=zeros(2,nw,Nf,Ch); end;
    0085 end
    0086
    0087 for n=1:nw;
    0088    indx=winstart(n):winstart(n)+Nwin-1;
    0089    datawin=data(indx,:);
    0090    if nargout==4
    0091      [s,f,serr]=mtspectrumc(datawin,params);
    0092      Serr(1,n,:,:)=squeeze(serr(1,:,:));
    0093      Serr(2,n,:,:)=squeeze(serr(2,:,:));
    0094    else
    0095      [s,f]=mtspectrumc(datawin,params);
    0096    end
    0097    S(n,:,:)=s;
    0098 end;
    0099 S=squeeze(S);
    0100 if nargout==4;Serr=squeeze(Serr);end;
    0101 winmid=winstart+round(Nwin/2);
    0102 t=winmid/Fs;