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. 
 
 The function is a helper function for various higher level functions. It
 is meant to be called from other functions (e.g., ltpda_tfe).
 
 Usage: 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
 
 M Hewitson 30-05-07

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

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

Generated on Fri 08-Jun-2007 16:09:11 by m2html © 2003