comparison m-toolbox/classes/@mfir/mfir.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 % MFIR FIR filter object class constructor.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: MFIR FIR filter object class constructor.
5 % Create a mfir object.
6 %
7 % CONSTRUCTORS:
8 %
9 % f = mfir() - creates an empty mfir object.
10 % f = mfir(fi) - creates a copy of the input mfir object, fi.
11 % f = mfir(a) - creates a mfir object based on the magnitude of
12 % the input AO/fsdata object a.
13 % f = mfir(pzm) - creates a mfir object from a pole/zero model
14 % f = mfir(c,fs) - creates an mfir object based on the vector of input
15 % coefficients c.
16 % The sample rate for which the filter is designed
17 % should be specified as well.
18 % f = mfir(filename) - creates an mfir object loading the mfir object from disk
19 % f = mfir(pl) - creates an mfir object from the description given
20 % in the parameter list.
21 %
22 %
23 % Parameter sets examples for plist constructor:
24 %
25 % EXAMPLE 1: Create an order 1 highpass filter with high frequency gain 2.
26 % Filter is designed for 10 Hz sampled data and has a cut-off
27 % frequency of 0.2 Hz.
28 %
29 % >> pl = plist('type', 'highpass', ...
30 % 'order', 128, ...
31 % 'gain', 2.0, ...
32 % 'fs', 10, ...
33 % 'fc', 0.2);
34 % >> f = mfir(pl)
35 %
36 % NOTES:
37 % ** The convention used here for naming the filter coefficients is
38 % the opposite to MATLAB's convention. The recursion formula
39 % for this convention is
40 %
41 % y(n) = a(1)*x(n) + a(2)*x(n-1) + ... + a(na+1)*x(n-na)
42 %
43 % <a href="matlab:utils.helper.displayMethodInfo('mfir', 'mfir')">Parameters Description</a>
44 %
45 % VERSION: $Id: mfir.m,v 1.117 2011/08/15 12:22:57 hewitson Exp $
46 %
47 % SEE ALSO: miir, ltpda_filter, ltpda_uoh, ltpda_uo, ltpda_obj, plist
48 %
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50
51 classdef mfir < ltpda_filter
52
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 % Property definition %
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56
57 %---------- Public (read/write) Properties ----------
58 properties
59 end
60
61 %---------- Protected read-only Properties ----------
62 properties (SetAccess = protected)
63 gd = []; %
64 end
65
66 %---------- Protected read-only Properties ----------
67 properties (SetAccess = protected, Dependent = true)
68 ntaps % number of coefficients in the filter
69 end
70
71 %---------- Private Properties ----------
72 properties (GetAccess = protected, SetAccess = protected)
73 end
74
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % Check property setting %
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 methods
79 function set.gd(obj, val)
80 if ~isempty(val)
81 if ~isnumeric(val) || ~isreal(val)
82 error('### The value for the property ''gd'' must be a real number(s)');
83 end
84 end
85 obj.gd = val;
86 end
87 function set.ntaps(obj, val)
88 error('### Don''t set the property ''ntaps''. It is computed by length(a).');
89 end
90 end
91
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % Compute the Dependent properties %
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95
96 methods
97 function val = get.ntaps(obj)
98 val = length(obj.a);
99 end
100 end
101
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 % Constructor %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105
106 methods
107 function obj = mfir(varargin)
108
109 import utils.const.*
110 utils.helper.msg(msg.OMNAME, 'running %s/%s', mfilename('class'), mfilename);
111
112 % Collect all mfir objects
113 [fs, invars, rest] = utils.helper.collect_objects(varargin(:), 'mfir');
114
115 if isempty(rest) && ~isempty(fs)
116 % Do copy constructor and return
117 utils.helper.msg(msg.OPROC1, 'copy constructor');
118 obj = copy(fs, 1);
119 for kk=1:numel(obj)
120 obj(kk).addHistory(mfir.getInfo('mfir', 'None'), [], [], obj(kk).hist);
121 end
122 return
123 end
124
125 switch nargin
126 case 0
127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 %%%%%%%%%%%%%%%%%%%%%%%%%%% no inputs %%%%%%%%%%%%%%%%%%%%%%%%%%%
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 utils.helper.msg(msg.OPROC1, 'empty constructor');
131 obj.addHistory(mfir.getInfo('mfir', 'None'), plist(), [], []);
132
133 case 1
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 %%%%%%%%%%%%%%%%%%%%%%%%%%% one input %%%%%%%%%%%%%%%%%%%%%%%%%%%
136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137
138 if ischar(varargin{1})
139 %%%%%%%%%% f = mfir('foo.xml') %%%%%%%%%%
140 %%%%%%%%%% f = mfir('foo.mat') %%%%%%%%%%
141 utils.helper.msg(msg.OPROC1, 'constructing from file %s', varargin{1});
142 obj = fromFile(obj, plist('filename', varargin{1}));
143
144 elseif isa(varargin{1}, 'ao')
145 %%%%%%%%%% f = mfir(ao-object) %%%%%%%%%%
146 utils.helper.msg(msg.OPROC1, 'constructing from AO %s', varargin{1}.name);
147 pl = mfir.getDefaultPlist('From AO');
148 pl = pset(pl, 'AO', varargin{1});
149 obj = fromAO(obj, pl);
150
151 elseif isstruct(varargin{1})
152 %%%%%%%%%% f = mfir(struct) %%%%%%%%%%
153 utils.helper.msg(msg.OPROC1, 'constructing from struct');
154 obj = fromStruct(obj, varargin{1});
155
156 elseif isa(varargin{1}, 'pzmodel')
157 %%%%%%%%%% f = mfir(pzmodel-object) %%%%%%%%%%
158 utils.helper.msg(msg.OPROC1, 'constructing from pzmodel %s', varargin{1}.name);
159 obj = fromPzmodel(obj, plist('pzmodel', varargin{1}));
160
161 elseif isa(varargin{1}, 'plist')
162 %%%%%%%%%% f = mfir(plist-object) %%%%%%%%%%
163 %----------- plist
164
165 pl = varargin{1};
166
167 % Selection of construction method
168 if pl.isparam('filename')
169 utils.helper.msg(msg.OPROC1, 'constructing from file %s', pl.find('filename'));
170 obj = fromFile(obj, pl);
171
172 elseif pl.isparam('hostname') || pl.isparam('conn')
173 utils.helper.msg(msg.OPROC1, 'constructing from repository');
174 obj = obj.fromRepository(pl);
175
176 elseif pl.isparam('type')
177 utils.helper.msg(msg.OPROC1, 'constructing standard %s filter', pl.find('type'));
178 obj = fromStandard(obj, pl);
179
180 elseif pl.isparam('pzmodel')
181 utils.helper.msg(msg.OPROC1, 'constructing from pzmodel object');
182 obj = fromPzmodel(obj, pl);
183
184 elseif pl.isparam('a')
185 utils.helper.msg(msg.OPROC1, 'constructing from A/B coefficients');
186 obj = fromA(obj, pl);
187
188 elseif pl.isparam('AO')
189 utils.helper.msg(msg.OPROC1, 'constructing from AO');
190 obj = fromAO(obj, pl);
191
192 elseif pl.isparam('built-in')
193 utils.helper.msg(msg.OPROC1, 'constructing from built-in model');
194 obj = fromModel(obj, pl);
195
196 elseif pl.isparam('plist')
197 %--- Construct from plist
198 % if the plist is empty, we return an empty MFIR
199 ipl = find(pl, 'plist');
200 if nparams(ipl) == 0
201 obj = mfir();
202 else
203 % do plist constructor
204 obj = mfir(ipl);
205 end
206
207 else
208 obj.setObjectProperties(pl);
209 obj.addHistory(mfir.getInfo('mfir', 'None'), pl, [], []);
210 end
211
212 else
213 error('### Unknown 1 argument constructor.');
214 end
215 case 2
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 %%%%%%%%%%%%%%%%%%%%%%%%%%% two input %%%%%%%%%%%%%%%%%%%%%%%%%%%
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219
220 if isa(varargin{1}, 'ao')
221 %%%%%%%%%% f = mfir(ao-object, plist-object) %%%%%%%%%%
222 utils.helper.msg(msg.OPROC1, 'constructing from AO %s', varargin{1}.name);
223 obj = fromAO(obj, pset(varargin{2}, 'AO', varargin{1}));
224
225 elseif (isa(varargin{1}, 'database') || isa(varargin{1}, 'mpipeline.repository.RepositoryConnection')) ...
226 && isnumeric(varargin{2})
227 %%%%%%%%%% f = mfir(<database-object>, [IDs]) %%%%%%%%%%
228 utils.helper.msg(msg.OPROC1, 'retrieve from repository');
229 obj = obj.fromRepository(plist('conn', varargin{1}, 'id', varargin{2}));
230
231 elseif isa(varargin{1}, 'pzmodel') && isa(varargin{2}, 'plist')
232 %%%%%%%%%% f = mfir(pzmodel-object, plist-object) %%%%%%%%%%
233 utils.helper.msg(msg.OPROC1, 'constructing from pzmodel %s', varargin{1}.name);
234 obj = fromPzmodel(obj, combine(plist('pzmodel', varargin{1}), varargin{2}));
235
236 elseif isa(varargin{1}, 'mfir') && isa(varargin{2}, 'plist') && isempty(varargin{2}.params)
237 %%%%%%%%%% f = mfir(mfir, <empty-plist>) %%%%%%%%%%
238 obj = mfir(varargin{1});
239
240 elseif isnumeric(varargin{1})
241 %%%%%%%%%% f = mfir(a, fs) %%%%%%%%%%
242 utils.helper.msg(msg.OPROC1, 'constructing from A coefficients');
243 obj = fromA(obj, plist('A', varargin{1}, 'fs', varargin{2}));
244
245 elseif isa(varargin{1}, 'org.apache.xerces.dom.DeferredElementImpl') && ...
246 isa(varargin{2}, 'history')
247 %%%%%%%%%% obj = mfir(DOM node, history-objects) %%%%%%%%%%
248 obj = fromDom(obj, varargin{1}, varargin{2});
249
250 elseif isa(varargin{1}, 'ltpda_uoh') && isa(varargin{2}, 'plist')
251 %%%%%%%%%%% mfir(<ltpda_uoh>-object, plist-object) %%%%%%%%%%
252 % always recreate from plist
253
254 % If we are trying to load from file, and the file exists, do
255 % that. Otherwise, copy the input object.
256 if varargin{2}.isparam('filename')
257 if exist(fullfile('.', find(varargin{2}, 'filename')), 'file')==2
258 obj = mfir(varargin{2});
259 else
260 obj = mfir(varargin{1});
261 end
262 else
263 obj = mfir(varargin{2});
264 end
265 else
266 error('### Unknown 2 argument constructor.');
267 end
268
269 otherwise
270 [firs, invars, rest] = utils.helper.collect_objects(args, 'mfir');
271
272 %%% Do we have a list of MFIR objects as input
273 if ~isempty(firs) && isempty(rest)
274 obj = mfir(firs);
275 else
276 error('### Unknown number of arguments.');
277 end
278 end
279
280 end % End constructor
281 end % End public methods
282
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284 % Methods (static) %
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 methods (Static)
287
288 function mdls = getBuiltInModels(varargin)
289 mdls = ltpda_uo.getBuiltInModels('mfir');
290 end
291
292 function out = VEROUT()
293 out = '$Id: mfir.m,v 1.117 2011/08/15 12:22:57 hewitson Exp $';
294 end
295
296 function ii = getInfo(varargin)
297 ii = utils.helper.generic_getInfo(varargin{:}, 'mfir');
298 end
299
300 function out = SETS()
301 out = [SETS@ltpda_uoh, ...
302 {'From Standard Type'}, ...
303 {'From Pzmodel'}, ...
304 {'From A'}, ...
305 {'From AO'}];
306 end
307
308
309 function plout = getDefaultPlist(set)
310 persistent pl;
311 persistent lastset;
312 if exist('pl', 'var')==0 || isempty(pl) || ~strcmp(lastset, set)
313 pl = mfir.buildplist(set);
314 lastset = set;
315 end
316 plout = pl;
317 end
318
319 function out = buildplist(set)
320
321 if ~utils.helper.ismember(lower(mfir.SETS), lower(set))
322 error('### Unknown set [%s]', set);
323 end
324
325 out = plist();
326 out = mfir.addGlobalKeys(out);
327 out = buildplist@ltpda_uoh(out, set);
328
329 switch lower(set)
330 case 'from standard type'
331
332 % Type
333 p = param({'type','Choose the filter type.'}, {2, {'highpass', 'lowpass', 'bandpass', 'bandreject'}, paramValue.SINGLE});
334 out.append(p);
335
336 % Fc
337 p = param({'fc','The roll-off frequency [Hz].'}, paramValue.DOUBLE_VALUE([0.1 0.4]));
338 out.append(p);
339
340 % Gain
341 p = param({'gain','The gain of the filter.'}, paramValue.DOUBLE_VALUE(1));
342 out.append(p);
343
344 % Fs
345 p = param({'fs','The sampling frequency to design for.'}, paramValue.DOUBLE_VALUE(1));
346 out.append(p);
347
348 % Order
349 p = param({'order', 'The filter order.'}, paramValue.DOUBLE_VALUE(128));
350 out.append(p);
351
352 % Iunits
353 p = param({'iunits','The input units of the filter.'}, paramValue.EMPTY_STRING);
354 out.append(p);
355
356 % Ounits
357 p = param({'ounits','The output units of the filter.'}, paramValue.EMPTY_STRING);
358 out.append(p);
359
360 case 'from pzmodel'
361
362 % Pzmodel
363 p = param({'pzmodel', 'A pole/zero model to design from.'}, {1, {pzmodel}, paramValue.OPTIONAL});
364 out.append(p);
365
366 % Fs
367 p = param({'fs','The sampling frequency to design for.'}, paramValue.EMPTY_DOUBLE);
368 out.append(p);
369
370 % Iunits
371 p = param({'iunits','The input units of the transfer function.'}, paramValue.EMPTY_STRING);
372 out.append(p);
373
374 % Ounits
375 p = param({'ounits','The output units of the transfer function.'}, paramValue.EMPTY_STRING);
376 out.append(p);
377
378 case 'from ao'
379
380 % AO
381 p = param({'AO', 'The AO object to design from.'}, {1, {ao}, paramValue.OPTIONAL});
382 out.append(p);
383
384 % N
385 p = param({'N', 'The filter order.'}, paramValue.DOUBLE_VALUE(512));
386 out.append(p);
387
388 % Method
389 p = param({'Method', ['The filter design method:<ul>'...
390 '<li>''frequency-sampling'' - uses <a href="matlab:doc(''fir2'')">fir2()</a></li>',...
391 '<li>''least-squares'' - uses <a href="matlab:doc(''firls'')">firls()</a></li>',...
392 '<li>''Parks-McClellan'' - uses <a href="matlab:doc(''firpm'')">firpm()</a></li></ul>']}, {1, {'frequency-sampling', 'least-squares', 'Parks-McClellan'}, paramValue.SINGLE});
393 out.append(p);
394
395 % Win
396 p = param({'win', 'A window to design with when using the frequency-sampling method.'}, paramValue.WINDOW);
397 out.append(p);
398
399 % PSLL
400 p = param({'psll', 'If you specify a Kaiser window, you can also specify the PSLL.'}, paramValue.DOUBLE_VALUE(100));
401 out.append(p);
402
403 % Iunits
404 p = param({'iunits','The input units of the transfer function.'}, paramValue.EMPTY_STRING);
405 out.append(p);
406
407 % Ounits
408 p = param({'ounits','The output units of the transfer function.'}, paramValue.EMPTY_STRING);
409 out.append(p);
410
411 case 'from a'
412
413 % A
414 p = param({'a','Vector of A coefficients.'}, paramValue.EMPTY_DOUBLE);
415 out.append(p);
416
417 % Fs
418 p = param({'fs','Sampling frequency of the filter.'}, paramValue.EMPTY_DOUBLE);
419 out.append(p);
420
421 % Iunits
422 p = param({'iunits','The input units of the transfer function.'}, paramValue.EMPTY_STRING);
423 out.append(p);
424
425 % Ounits
426 p = param({'ounits','The output units of the transfer function.'}, paramValue.EMPTY_STRING);
427 out.append(p);
428
429 end
430 end
431
432 function obj = initObjectWithSize(n,m)
433 obj = mfir.newarray([n m]);
434 end
435
436 end % End static methods
437
438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
439 % Methods (static, private) %
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
441
442 methods (Static, Access=private)
443 plo = parseFilterParams(pl)
444 end
445
446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
447 % Methods (static, hidden) %
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
449
450 methods (Static = true, Hidden = true)
451 varargout = loadobj(varargin)
452 varargout = update_struct(varargin);
453 end
454
455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
456 % Methods (public) %
457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458
459 methods
460 varargout = char(varargin)
461 varargout = copy(varargin)
462 varargout = display(varargin)
463
464 varargout = setGd(varargin)
465 end
466
467 methods (Hidden = true)
468 varargout = attachToDom(varargin)
469 end
470
471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
472 % Methods (protected) %
473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474
475 methods (Access = protected)
476 varargout = fromStruct(varargin)
477 varargout = fromDom(varargin)
478 end
479
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481 % Methods (private) %
482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
483
484 methods (Access = private)
485 f = fromPzmodel(f, pli)
486 f = fromA(f, pli)
487 f = fromAO(f, pli)
488 f = fromStandard(f, pli)
489
490 f = mklowpass(f, pl)
491 f = mkhighpass(f, pl)
492 f = mkbandpass(f, pl)
493 f = mkbandreject(f, pl)
494 end
495
496 end
497