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.

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

 DESCRIPTION: LTPDA_LINCOM make the linear combination of the
              input analysis objects.

 CALL:        b = lincom(a1,a2,a3,...,pl)
              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')

 HISTORY:     14-02-07 M Hewitson
                 Creation.

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = ltpda_lincom(varargin)
0002 % LTPDA_LINCOM make the linear combination of the input analysis objects.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: LTPDA_LINCOM make the linear combination of the
0007 %              input analysis objects.
0008 %
0009 % CALL:        b = lincom(a1,a2,a3,...,pl)
0010 %              b = lincom(a1,a2,a3,...,aN)
0011 %
0012 %              If no parameter list is specified, one of the analysis objects
0013 %              should contain a cdata type with the coefficients inside.
0014 %
0015 % INPUTS:      aN - a list of analysis objects of the same type
0016 %              pl - a parameter list
0017 %
0018 % OUTPUTS:     b  - output analysis object
0019 %
0020 %              If the last input argument is a parameter list (plist) it is used.
0021 %              The following parameters are recognised.
0022 %
0023 % PARAMETERS:  'coeffs' - vector of coefficients, same length as number of input AOs
0024 %
0025 %              The following call returns a parameter list object that
0026 %              contains the default parameter values:
0027 %
0028 %              >> pl = ltpda_lincom('Params')
0029 %
0030 % HISTORY:     14-02-07 M Hewitson
0031 %                 Creation.
0032 %
0033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0034 
0035 % Check if this is a call for parameters
0036 if nargin == 1
0037   in = char(varargin{1});
0038   if strcmp(in, 'Params')
0039     varargout{1} = getDefaultPL();
0040     return
0041   end
0042 end
0043 
0044 % capture input variable names
0045 invars = {};
0046 for j=1:nargin
0047   invars = [invars cellstr(inputname(j))];
0048 end
0049 
0050 ALGONAME = mfilename;
0051 VERSION  = '$Id: ltpda_lincom.m,v 1.13 2007/11/09 14:07:39 ingo Exp $';
0052 
0053 % Initialise output
0054 b  = [];
0055 ho = [];
0056 %% Look at inputs
0057 
0058 % check for plist and count
0059 % AOs with fsdata and tsdata
0060 pl     = [];
0061 coeffs = [];
0062 na = 0;
0063 for j=1:nargin
0064   a = varargin{j};
0065   if isa(a, 'plist')
0066     pl = a;
0067   end
0068 end
0069 % if a cdata is supplied, use
0070 % this instead of a plist
0071 as = [];
0072 for j=1:nargin
0073   a = varargin{j};
0074   for k=1:length(a)
0075     ak = a(k);
0076     if isa(ak, 'ao')
0077       d = ak.data;
0078       % is this a constant data
0079       if isa(d, 'cdata')
0080         coeffs = d.y;
0081         % add history of this AO
0082         ho = [ho ak.hist];
0083 %         % Add coefficients to plist for history purposes
0084 %         pl = plist(param('coeffs', coeffs));
0085       elseif isa(d, 'tsdata')
0086         as = [as ak];
0087       end
0088     end
0089   end
0090 end
0091 
0092 % If a plist is supplied and no cdata, use plist
0093 if ~isempty(pl) && isempty(coeffs)
0094   coeffs = find(pl, 'coeffs');
0095 end
0096 
0097 na = length(as);
0098 nc = length(coeffs);
0099 
0100 if na < 2
0101   error('### at least two analysis objects are needed to form a linear combination.');
0102 end
0103 
0104 if na ~= nc
0105   disp(sprintf('Num AOs input: %d', na));
0106   disp(sprintf('Num Coeffs input: %d', nc));
0107   error('### specify one coefficient per analysis object.');
0108 end
0109 
0110 % loop over all inputs AOs
0111 nameStr = [];
0112 y = 0;
0113 for j=1:na
0114 
0115   % get this data
0116   aa = as(j);
0117   da = aa.data;
0118   dainfo = whos('da');
0119   % store the first AO data type to
0120   % check against the others
0121   if j==1
0122     d = da;
0123     dinfo = whos('d');
0124   end
0125   % check we know what type of data this is
0126   if isa(da, 'tsdata') || isa(da, 'fsdata')
0127     ya = da.y;
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 y~=0
0146     if length(y) ~= length(ya)
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   y = y + c.*ya;
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(d.x, y);
0179   data = set(data, 'name', nameStr);
0180 elseif isa(d, 'fsdata')
0181   data = fsdata(d.x, y, 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 plo = plist();
0204 
0205 
0206 % END

Generated on Fri 07-Mar-2008 15:46:43 by m2html © 2003