Mercurial > hg > ltpda
comparison m-toolbox/classes/@ao/polyfit.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % POLYFIT overloads polyfit() function of MATLAB for Analysis Objects. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: POLYFIT overloads polyfit() function of MATLAB for Analysis | |
5 % Objects. It finds the coefficients of a polynomial P(X) of | |
6 % degree N that fits the data Y best in a least-squares sense: | |
7 % P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1) | |
8 % | |
9 % CALL: bs = polyfit(a1, a2, a3, ..., pl) | |
10 % bs = polyfit(as,pl) | |
11 % bs = as.polyfit(pl) | |
12 % | |
13 % INPUTS: aN - input analysis objects with data to be fitted. | |
14 % X will be a.x | |
15 % Y will be a.y | |
16 % as - input analysis objects array | |
17 % pl - input parameter list | |
18 % | |
19 % OUTPUTs: bs - An array of pest objects, each with the N+1 fitting coefficients P(j) | |
20 % | |
21 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'polyfit')">Parameters Description</a> | |
22 % | |
23 % VERSION: $Id: polyfit.m,v 1.48 2011/05/12 03:37:08 mauro Exp $ | |
24 % | |
25 % EXAMPLES: | |
26 % | |
27 % %% Make fake AO from polyval | |
28 % nsecs = 100; | |
29 % fs = 10; | |
30 % | |
31 % u = unit('fm s^-2'); | |
32 % | |
33 % pl = plist('nsecs', nsecs, 'fs', fs, ... | |
34 % 'tsfcn', 'polyval([3 2 1 ], t) + 1000*randn(size(t))', ... | |
35 % 'xunits', 's', 'yunits', u); | |
36 % | |
37 % a1 = ao(pl); | |
38 % | |
39 % %% Fit a polynomial | |
40 % N = 3; | |
41 % p1 = polyfit(a1, plist('N', N)); | |
42 % p2 = polyfit(a1, plist('N', N, 'rescale', true)); | |
43 % | |
44 % %% Compute fit: evaluating pest | |
45 % %% Here we need to specify that we want to use the 'x' field of | |
46 % %% the AO a to build the output AO | |
47 % | |
48 % b1 = p1.eval(plist('type', 'tsdata', 'XData', a1, 'Xfield', 'x')); | |
49 % b2 = p2.eval(a1, plist('type', 'tsdata', 'Xfield', 'x')); | |
50 % | |
51 % %% Plot fit | |
52 % iplot(a1, b1, plist('LineStyles', {'', '--'})); | |
53 % | |
54 % %% Remove polynomial | |
55 % c = a1-b1; | |
56 % iplot(c) | |
57 % | |
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
59 | |
60 function varargout = polyfit(varargin) | |
61 | |
62 % Check if this is a call for parameters | |
63 if utils.helper.isinfocall(varargin{:}) | |
64 varargout{1} = getInfo(varargin{3}); | |
65 return | |
66 end | |
67 | |
68 import utils.const.* | |
69 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename); | |
70 | |
71 % Collect input variable names | |
72 in_names = cell(size(varargin)); | |
73 for ii = 1:nargin,in_names{ii} = inputname(ii);end | |
74 | |
75 % Collect all AOs and plists | |
76 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names); | |
77 pl = utils.helper.collect_objects(varargin(:), 'plist', in_names); | |
78 | |
79 if nargout == 0 | |
80 error('### polyfit can not be used as a modifier method. Please give at least one output'); | |
81 end | |
82 | |
83 % Combine plists | |
84 use_pl = parse(pl, getDefaultPlist); | |
85 | |
86 % Degree of polynomial to fit | |
87 N = find(use_pl, 'N'); | |
88 | |
89 % Center and rescale the data | |
90 rescale = utils.prog.yes2true(find(use_pl, 'rescale')); | |
91 | |
92 % Loop over input AOs | |
93 for jj = 1 : numel(as) | |
94 | |
95 if isa(as(jj).data, 'cdata') | |
96 warning('!!! Can''t fit to cdata objects. Skipping AO %s', ao_invars{jj}); | |
97 bs = []; | |
98 else | |
99 % Fit polynomial | |
100 mu = []; | |
101 if rescale | |
102 [p,s,mu] = polyfit(as(jj).x, as(jj).y, N); | |
103 else | |
104 [p,s] = polyfit(as(jj).x, as(jj).y, N); | |
105 end | |
106 | |
107 % prepare models, units, names | |
108 model = []; | |
109 for kk = 1:N+1 | |
110 names{kk} = ['P' num2str(kk)]; | |
111 units{kk} = as(jj).yunits ./ ((as(jj).xunits).^(N-kk+1)); | |
112 if kk == 1 | |
113 model = [model 'P' num2str(kk) '*X.^' num2str(N-kk+1)]; | |
114 else | |
115 model = [model ' + P' num2str(kk) '*X.^' num2str(N-kk+1)]; | |
116 end | |
117 end | |
118 model = smodel(plist('expression', model, ... | |
119 'params', names, ... | |
120 'values', p, ... | |
121 'xvar', 'X', ... | |
122 'xunits', as(jj).xunits, ... | |
123 'yunits', as(jj).yunits ... | |
124 )); | |
125 | |
126 % Build new pest objects from these N+1 coefficients | |
127 bs(jj) = pest; | |
128 bs(jj).setY(p); | |
129 bs(jj).setDof(s.df); | |
130 bs(jj).setNames(names{:}); | |
131 bs(jj).setYunits(units); | |
132 bs(jj).setModels(model); | |
133 bs(jj).name = sprintf('polyfit(%s)', ao_invars{jj}); | |
134 bs(jj).addHistory(getInfo('None'), use_pl, ao_invars(jj), as(jj).hist); | |
135 % Set procinfo object with some data | |
136 bs(jj).procinfo = plist('S', s, 'mu', mu); | |
137 | |
138 end | |
139 | |
140 end | |
141 | |
142 % Set output | |
143 varargout = utils.helper.setoutputs(nargout, bs); | |
144 | |
145 end | |
146 | |
147 %-------------------------------------------------------------------------- | |
148 % Get Info Object | |
149 %-------------------------------------------------------------------------- | |
150 function ii = getInfo(varargin) | |
151 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
152 sets = {}; | |
153 pl = []; | |
154 else | |
155 sets = {'Default'}; | |
156 pl = getDefaultPlist(); | |
157 end | |
158 % Build info object | |
159 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: polyfit.m,v 1.48 2011/05/12 03:37:08 mauro Exp $', sets, pl); | |
160 ii.setModifier(false); | |
161 ii.setArgsmin(1); | |
162 end | |
163 | |
164 %-------------------------------------------------------------------------- | |
165 % Get Default Plist | |
166 %-------------------------------------------------------------------------- | |
167 function plout = getDefaultPlist() | |
168 persistent pl; | |
169 if ~exist('pl', 'var') || isempty(pl) | |
170 pl = buildplist(); | |
171 end | |
172 plout = pl; | |
173 end | |
174 | |
175 function pl = buildplist() | |
176 pl = plist(); | |
177 | |
178 % N | |
179 p = param({'N','Degree of polynomial to fit.'}, {2, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, paramValue.SINGLE}); | |
180 pl.append(p); | |
181 | |
182 % Rescale | |
183 p = param({'rescale',['set to ''true'' or ''false'' to center and ', ... | |
184 'rescale the data before fitting.<br>', ... | |
185 'See "help polyfit" for further details.']}, paramValue.FALSE_TRUE); | |
186 pl.append(p); | |
187 | |
188 end | |
189 | |
190 % END | |
191 |