# spsvd

## PURPOSE

Space frequency SVD of input data - continuous processes

## SYNOPSIS

function [sv,sp,fm] = spsvd(data,params,mdkp)

## DESCRIPTION

     Space frequency SVD of input data - continuous processes
     Usage: [sv,sp,fm] = spsvd(data,params,mdkp)
     Inputs:
     data       (data matrix in timexchannels form)-required
           params      structure containing parameters - params has the
           following fields: tapers, Fs, fpass, pad
               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

                Fs             (sampling frequency) -- optional. Defaults to 1.
               fpass       (frequency band to be used in the calculation in the form
                                       [fmin fmax])- optional.
                                       Default all frequencies between 0 and Fs/2
                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.
     mdkp       (number of dimensions to be kept)-optional. Default is the
                   maximum possible modes determined by taper parameters

     Outputs:
     sv sp fm  : singular values, space modes, frequency modes

## CROSS-REFERENCE INFORMATION

This function calls:

- 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:

## SOURCE CODE

    0001 function [sv,sp,fm] = spsvd(data,params,mdkp)
    0002 % Space frequency SVD of input data - continuous processes
    0003 % Usage: [sv,sp,fm] = spsvd(data,params,mdkp)
    0004 % Inputs:
    0005 % data       (data matrix in timexchannels form)-required
    0006 %       params      structure containing parameters - params has the
    0007 %       following fields: tapers, Fs, fpass, pad
    0008 %           tapers : precalculated tapers from dpss or in the one of the following
    0009 %                    forms:
    0010 %                   (1) A numeric vector [TW K] where TW is the
    0011 %                       time-bandwidth product and K is the number of
    0012 %                       tapers to be used (less than or equal to
    0013 %                       2TW-1).
    0014 %                   (2) A numeric vector [W T p] where W is the
    0015 %                       bandwidth, T is the duration of the data and p
    0016 %                       is an integer such that 2TW-p tapers are used. In
    0017 %                       this form there is no default i.e. to specify
    0018 %                       the bandwidth, you have to specify T and p as
    0019 %                       well. Note that the units of W and T have to be
    0020 %                       consistent: if W is in Hz, T must be in seconds
    0021 %                       and vice versa. Note that these units must also
    0022 %                       be consistent with the units of params.Fs: W can
    0023 %                       be in Hz if and only if params.Fs is in Hz.
    0024 %                       The default is to use form 1 with TW=3 and K=5
    0025 %
    0026 %            Fs             (sampling frequency) -- optional. Defaults to 1.
    0027 %           fpass       (frequency band to be used in the calculation in the form
    0028 %                                   [fmin fmax])- optional.
    0029 %                                   Default all frequencies between 0 and Fs/2
    0030 %            pad            (padding factor for the FFT) - optional (can take values -1,0,1,2...).
    0031 %                    -1 corresponds to no padding, 0 corresponds to padding
    0032 %                    to the next highest power of 2 etc.
    0033 %                       e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
    0034 %                       to 512 points, if pad=1, we pad to 1024 points etc.
    0035 %                       Defaults to 0.
    0036 % mdkp       (number of dimensions to be kept)-optional. Default is the
    0037 %               maximum possible modes determined by taper parameters
    0038 %
    0039 % Outputs:
    0040 % sv sp fm  : singular values, space modes, frequency modes
    0041
    0042
    0043 if nargin < 1; error('Need data'); end;
    0044 if nargin < 2 || isempty(params); params=[]; end;
    0045 [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
    0046 clear err trialave params
    0047 [N,NCHAN]=size(data);
    0048 tapers=dpsschk(tapers,N,Fs);
    0049 nfft=max(2^(nextpow2(N)+pad),N);% number of points in fft
    0050 [N,K]=size(tapers);
    0051 if nargin<3 || isempty(mdkp); mdkp=min(K,NCHAN);
    0052 elseif mdkp > min(K,NCHAN); error('mdkp has to be less than both K and NCHAN');end;
    0053
    0054 tvec=(1:N)';
    0055 tvec=repmat(tvec,[1 K]);
    0056 tvec=tvec*2*pi*i;
    0057 f=getfgrid(Fs,nfft,fpass);
    0058 nf=length(f);
    0059 sp=zeros(NCHAN,nf,mdkp);
    0060 sp=sp+i*sp;
    0061 fm=zeros(K,nf,mdkp);
    0062 fm=fm+i*fm;
    0063 sv=zeros(nf,min([K,NCHAN]));
    0064 for j=1:nf
    0065 %     for k=1:K
    0066 %       proj(:,k)=tapers(:,k).*exp(-f0*tvec');
    0067 %     end
    0068     proj=tapers.*exp(-f(j)*tvec);
    0069     tmp=data'*proj; % projected data
    0070     [u,s,v]= svd(tmp,0); % svd
    0071     for mk=1:mdkp,
    0072       sp(:,j,mk)=u(:,mk)';
    0073       fm(:,j,mk)=v(:,mk)';
    0074     end
    0075     sv(j,:)=diag(s);
    0076 end;