


POLYFIT overloads polyfit() function of MATLAB for Analysis Objects.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
DESCRIPTION: POLYFIT overloads polyfit() function of MATLAB for Analysis
Objects.
CALL: b = polyfit(a, pl)
VERSION: $Id: polyfit.html,v 1.2 2007/07/10 05:37:08 hewitson Exp $
Parameters: 'N' - degree of polynomial to fit
The following call returns a parameter list object that contains the
default parameter values:
>> pl = polyfit(ao, 'Params')
HISTORY: 05-06-07 M Hewitson
Creation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


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