Home > m > sigproc > frequency_domain > ltpda_xspec.m

ltpda_xspec

PURPOSE ^

LTPDA_XSPEC performs cross-spectral analysis of various forms.

SYNOPSIS ^

function varargout = ltpda_xspec(varargin)

DESCRIPTION ^

 LTPDA_XSPEC performs cross-spectral analysis of various forms.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: LTPDA_XSPEC performs cross-spectral analysis of various forms.
              The function is a helper function for various higher level
              functions. It is meant to be called from other functions
              (e.g., ltpda_tfe).

 CALL:       b = ltpda_xspec(a, pl, method, iALGO, iVER, invars);

 INPUTS:     a      - vector of input AOs
             pl     - input parameter list
             method - one of 'TF', 'CPSD', 'COHERE'
             iALGO  - ALGONAME from the calling higher level script
             iVER   - VERSION from the calling higher level script
             invars - invars variable from the calling higher level script

 OUTPUTS:    b  - Na x Na matrix of output AOs

 VERSION:    $Id: ltpda_xspec.m,v 1.5 2007/07/16 12:52:21 ingo Exp $

 HISTORY:    30-05-2007 M Hewitson
                Creation

 The following call returns a parameter list object that contains the
 default parameter values:

 >> pl = ltpda_xspec('Params')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = ltpda_xspec(varargin)
