Home > m > math > ltpda_lincom.m

ltpda_lincom

PURPOSE ^

LTPDA_LINCOM make the linear combination of the input analysis objects.

SYNOPSIS ^

function varargout = ltpda_lincom(varargin)

DESCRIPTION ^

 LTPDA_LINCOM make the linear combination of the input analysis objects.
 
   >> b = lincom(a1,a2,a3,...,pl)
 or
   >> b = lincom(a1,a2,a3,...,aN)
 
 If no parameter list is specified, one of the analysis objects should
 contain a cdata type with the coefficients inside.
 
 Inputs:
   aN - a list of analysis objects of the same type
   pl - a parameter list
 
 Outputs:
   b - output analysis object
 
 If the last input argument is a parameter list (plist) it is used. The
 following parameters are recognised.
 
 Parameters:
   'coeffs' - vector of coefficients, same length as number of input AOs
 
 The following call returns a parameter list object that contains the
 default parameter values:
 
 >> pl = ltpda_lincom('Params')
 
 M Hewitson 14-02-07

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = ltpda_lincom(varargin)
0002 
0003 % LTPDA_LINCOM make the linear combination of the input analysis objects.
0004 %
0005 %   >> b = lincom(a1,a2,a3,...,pl)
0006 % or
0007 %   >> b = lincom(a1,a2,a3,...,aN)
0008 %
0009 % If no parameter list is specified, one of the analysis objects should
0010 % contain a cdata type with the coefficients inside.
0011 %
0012 % Inputs:
0013 %   aN - a list of analysis objects of the same type
0014 %   pl - a parameter list
0015 %
0016 % Outputs:
0017 %   b - output analysis object
0018 %
0019 % If the last input argument is a parameter list (plist) it is used. The
0020 % following parameters are recognised.
0021 %
0022 % Parameters:
0023 %   'coeffs' - vector of coefficients, same length as number of input AOs
0024 %
0025 % The following call returns a parameter list object that contains the
0026 % default parameter values:
0027 %
0028 % >> pl = ltpda_lincom('Params')
0029 %
0030 % M Hewitson 14-02-07
0031 %
0032 
0033 % Check if this is a call for parameters
0034 if nargin == 1
0035   in = char(varargin{1});
0036   if strcmp(in, 'Params')
0037     varargout{1} = getDefaultPL();
0038     return
0039   end
0040 end
0041 
0042 % capture input variable names
0043 invars = {};
0044 for j=1:nargin
0045   invars = [invars cellstr(inputname(j))];
0046 end
0047 
0048 ALGONAME = mfilename;
0049 VERSION  = '$Id: ltpda_lincom.html,v 1.2 2007/07/10 05:37:14 hewitson Exp $';
0050 
0051 % Initialise output
0052 b  = [];
0053 ho = [];
0054 %% Look at inputs
0055 
0056 % check for plist and count
0057 % AOs with fsdata and tsdata
0058 pl     = [];
0059 coeffs = [];
0060 na = 0;
0061 for j=1:nargin
0062   a = varargin{j};
0063   if isa(a, 'plist')
0064     pl = a;
0065   end
0066 end
0067 % if a cdata is supplied, use
0068 % this instead of a plist
0069 as = [];
0070 for j=1:nargin
0071   a = varargin{j};
0072   for k=1:length(a)
0073     ak = a(k);
0074     if isa(ak, 'ao')
0075       d = ak.data;
0076       % is this a constant data
0077       if isa(d, 'cdata')
0078         coeffs = get(d, 'vals');
0079         % add history of this AO
0080         ho = [ho ak.hist];
0081 %         % Add coefficients to plist for history purposes
0082 %         pl = plist(param('coeffs', coeffs));
0083       elseif isa(d, 'tsdata')
0084         as = [as ak];
0085       end
0086     end
0087   end
0088 end
0089 
0090 % If a plist is supplied and no cdata, use plist
0091 if ~isempty(pl) && isempty(coeffs)
0092   coeffs = find(pl, 'coeffs');
0093 end
0094 
0095 na = length(as);
0096 nc = length(coeffs);
0097 
0098 if na < 2
0099   error('### at least two analysis objects are needed to form a linear combination.');
0100 end
0101 
0102 if na ~= nc
0103   disp(sprintf('Num AOs input: %d', na));
0104   disp(sprintf('Num Coeffs input: %d', nc));
0105   error('### specify one coefficient per analysis object.');
0106 end
0107   
0108 % loop over all inputs AOs
0109 nameStr = [];
0110 x = 0;
0111 for j=1:na
0112   
0113   % get this data
0114   aa = as(j);
0115   da = aa.data;
0116   dainfo = whos('da');
0117   % store the first AO data type to
0118   % check against the others
0119   if j==1
0120     d = da;
0121     dinfo = whos('d');
0122   end
0123   % check we know what type of data this is
0124   if isa(da, 'tsdata')
0125     xa  = da.x;
0126   elseif isa(da, 'fsdata')
0127     xa = da.xx;
0128   else
0129     error('### unknown data type.');
0130   end
0131 
0132   % check against first AO data
0133   if ~strcmp(dainfo.class, dinfo.class)
0134     error('### all analysis objects should have the same data type.');
0135   end
0136 
0137   % All time-series vectors should be the same sample rate
0138   if isa(d, 'tsdata')
0139     if da.fs ~= d.fs
0140       error('### all analysis objects should contain same sample rate data.');
0141     end
0142   end
0143 
0144   % All vectors need to be the same length
0145   if x~=0
0146     if length(x) ~= length(xa)
0147       error('### all vectors should be the same length.');
0148     end
0149   end
0150   % get the appropriate coefficient and add to linear combination
0151   c = coeffs(j);
0152   x = x + c.*xa;
0153   % add name to output str
0154   if j<=length(invars)
0155     if isempty(invars{j})
0156       n = aa.name;
0157     else
0158       n = invars{j};
0159     end
0160   else
0161     n = aa.name;
0162   end
0163   nameStr = [nameStr '+' num2str(c) sprintf('*%s', n)];
0164 
0165   % add history of this AO
0166   ho = [ho aa.hist];
0167 end
0168 
0169 % clean up nameStr
0170 if nameStr(1) == '+'
0171   nameStr = nameStr(2:end);
0172 end
0173 
0174 % Make output analysis object
0175 
0176 % create new output data
0177 if isa(d, 'tsdata')
0178   data = tsdata(x, d.fs);
0179   data = set(data, 'name', nameStr);
0180 elseif isa(d, 'fsdata')
0181   data = fsdata(d.f, x, d.fs);
0182   data = set(data, 'name', nameStr);
0183 else
0184   error('### unknown data type.');
0185 end
0186     
0187 % create new output history
0188 h = history(ALGONAME, VERSION, pl, ho);
0189 h = set(h, 'invars', invars);
0190 
0191 % make output analysis object
0192 b = ao(data, h);
0193 
0194 % set name
0195 b = set(b, 'name', nameStr);
0196 
0197 varargout{1} = b;
0198 
0199 %--------------------------------------------------------------------------
0200 % Get default params
0201 function plo = getDefaultPL()
0202 
0203 disp('* creating default plist...');
0204 plo = plist();
0205 disp('* done.');
0206 
0207 
0208 % END

Generated on Wed 04-Jul-2007 19:03:10 by m2html © 2003