comparison m-toolbox/classes/@rational/rational.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children a71a40911c27
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % RATIONAL rational representation of a transfer function.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: RATIONAL rational representation of a transfer function.
5 %
6 % a(1)s^m + a(2)s^{m-1} + ... + a(m+1)
7 % H(s) = --------------------------------------
8 % b(1)s^n + b(2)s^{n-1} + ... + b(n+1)
9 %
10 % CONSTRUCTOR:
11 %
12 % r = rational() - creates an empty rational object
13 % r = rational(nun, den) - construct from numerator and
14 % denominator coefficients
15 % r = rational(num, den, 'name') - construct including name
16 % r = rational(num, den, - construct from num, den, and io-units
17 % iunits, ounits)
18 % r = rational(pl) - create a rational object from the
19 % description given in the parameter list.
20 % r = rational(pzm) - convert the TF described by the
21 % pzmodel into a rational TF.
22 %
23 % Example constructor plists:
24 %
25 % Example: plist('filename', 'rational1.xml')
26 % Example: plist('filename', 'rational1.mat')
27 % Example: pzm = pzmodel(1, {1 2 3}, {4 5})
28 % plist('pzmodel', pzm)
29 %
30 % Example: pzm = pzmodel(1, {1 2 3}, {4 5})
31 % pl = plist('pzmodel', pzm)
32 % plist('PLIST', pl)
33 %
34 % <a href="matlab:utils.helper.displayMethodInfo('rational', 'rational')">Parameters Description</a>
35 %
36 % VERSION: $Id: rational.m,v 1.45 2011/08/15 12:58:59 hewitson Exp $
37 %
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39
40 classdef rational < ltpda_tf
41
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 % Property definition %
44 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45
46 %---------- Public (read/write) Properties ----------
47 properties
48 end
49
50 %---------- Protected read-only Properties ----------
51 properties (SetAccess = protected)
52 num = []; % numerator coefficients [a]
53 den = []; % denominator coefficients [b]
54 end
55
56 %---------- Private Properties ----------
57 properties (GetAccess = protected, SetAccess = protected)
58 end
59
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 % Check property setting %
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63
64 methods
65 function set.num(obj, val)
66 if ~isnumeric(val) && ~isempty(val)
67 error('### The value for the property ''num'' must be a numeric array.');
68 end
69 obj.num = val;
70 end
71 function set.den(obj, val)
72 if ~isnumeric(val) && ~isempty(val)
73 error('### The value for the property ''den'' must be a numeric array.');
74 end
75 obj.den = val;
76 end
77 end
78
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % Constructor %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82
83 methods
84 function obj = rational(varargin)
85
86 import utils.const.*
87 utils.helper.msg(msg.OMNAME, 'running %s/%s', mfilename('class'), mfilename);
88
89 % Collect all pzmodel objects
90 [rationals, invars, rest] = utils.helper.collect_objects(varargin(:), 'rational');
91
92 if isempty(rest) && ~isempty(rationals)
93 % Do copy constructor and return
94 utils.helper.msg(msg.OPROC1, 'copy constructor');
95 obj = copy(rationals, 1);
96 for kk=1:numel(obj)
97 obj(kk).addHistory(rational.getInfo('rational', 'None'), [], [], obj(kk).hist);
98 end
99 return
100 end
101
102 switch nargin
103 case 0
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 %%%%%%%%%%%% Zero inputs %%%%%%%%%%%%%%%
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 utils.helper.msg(msg.OPROC1, 'empty constructor');
108 obj.addHistory(rational.getInfo('rational', 'None'), plist(), [], []);
109
110 case 1
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 %%%%%%%%%%%% One inputs %%%%%%%%%%%%%%%
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114
115 if ischar(varargin{1})
116 %%%%%%%%%% pzm = pzmodel('foo.mat') %%%%%%%%%%
117 %%%%%%%%%% pzm = pzmodel('foo.xml') %%%%%%%%%%
118 utils.helper.msg(msg.OPROC1, 'constructing from file %s', varargin{1});
119 obj = fromFile(obj, varargin{1});
120
121 elseif isstruct(varargin{1})
122 %%%%%%%%%% r = rational(struct) %%%%%%%%%%
123 utils.helper.msg(msg.OPROC1, 'constructing from struct');
124 obj = fromStruct(obj, varargin{1});
125
126 elseif isa(varargin{1}, 'pzmodel')
127 %%%%%%%%%% r = rational(pzm) %%%%%%%%%%
128 utils.helper.msg(msg.OPROC1, 'constructing from pzmodel');
129 pl = plist('pzmodel', varargin{1});
130 obj = fromPzmodel(obj, pl);
131
132 elseif isa(varargin{1}, 'parfrac')
133 %%%%%%%%%% r = rational(pf) %%%%%%%%%%
134 utils.helper.msg(msg.OPROC1, 'constructing from parfrac');
135 pl = plist('parfrac', varargin{1});
136 obj = fromParfrac(obj, pl);
137
138 elseif isa(varargin{1}, 'plist')
139 %%%%%%%%%% r = rational(plist) %%%%%%%%%%
140 pl = varargin{1};
141
142 % Selection of construction method
143 if pl.isparam('filename')
144 utils.helper.msg(msg.OPROC1, 'constructing from file %s', pl.find('filename'));
145 obj = fromFile(obj, pl);
146
147 elseif pl.isparam('hostname') || pl.isparam('conn')
148 utils.helper.msg(msg.OPROC1, 'constructing from repository %s', pl.find('hostname'));
149 obj = obj.fromRepository(pl);
150
151 elseif pl.isparam('num') || pl.isparam('den')
152 utils.helper.msg(msg.OPROC1, 'constructing from coefficients');
153 obj = fromCoefficients(obj, pl);
154
155 elseif pl.isparam('pzmodel')
156 utils.helper.msg(msg.OPROC1, 'constructing from pole/zero model');
157 obj = fromPzmodel(obj, pl);
158
159 elseif pl.isparam('parfrac')
160 utils.helper.msg(msg.OPROC1, 'constructing from parfrac object');
161 obj = fromParfrac(obj, pl);
162
163 elseif pl.isparam('built-in')
164 utils.helper.msg(msg.OPROC1, 'constructing from built-in model');
165 obj = fromModel(obj, pl);
166
167 elseif pl.isparam('Plist')
168 ipl = find(pl, 'Plist');
169 obj = rational(ipl);
170
171 else
172 obj.setObjectProperties(pl);
173 obj.addHistory(rational.getInfo('rational', 'None'), pl, [], []);
174 end
175 else
176 error('### Unknown single argument constructor.');
177 end
178
179 case 2
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 %%%%%%%%%%%%% Two inputs %%%%%%%%%%%%%%%
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 if (isa(varargin{1}, 'database') || isa(varargin{1}, 'mpipeline.repository.RepositoryConnection')) && isnumeric(varargin{2})
184 %%%%%%%%%% f = rational(<database-object>, [IDs]) %%%%%%%%%%
185 utils.helper.msg(msg.OPROC1, 'retrieve from repository');
186 obj = obj.fromRepository(plist('conn', varargin{1}, 'id', varargin{2}));
187
188 elseif isnumeric(varargin{1}) && isnumeric(varargin{2})
189 %%%%%%%%% f = rational(num,den) %%%%%%%%%
190 obj = fromCoefficients(obj, plist('num', varargin{1}, 'den', varargin{2}));
191
192 elseif isa(varargin{1}, 'rational') && isa(varargin{2}, 'plist') && isempty(varargin{2}.params)
193 % pass to copy constructor
194 obj = rational(varargin{1});
195
196 elseif isa(varargin{1}, 'org.apache.xerces.dom.DeferredElementImpl') && ...
197 isa(varargin{2}, 'history')
198 %%%%%%%%%% obj = rational(DOM node, history-objects) %%%%%%%%%%
199 obj = fromDom(obj, varargin{1}, varargin{2});
200
201 elseif isa(varargin{1}, 'ltpda_uoh') && isa(varargin{2}, 'plist')
202 %%%%%%%%%%% rational(<ltpda_uoh>-object, plist-object) %%%%%%%%%%
203 % always recreate from plist
204
205 % If we are trying to load from file, and the file exists, do
206 % that. Otherwise, copy the input object.
207 if varargin{2}.isparam('filename')
208 if exist(fullfile('.', find(varargin{2}, 'filename')), 'file')==2
209 obj = rational(varargin{2});
210 else
211 obj = rational(varargin{1});
212 end
213 else
214 obj = rational(varargin{2});
215 end
216 else
217 error('### Unknown 2 argument constructor.');
218 end
219
220 case 3
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 %%%%%%%%%%%% Three inputs %%%%%%%%%%%%%%
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224
225 %%%%%%%%%% r = rational(num, den, name) %%%%%%%%%%
226 utils.helper.msg(msg.OPROC1, 'constructing from coefficients');
227 pl = plist('num', varargin{1}, 'den', varargin{2}, 'name', varargin{3});
228 obj = fromCoefficients(obj, pl);
229
230 case 4
231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 %%%%%%%%%%%% Four inputs %%%%%%%%%%%%%%%
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 %%%%%%%%%% pzm = rational(num, den, iunits, ounits) %%%%%%%%%%
235 utils.helper.msg(msg.OPROC1, 'constructing from coefficients');
236 pl = plist('num', varargin{1}, 'den', varargin{2}, 'iunits', varargin{3}, 'ounits', varargin{4});
237 obj = fromCoefficients(obj, pl);
238
239 case 5
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 %%%%%%%%%%%% five inputs %%%%%%%%%%%%%%%
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 %%%%%%%%%% pzm = rational(num, den, name, iunits, ounits) %%%%%%%%%%
244 utils.helper.msg(msg.OPROC1, 'constructing from coefficients');
245 pl = plist('num', varargin{1}, 'den', varargin{2}, 'name', varargin{3}, 'iunits', varargin{4}, 'ounits', varargin{5});
246 obj = fromCoefficients(obj, pl);
247
248 otherwise
249 [rationals, invars, rest] = utils.helper.collect_objects(varargin, 'rational');
250
251 %%% Do we have a list of RATIONAL objects as input
252 if ~isempty(rationals) && isempty(rest)
253 obj = rational(rationals);
254 else
255 error('### Unknown number of arguments.');
256 end
257 end
258
259 end % End constructor
260
261 end
262
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 % Methods (static) %
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 methods (Static)
267
268 function mdls = getBuiltInModels(varargin)
269 mdls = ltpda_uo.getBuiltInModels('rational');
270 end
271
272 function out = VEROUT()
273 out = '$Id: rational.m,v 1.45 2011/08/15 12:58:59 hewitson Exp $';
274 end
275
276 function ii = getInfo(varargin)
277 ii = utils.helper.generic_getInfo(varargin{:}, 'rational');
278 end
279
280 function out = SETS()
281 out = [SETS@ltpda_uoh, ...
282 {'From Pzmodel'}, ...
283 {'From Coefficients'}, ...
284 {'From Parfrac'}];
285 end
286
287 function plout = getDefaultPlist(set)
288 persistent pl;
289 persistent lastset;
290 if exist('pl', 'var')==0 || isempty(pl) || ~strcmp(lastset, set)
291 pl = rational.buildplist(set);
292 lastset = set;
293 end
294 plout = pl;
295 end
296
297 function out = buildplist(set)
298
299 if ~utils.helper.ismember(lower(rational.SETS), lower(set))
300 error('### Unknown set [%s]', set);
301 end
302
303 out = plist();
304 out = rational.addGlobalKeys(out);
305 out = buildplist@ltpda_uoh(out, set);
306
307 switch lower(set)
308 case 'from pzmodel'
309 % pzmodel
310 p = param({'pzmodel','Construct from a pole/zero model.'}, {1, {pzmodel}, paramValue.OPTIONAL});
311 out.append(p);
312 % Iunits
313 p = param({'iunits','The input units of the model.'}, paramValue.EMPTY_STRING);
314 out.append(p);
315
316 % Ounits
317 p = param({'ounits','The output units of the model.'}, paramValue.EMPTY_STRING);
318 out.append(p);
319
320 case 'from parfrac'
321 % parfrac
322 p = param({'parfrac','Construct from a partial fraction model.'}, {1, {parfrac}, paramValue.OPTIONAL});
323 out.append(p);
324 % Iunits
325 p = param({'iunits','The input units of the model.'}, paramValue.EMPTY_STRING);
326 out.append(p);
327
328 % Ounits
329 p = param({'ounits','The output units of the model.'}, paramValue.EMPTY_STRING);
330 out.append(p);
331
332 case 'from coefficients'
333 % Num
334 p = param({'num','Vector of coefficients.'}, paramValue.EMPTY_DOUBLE);
335 out.append(p);
336
337 % Den
338 p = param({'den','Vector of coefficients.'}, paramValue.EMPTY_DOUBLE);
339 out.append(p);
340 % Iunits
341 p = param({'iunits','The input units of the model.'}, paramValue.EMPTY_STRING);
342 out.append(p);
343
344 % Ounits
345 p = param({'ounits','The output units of the model.'}, paramValue.EMPTY_STRING);
346 out.append(p);
347 end
348 end % function out = getDefaultPlist(varargin)
349
350 function obj = initObjectWithSize(n,m)
351 obj = rational.newarray([n m]);
352 end
353
354 end % End static methods
355
356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357 % Methods (static, private) %
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359
360 methods (Static, Access=private)
361 end % End static, private methods
362
363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364 % Methods (static, hidden) %
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366
367 methods (Static = true, Hidden = true)
368 varargout = loadobj(varargin)
369 varargout = update_struct(varargin);
370 end
371
372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 % Methods (public) %
374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375 methods
376 varargout = char(varargin)
377 varargout = display(varargin)
378 varargout = copy(varargin)
379 end
380
381 methods (Hidden = true)
382 varargout = attachToDom(varargin)
383 end
384
385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386 % Methods (protected) %
387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388 methods (Access = protected)
389 varargout = respCore(varargin)
390 varargout = fromStruct(varargin)
391 varargout = fromDom(varargin)
392 end
393
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395 % Methods (private) %
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 methods (Access = private)
398 % Constructors
399 varargout = fromCoefficients(varargin)
400 varargout = fromPzmodel(varargin)
401 varargout = fromParfrac(varargin)
402 end
403
404 end % End classdef
405