0002 % LTPDA_XSPEC performs cross-spectral analysis of various forms.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: LTPDA_XSPEC performs cross-spectral analysis of various forms.
0007 %              The function is a helper function for various higher level
0008 %              functions. It is meant to be called from other functions
0009 %              (e.g., ltpda_tfe).
0010 %
0011 % CALL:       b = ltpda_xspec(a, pl, method, iALGO, iVER, invars);
0012 %
0013 % INPUTS:     a      - vector of input AOs
0014 %             pl     - input parameter list
0015 %             method - one of 'TF', 'CPSD', 'COHERE'
0016 %             iALGO  - ALGONAME from the calling higher level script
0017 %             iVER   - VERSION from the calling higher level script
0018 %             invars - invars variable from the calling higher level script
0019 %
0020 % OUTPUTS:    b  - Na x Na matrix of output AOs
0021 %
0022 % VERSION:    $Id: ltpda_xspec.m,v 1.5 2007/07/16 12:52:21 ingo Exp $
0023 %
0024 % HISTORY:    30-05-2007 M Hewitson
0025 %                Creation
0026 %
0027 % The following call returns a parameter list object that contains the
0028 % default parameter values:
0029 %
0030 % >> pl = ltpda_xspec('Params')
0031 %
0032 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0033 
0034 % unpack inputs
0035 as     = varargin{1};
0036 pl     = varargin{2};
0037 method = varargin{3};
0038 iALGO  = varargin{4};
0039 iVER   = varargin{5};
0040 invars = varargin{6};
0041 
0042 ALGONAME = iALGO;
0043 VERSION  = [iVER '/' '$Id: ltpda_xspec.m,v 1.5 2007/07/16 12:52:21 ingo Exp $'];
0044 
0045 
0046 % initialise output array
0047 bs = [];
0048 
0049 na = length(as);
0050 if na < 2
0051   error('### LTPDA_XSPEC needs at least two AOs to make a transfer function.');
0052 end
0053 
0054 
0055 %----------------- Resample all AOs
0056 
0057 disp('*** Resampling AOs...');
0058 fsmax = findFsMax(as);
0059 fspl  = plist(param('fsout', fsmax));
0060 bs = [];
0061 for i=1:na
0062   a = as(i);
0063   % check this is a time-series object
0064   if ~isa(a.data, 'tsdata')
0065     error('### ltpda_xspec requires tsdata (time-series) inputs.');
0066   end
0067   % Check Fs
0068   if a.data.fs ~= fsmax
0069     warning(sprintf('!!! Resampling AO %s/%s to %f Hz', a.name, a.data.name, fsmax));
0070     a = resample(a, fspl);
0071   end
0072   bs = [bs a];
0073 end
0074 as = bs;
0075 clear bs;
0076 disp('*** Done.');
0077 
0078 
0079 %----------------- Truncate all vectors
0080 
0081 % Get shortest vector
0082 disp('*** Truncating all vectors...');
0083 lmin = findShortestVector(as);
0084 nsecs = lmin / fsmax;
0085 bs = [];
0086 for i=1:na
0087   a = as(i);
0088   if len(a) ~= lmin
0089     warning(sprintf('!!! Truncating AO %s/%s to %d secs', a.name, a.data.name, nsecs));
0090     bs = [bs  select(a, 1:lmin)];
0091   else
0092     bs = [bs a];
0093   end
0094 end
0095 as = bs;
0096 clear bs;
0097 disp('*** Done.');
0098 
0099 %----------------- check input parameters
0100 
0101 % points in FFT
0102 Nfft  = find(pl, 'Nfft');
0103 if ~isempty(Nfft)
0104   if Nfft < 0
0105     Nfft = abs(Nfft) * lmin;
0106     disp(sprintf('! Using default Nfft of %g', Nfft))
0107     pl = set(pl, 'Nfft', Nfft);
0108   end
0109 end
0110 
0111 % Window object
0112 Win   = find(pl, 'Win');
0113 if ~isempty(Win)
0114   if length(Win.win)~=Nfft
0115     switch Win.name
0116       case 'Kaiser'
0117         Win = specwin(Win.name, Nfft, Win.psll);
0118       otherwise
0119         Win = specwin(Win.name, Nfft);
0120     end
0121     disp(sprintf('! Reset window to %s(%d)', strrep(Win.name, '_', '\_'), length(Win.win)))
0122     pl = set(pl, 'Win', Win);
0123   end
0124 end
0125 
0126 % desired overlap
0127 Nolap = find(pl, 'Nolap');
0128 if ~isempty(Nolap)
0129   if Nolap < 0
0130     % use window function rov
0131     Nolap = Win.rov/100;
0132     disp(sprintf('! Using recommended overlap of %d', Nolap))
0133     pl = set(pl, 'Nolap', Nolap);
0134   end
0135 end
0136 
0137 % Loop over input AOs
0138 bs = cell(na);
0139 for j=1:na
0140   aj = as(j);
0141   for k=1:na
0142     ak = as(k);
0143     % -------- Make Xspec estimate
0144 
0145     switch method
0146       case 'TF'
0147         % Compute TF using tfestimate
0148         [txy,f] = tfestimate(aj.data.x,ak.data.x,Win.win,round(Nolap*Nfft),Nfft,fsmax);
0149       case 'COHERE'
0150         % Compute coherence using mscohere
0151         [txy,f] = mscohere(aj.data.x,ak.data.x,Win.win,round(Nolap*Nfft),Nfft,fsmax);
0152       case 'CPSD'
0153         % Compute Cross-spectral density using CPSD
0154         [txy,f] = cpsd(aj.data.x,ak.data.x,Win.win,round(Nolap*Nfft),Nfft,fsmax);
0155       otherwise
0156         error('### Unknown method.')
0157     end
0158 
0159     % create new output fsdata
0160     fs = fsdata(f, txy, fsmax);
0161     fs = set(fs, 'name', sprintf('%s(%s->%s)', method, aj.data.name, ak.data.name));
0162     fs = set(fs, 'enbw', Win.nenbw*fsmax/Nfft);
0163 
0164     %----------- create new output history
0165 
0166     % we need to get the input histories in the same order as the inputs
0167     % to this function call, not in the order of the input to tfestimate;
0168     % otherwise the resulting matrix on a 'create from history' will be
0169     % mirrored.
0170     if j<k
0171       h = history(ALGONAME, VERSION, pl, [aj.hist ak.hist]);
0172     else
0173       h = history(ALGONAME, VERSION, pl, [ak.hist aj.hist]);
0174     end
0175     h = set(h, 'invars', [invars(j) invars(k)]);
0176 
0177     % make output analysis object
0178     b = ao(fs, h);
0179 
0180     % set name
0181     if isempty(invars{k})
0182       nk = ak.name;
0183     else
0184       nk = invars{k};
0185     end
0186     if isempty(invars{j})
0187       nj = aj.name;
0188     else
0189       nj = invars{j};
0190     end
0191     b = set(b, 'name', sprintf('%s(%s->%s)', method, nj, nk));
0192 
0193     % add to outputs
0194     bs(j,k) = {b};
0195   end % End second loop over AOs
0196 end % End first loop over AOs
0197 
0198 % now we have a cell matrix of AOs but
0199 % we want a normal matrix
0200 b  = [bs{:}];
0201 varargout{1} = reshape(b, na, na);
0202 
0203 
0204 %--------------------------------------------------------------------------
0205 % Returns the length of the shortest vector in samples
0206 function lmin = findShortestVector(as)
0207 
0208 lmin = 1e20;
0209 for j=1:length(as)
0210   if len(as(j)) < lmin
0211     lmin = len(as(j));
0212   end
0213 end
0214 
0215 
0216 %--------------------------------------------------------------------------
0217 % Returns the max Fs of a set of AOs
0218 function fs = findFsMax(as)
0219 
0220 fs = 0;
0221 for j=1:length(as)
0222   a = as(j);
0223   if a.data.fs > fs
0224     fs = a.data.fs;
0225   end
0226 end
0227 
0228 
0229

Generated on Mon 03-Sep-2007 12:12:34 by m2html © 2003