Home > m > math > ltpda_lincom.m

ltpda_lincom

PURPOSE ^

LTPDA_LINCOM make the linear combination of the input analysis objects.

SYNOPSIS ^

function b = 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
 
 
 M Hewitson 14-02-07

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function b = 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 %
0026 % M Hewitson 14-02-07
0027 %
0028 
0029 % capture input variable names
0030 invars = {};
0031 for j=1:nargin
0032   invars = [invars cellstr(inputname(j))];
0033 end
0034 
0035 ALGONAME = mfilename;
0036 VERSION  = '$Id: ltpda_lincom.html,v 1.1 2007/06/08 14:15:10 hewitson Exp $';
0037 
0038 % Initialise output
0039 b  = [];
0040 ho = [];
0041 %% Look at inputs
0042 
0043 % check for plist and count
0044 % AOs with fsdata and tsdata
0045 pl     = [];
0046 coeffs = [];
0047 na = 0;
0048 for j=1:nargin
0049   a = varargin{j};
0050   if isa(a, 'plist')
0051     pl = a;
0052   end
0053 end
0054 % if a cdata is supplied, use
0055 % this instead of a plist
0056 as = [];
0057 for j=1:nargin
0058   a = varargin{j};
0059   for k=1:length(a)
0060     ak = a(k);
0061     if isa(ak, 'ao')
0062       d = ak.data;
0063       % is this a constant data
0064       if isa(d, 'cdata')
0065         coeffs = get(d, 'vals');
0066         % add history of this AO
0067         ho = [ho ak.hist];
0068 %         % Add coefficients to plist for history purposes
0069 %         pl = plist(param('coeffs', coeffs));
0070       elseif isa(d, 'tsdata')
0071         as = [as ak];
0072       end
0073     end
0074   end
0075 end
0076 
0077 % If a plist is supplied and no cdata, use plist
0078 if ~isempty(pl) && isempty(coeffs)
0079   coeffs = find(pl, 'coeffs');
0080 end
0081 
0082 na = length(as);
0083 nc = length(coeffs);
0084 
0085 if na < 2
0086   error('### at least two analysis objects are needed to form a linear combination.');
0087 end
0088 
0089 if na ~= nc
0090   disp(sprintf('Num AOs input: %d', na));
0091   disp(sprintf('Num Coeffs input: %d', nc));
0092   error('### specify one coefficient per analysis object.');
0093 end
0094   
0095 % loop over all inputs AOs
0096 nameStr = [];
0097 x = 0;
0098 for j=1:na
0099   
0100   % get this data
0101   aa = as(j);
0102   da = aa.data;
0103   dainfo = whos('da');
0104   % store the first AO data type to
0105   % check against the others
0106   if j==1
0107     d = da;
0108     dinfo = whos('d');
0109   end
0110   % check we know what type of data this is
0111   if isa(da, 'tsdata')
0112     xa  = da.x;
0113   elseif isa(da, 'fsdata')
0114     xa = da.xx;
0115   else
0116     error('### unknown data type.');
0117   end
0118 
0119   % check against first AO data
0120   if ~strcmp(dainfo.class, dinfo.class)
0121     error('### all analysis objects should have the same data type.');
0122   end
0123 
0124   % All time-series vectors should be the same sample rate
0125   if isa(d, 'tsdata')
0126     if da.fs ~= d.fs
0127       error('### all analysis objects should contain same sample rate data.');
0128     end
0129   end
0130 
0131   % All vectors need to be the same length
0132   if x~=0
0133     if length(x) ~= length(xa)
0134       error('### all vectors should be the same length.');
0135     end
0136   end
0137   % get the appropriate coefficient and add to linear combination
0138   c = coeffs(j);
0139   x = x + c.*xa;
0140   % add name to output str
0141   if j<=length(invars)
0142     if isempty(invars{j})
0143       n = aa.name;
0144     else
0145       n = invars{j};
0146     end
0147   else
0148     n = aa.name;
0149   end
0150   nameStr = [nameStr '+' num2str(c) sprintf('*%s', n)];
0151 
0152   % add history of this AO
0153   ho = [ho aa.hist];
0154 end
0155 
0156 % clean up nameStr
0157 if nameStr(1) == '+'
0158   nameStr = nameStr(2:end);
0159 end
0160 
0161 % Make output analysis object
0162 
0163 % create new output data
0164 if isa(d, 'tsdata')
0165   data = tsdata(x, d.fs);
0166   data = set(data, 'name', nameStr);
0167 elseif isa(d, 'fsdata')
0168   data = fsdata(d.f, x, d.fs);
0169   data = set(data, 'name', nameStr);
0170 else
0171   error('### unknown data type.');
0172 end
0173     
0174 % create new output history
0175 h = history(ALGONAME, VERSION, pl, ho);
0176 h = set(h, 'invars', invars);
0177 
0178 % make output analysis object
0179 b = ao(data, h);
0180 
0181 % set name
0182 b = set(b, 'name', nameStr);
0183 
0184 % END

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