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.14 2008/03/31 10:27:34 hewitson Exp $ Parameters: 'N' - degree of polynomial to fit 'coeffs' - (optional) coefficients formed e.g. by [p,s] = polyfit(x,y,N); The following call returns a parameter list object that contains the default parameter values: >> pl = polyfit(ao, 'Params') The following call returns a string that contains the routine CVS version: >> version = polyfit(ao,'Version') The following call returns a string that contains the routine category: >> category = polyfit(ao,'Category') 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.14 2008/03/31 10:27:34 hewitson Exp $ 0012 % 0013 % Parameters: 'N' - degree of polynomial to fit 0014 % 'coeffs' - (optional) coefficients 0015 % formed e.g. by [p,s] = polyfit(x,y,N); 0016 % 0017 % The following call returns a parameter list object that contains the 0018 % default parameter values: 0019 % 0020 % >> pl = polyfit(ao, 'Params') 0021 % 0022 % The following call returns a string that contains the routine CVS version: 0023 % 0024 % >> version = polyfit(ao,'Version') 0025 % 0026 % The following call returns a string that contains the routine category: 0027 % 0028 % >> category = polyfit(ao,'Category') 0029 % 0030 % HISTORY: 05-06-07 M Hewitson 0031 % Creation 0032 % 0033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0034 0035 ALGONAME = mfilename; 0036 VERSION = '$Id: polyfit.html,v 1.14 2008/03/31 10:27:34 hewitson Exp $'; 0037 CATEGORY = 'Signal Processing'; 0038 0039 bs = []; 0040 0041 %% Check if this is a call for parameters 0042 if nargin == 2 0043 if isa(varargin{1}, 'ao') && ischar(varargin{2}) 0044 in = char(varargin{2}); 0045 if strcmp(in, 'Params') 0046 varargout{1} = getDefaultPL(); 0047 return 0048 elseif strcmp(in, 'Version') 0049 varargout{1} = VERSION; 0050 return 0051 elseif strcmp(in, 'Category') 0052 varargout{1} = CATEGORY; 0053 return 0054 end 0055 end 0056 end 0057 0058 %% Collect input ao's, plist's and ao variable names 0059 in_names = {}; 0060 for ii = 1:nargin 0061 in_names{end+1} = inputname(ii); 0062 end 0063 0064 [as, ps, invars] = collect_inputs(varargin, in_names); 0065 0066 % check plist 0067 if isempty(ps) 0068 pl = getDefaultPL(); 0069 else 0070 pl = combine(ps, getDefaultPL); 0071 end 0072 0073 0074 % Degree of polynomial to fit 0075 N = find(pl, 'N'); 0076 coeffs = find(pl, 'coeffs'); 0077 0078 %----------------------- 0079 % Loop over input AOs 0080 for j=1:numel(as) 0081 0082 a = as(j); 0083 0084 %---------------------------- 0085 % Get data 0086 [x,y] = get_xy_values(a.data); 0087 0088 %---------------------------- 0089 % Fit polynomial 0090 0091 if isempty(coeffs) 0092 %[p,s,mu] = polyfit(x,y,N); 0093 [p,s] = polyfit(x,y,N); 0094 0095 % Add coefficients to plist 0096 pl = pset(pl, 'coeffs', p); 0097 else 0098 p = coeffs; 0099 end 0100 0101 % make new values from these coefficients 0102 y = polyval(p, x); 0103 0104 %---------------------------- 0105 % Make output AO/cdata 0106 d = a.data; 0107 d = set_xy_axis(d, x, y); 0108 d = set(d, 'name', sprintf('polyfit(%s)', d.name)); 0109 0110 h = history(ALGONAME, VERSION, pl, a.hist); 0111 h = set(h, 'invars', invars(j)); 0112 0113 % make output analysis object 0114 b = ao(d, h); 0115 0116 % set name 0117 b = setnh(b, 'name', sprintf('polyfit(%s)', invars{j})); 0118 0119 % add to outputs 0120 bs = [bs b]; 0121 0122 end 0123 0124 % Reshape the ouput to the same size of the input 0125 bs = reshape(bs, size(as)); 0126 0127 %--------------------- 0128 % Set outputs 0129 varargout{1} = bs; 0130 0131 %-------------------------------------------------------------------------- 0132 % Get default params 0133 function plo = getDefaultPL() 0134 0135 plo = plist(); 0136 plo = append(plo, param('N', 1)); 0137 plo = append(plo, param('coeffs', [])); 0138 0139 % END 0140