Home > classes > @ao > polyfit.m

polyfit

PURPOSE ^

POLYFIT overloads polyfit() function of MATLAB for Analysis Objects.

SYNOPSIS ^

function varargout = polyfit(varargin)

DESCRIPTION ^

 POLYFIT overloads polyfit() function of MATLAB for Analysis Objects.

 Usage: b = polyfit(a, pl)

 Parameters:
   'N' - degree of polynomial to fit

 M Hewitson 05-06-07

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = polyfit(varargin)
0002 
0003 % POLYFIT overloads polyfit() function of MATLAB for Analysis Objects.
0004 %
0005 % Usage: b = polyfit(a, pl)
0006 %
0007 % Parameters:
0008 %   'N' - degree of polynomial to fit
0009 %
0010 % M Hewitson 05-06-07
0011 %
0012 %
0013 
0014 
0015 ALGONAME = mfilename;
0016 VERSION  = '$Id: polyfit.html,v 1.1 2007/06/08 14:15:03 hewitson Exp $';
0017 
0018 
0019 % Check if this is a call for parameters
0020 if nargin == 1
0021   in = char(varargin{1});
0022   if strcmp(in, 'Params')
0023     varargout{1} = getDefaultPL();
0024     return
0025   end
0026 end
0027 
0028 % capture input variable names
0029 invars = {};
0030 as     = [];
0031 ps     = [];
0032 for j=1:nargin
0033   invars = [invars cellstr(inputname(j))];
0034   if isa(varargin{j}, 'ao')
0035     as = [as varargin{j}];
0036   end
0037   if isa(varargin{j}, 'plist')
0038     ps = [ps varargin{j}];
0039   end
0040 end
0041 
0042 % check plist
0043 if isempty(ps)
0044   pl = getDefaultPL();
0045 else
0046   pl = combine(ps, getDefaultPL);
0047 end
0048 
0049 
0050 % Degree of polynomial to fit
0051 N = find(pl, 'N');
0052 
0053 %-----------------------
0054 % Loop over input AOs
0055 bs = [];
0056 na = length(as);
0057 for j=1:na
0058 
0059   a = as(j);
0060 
0061   %----------------------------
0062   % Get data
0063   [x,y] = getAOdata(a);
0064 
0065   %----------------------------
0066   % Fit polynomial
0067 %   [p,s,mu] = polyfit(x,y,N);
0068   [p,s] = polyfit(x,y,N);
0069   
0070   % Add coefficients to plist
0071   pl = append(pl, param('coeffs', p));
0072 
0073   % make new values from these coefficients
0074   y = polyval(p, x);
0075   
0076   %----------------------------
0077   % Make output AO/cdata
0078   d = a.data;
0079   if isa(d, 'tsdata')
0080     d = set(d, 'x', y);
0081   elseif isa(d, 'fsdata')
0082     d = set(d, 'xx', y);
0083   elseif isa(d, 'cdata')
0084     d = set(d, 'vals', y);
0085   elseif isa(d, 'xydata')
0086     d = set(d, 'y', y);
0087   else
0088     error('### Unknown data type.')
0089   end  
0090   d = set(d, 'name', sprintf('polyfit(%s)', d.name));
0091 
0092   h = history(ALGONAME, VERSION, pl, a.hist);
0093   h = set(h, 'invars', invars(j));
0094 
0095   % make output analysis object
0096   b = ao(d, h);
0097 
0098   % set name
0099   if isempty(invars{j})
0100     n = a.name;
0101   else
0102     n = invars{j};
0103   end
0104   b = set(b, 'name', sprintf('polyfit(%s)', n));
0105 
0106   % add to outputs
0107   bs = [bs b];
0108 
0109 end
0110 
0111 %---------------------
0112 % Set outputs
0113 varargout{1} = bs;
0114 
0115 %--------------------------------------------------------------------------
0116 % Get default params
0117 function plo = getDefaultPL()
0118 
0119 disp('* creating default plist...');
0120 plo = plist();
0121 plo = append(plo, param('N', 1));
0122 disp('* done.');
0123 
0124 
0125 
0126 
0127 
0128 % END

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