comparison m-toolbox/classes/@ao/fromPolyval.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 % FROMPOLYVAL Construct an ao from polynomial coefficients
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % FUNCTION: fromPolyval
5 %
6 % DESCRIPTION: Construct an ao from polynomial coefficients
7 %
8 % CALL: a = fromPolyval(a, vals)
9 %
10 % PARAMETER: pl: plist containing 'polyval', 'Nsecs', 'fs', or 't'
11 %
12 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13
14 function a = fromPolyval(a, pli)
15
16 VERSION = '$Id: fromPolyval.m,v 1.27 2011/08/15 05:57:20 hewitson Exp $';
17
18 import utils.const.*
19
20 % get AO info
21 ii = ao.getInfo('ao', 'From Polynomial');
22
23 % Set the method version string in the minfo object
24 ii.setMversion([VERSION '-->' ii.mversion]);
25
26 % Add default values
27 pl = applyDefaults(ii.plists, pli);
28
29 coeffs = find(pl, 'polyval');
30 Nsecs = find(pl, 'Nsecs');
31 fs = find(pl, 'fs');
32 t = find(pl, 't');
33 x = find(pl, 'x');
34 f = find(pl, 'f');
35 dtype = find(pl, 'type');
36
37 % Collect the coefficients
38 coeff_class = class(coeffs);
39
40 switch coeff_class
41 case 'ao'
42 if numel(coeffs) == 1
43 % Single AO with an array of coefficients
44 coeffs = coeffs.y;
45 else
46 % Vector of AOs with single coefficients
47 error(['### Incorrect container for coefficients. \n' ...
48 'Supporting only aos, aos inside Nx1 or 1xN matrix object, single pest objects, double arrays']);
49 end
50 case 'matrix'
51 % A matrix of AOs with single coefficients
52 coeff_array = [ao];
53 ncol = ncols(coeffs);
54 nrow = nrows(coeffs);
55 if ncol > 1
56 if nrow == 1
57 nc = ncol;
58 for jj = 1:nc
59 coeff_array(jj) = coeffs.getObjectAtIndex(1, jj);
60 end
61 coeffs = coeff_array.y;
62 else
63 error(['### Incorrect container for coefficients. \n' ...
64 'Supporting only aos, aos inside Nx1 or 1xN matrix object, single pest objects, double arrays']);
65 end
66 else
67 nc = nrow;
68 for jj = 1:nc
69 coeff_array(jj) = coeffs.getObjectAtIndex(jj, 1);
70 end
71 coeffs = coeff_array.y;
72 end
73 case 'pest'
74 nc = numel(coeffs.y);
75 if numel(coeffs) == 1
76 % Single pest with an array of coefficients
77 if ~isempty(coeffs.models)
78 yunits = coeffs.models.yunits;
79 else
80 yunits = [];
81 end
82 coeff_yunits = coeffs.yunits;
83 coeffs = coeffs.y;
84 else
85 % Vector of pests with single coefficients
86 error(sprintf(['### Incorrect container for coefficients. \n' ...
87 'Supporting only aos, aos inside Nx1 or 1xN matrix object, single pest objects, double arrays']));
88 end
89 case 'double'
90 % Do nothing in this case
91 otherwise
92 error(sprintf(['### Incorrect container for coefficients. \n' ...
93 'Supporting only aos, aos inside Nx1 or 1xN matrix object, single pest objects, double arrays']));
94 end
95
96 % Try to decide what to do if the user doesn't specify data type
97 if isempty(dtype)
98 if ~isempty(Nsecs) || ~isempty(t) || ~isempty(fs)
99 dtype = 'tsdata';
100 elseif ~isempty(x)
101 dtype = 'xydata';
102 elseif ~isempty(f)
103 dtype = 'fsdata';
104 else
105 error('### Please specify data type and parameters');
106 end
107 end
108
109 switch dtype
110 case 'tsdata'
111 if isa(t, 'ao')
112 t_xunits = t.xunits;
113 % Override the xunits by using those from the t vector
114 utils.helper.msg(msg.OFF, ...
115 'warning: overriding the user-input xunits %s with the t ao %s', ...
116 char(find(pl, 'xunits')), char(t_xunits));
117 t = t.x;
118 else
119 t_xunits = [];
120 end
121
122 % Check t vector
123 if isempty(t)
124 if isempty(Nsecs) || isempty(fs)
125 error('Please provide either ''Nsecs'' and ''fs'', or ''t'' for polyval constructor with data type ''tsdata''.');
126 end
127 t = 0 : 1/fs : Nsecs-1/fs; %t = linspace(0, Nsecs - 1/fs, Nsecs*fs);
128 end
129
130 y = polyval(coeffs,t);
131
132 % Make a tsdata object
133 p = tsdata(t, y);
134
135 % Make sure the actual sampling frequency fs is captured in the plist
136 pl.pset('fs', p.fs);
137
138 % set T0
139 p.setT0(pl.find('t0'));
140
141 % set Toffset
142 if ~isempty(pl.find('toffset'))
143 toffset = p.toffset + 1000*pl.find('toffset');
144 p.setToffset(toffset);
145 end
146
147 % Checks the units
148 % xunits
149 if isempty(t_xunits)
150 new_xunits = find(pl, 'xunits');
151 if isempty(new_xunits)
152 new_xunits = 's';
153 end
154 else
155 new_xunits = t_xunits;
156 end
157 pl.pset('xunits', new_xunits);
158
159 case 'xydata'
160 if isa(x, 'ao')
161 x_xunits = x.xunits;
162 % Override the xunits by using those from the x vector
163 utils.helper.msg(msg.OFF, ...
164 'warning: overriding the user-input xunits %s with the x ao %s', ...
165 char(find(pl, 'xunits')), char(x_xunits));
166 pl.pset('xunits', x_xunits);
167 x = x.x;
168 else
169 x_xunits = [];
170 end
171
172 % Check x vector
173 if isempty(x)
174 error('### Please provide the x values');
175 end
176
177 y = polyval(coeffs,x);
178
179 % Make a xydata object
180 p = xydata(x, y);
181
182 % Checks the units
183 % xunits
184 if isempty(x_xunits)
185 new_xunits = find(pl, 'xunits');
186 else
187 new_xunits = x_xunits;
188 end
189 pl.pset('xunits', new_xunits);
190
191 case 'fsdata'
192 if isa(f, 'ao')
193 f_xunits = f.xunits;
194 % Override the xunits by using those from the x vector
195 utils.helper.msg(msg.OFF, ...
196 'warning: overriding the user-input xunits %s with the x ao %s', ...
197 char(find(pl, 'xunits')), char(f_xunits));
198 pl.pset('xunits', f_xunits);
199 f = f.x;
200 else
201 f_xunits = [];
202 end
203
204 % Check f vector
205 if isempty(f)
206 error('### Please provide the frequency values');
207 end
208
209 y = polyval(coeffs,f);
210
211 % Make a fsdata object
212 p = fsdata(f, y);
213
214 % Checks the units
215 % xunits
216 if isempty(f_xunits)
217 new_xunits = find(pl, 'xunits');
218 if isempty(new_xunits)
219 new_xunits = 'Hz';
220 end
221 else
222 new_xunits = f_xunits;
223 end
224 pl.pset('xunits', new_xunits);
225
226 otherwise
227 error('### Unsupported data type %s', dtype);
228 end
229
230 % yunits
231 switch lower(coeff_class)
232 case 'matrix'
233 % Matrix of AOs with coefficients was provided. So the units must be
234 % consistent (with requested, if any, and between each other)
235 new_yunits = find(pl, 'yunits');
236 if isempty(new_yunits)
237 new_yunits = simplify(coeff_array(1).yunits .* (new_xunits.^(nc-1)));
238 else
239 new_yunits = unit(new_yunits);
240 end
241 for jj = 1:nc
242 if coeff_array(jj).yunits * (new_xunits.^(nc-jj)) ~= new_yunits
243 error('### Inconsistent units either between coefficients or with requested')
244 end
245 end
246 pl.pset('yunits', new_yunits);
247 case 'pest'
248 % A pest object with coefficients was provided
249 % Did the user provide yunits? Let's use them
250 new_yunits = find(pl, 'yunits');
251 % Otherwise, let's use the yunits of the smodel inside the pest
252 if isempty(new_yunits)
253 new_yunits = yunits;
254 end
255 % Otherwise, let's calculate them
256 if isempty(new_yunits)
257 new_yunits = simplify(coeff_yunits(1) .* (new_xunits.^(nc-1)));
258 for jj = 1:nc
259 if simplify(coeff_yunits(jj) * (new_xunits.^(nc-jj))) ~= new_yunits
260 error('### Inconsistent units either between coefficients')
261 end
262 end
263 end
264 pl.pset('yunits', new_yunits);
265 otherwise
266 end
267
268 % Make an analysis object
269 a.data = p;
270 % Set xunits
271 a.setXunits(pl.find('xunits'));
272 % Set yunits
273 a.setYunits(pl.find('yunits'));
274 % Simplify units
275 a.simplifyYunits(plist('Prefixes', false));
276 % Add history
277 a.addHistory(ii, pl, [], []);
278 % Set object properties from the plist
279 a.setObjectProperties(pl);
280
281
282
283 end
284