comparison m-toolbox/classes/@ao/filter.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 % FILTER overrides the filter function for analysis objects.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: FILTER overrides the filter function for analysis objects.
5 % Applies the input digital IIR/FIR filter to the input analysis
6 % object. If the input analysis object contains a
7 % time-series (tsdata) then the filter is applied using the normal
8 % recursion algorithm. The output analysis object contains a tsdata
9 % object.
10 %
11 % If the input analysis object contains a frequency-series (fsdata)
12 % then the response of the filter is computed and then multiplied
13 % with the input frequency series. The output analysis object
14 % contains a frequency series.
15 %
16 % CALL: >> [b, filt] = filter(a,pl)
17 % >> [b, filt] = filter(a,filt,pl)
18 % >> b = filter(a,pl)
19 %
20 % INPUTS: pl - a parameter list
21 % a - input analysis object
22 %
23 % OUTPUTS: filt - a copy of the input filter object with the
24 % history values filled in.
25 % (only possible if the ouput is a single AO)
26 % b - output analysis object containing the filtered data.
27 %
28 % PROCINFO: The input filter object with the history values filled in are
29 % always stored with a plist in the 'procinfo' property of the AO.
30 % The key of the plist to get the filter is 'Filter'.
31 %
32 %
33 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'filter')">Parameters Description</a>
34 %
35 % VERSION: $Id: filter.m,v 1.86 2011/04/08 08:56:14 hewitson Exp $
36 %
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38
39 function varargout = filter(varargin)
40
41 % Check if this is a call for parameters
42 if utils.helper.isinfocall(varargin{:})
43 varargout{1} = getInfo(varargin{3});
44 return
45 end
46
47 import utils.const.*
48 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
49
50 % Collect input variable names
51 in_names = cell(size(varargin));
52 for ii = 1:nargin,in_names{ii} = inputname(ii);end
53
54 % Collect all AOs and plists
55 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
56 [pl, pl_invars] = utils.helper.collect_objects(varargin(:), 'plist', in_names);
57 [fobjs, f_invars] = utils.helper.collect_objects(varargin(:), 'ltpda_filter', in_names);
58 [fbobjs, fb_invars] = utils.helper.collect_objects(varargin(:), 'filterbank', in_names);
59 [mobjs, m_invars] = utils.helper.collect_objects(varargin(:), 'matrix', in_names);
60
61 % Make copies or handles to inputs
62 bs = copy(as, nargout);
63
64 % combine plists
65 pl = parse(pl, getDefaultPlist());
66
67 % Filter with a filterbank object or a matrix
68 if ~isempty(fbobjs)
69 fobjs = fbobjs.filters;
70 pl.pset('bank', fbobjs.type);
71 elseif ~isempty(mobjs)
72 fobjs = mobjs.objs;
73 % check we do not have more than one object into the matrix, if this is
74 % the case the problem is considered a N-dimensional filtering problem
75 % that can be solved by matrix/filter
76 if numel(mobjs.objs)>1
77 error(['### Filter matrix has more than one object. '...
78 'This seems to be a N-dimensional filtering problem that has to be solved with matrix/filter. '...
79 'Type help matrix/filter for more information ###']);
80 end
81 if isa(fobjs,'filterbank') % in case of filterbanks
82 pl.pset('bank', fobjs.type);
83 fobjs = fobjs.filters;
84 end
85 end
86
87 if isempty(fobjs)
88 fobjs = find(pl, 'filter');
89 % check if we have filterbank or matrix
90 if isa(fobjs,'filterbank') % in case of filterbank
91 pl.pset('bank', fobjs.type);
92 fobjs = fobjs.filters;
93 elseif isa(fobjs,'matrix') % in case of matrix
94 fobjs = fobjs.objs;
95 % check we do not have more than one object into the matrix, if this is
96 % the case the problem is considered a N-dimensional filtering problem
97 % that can be solved by matrix/filter
98 if numel(fobjs)>1
99 error(['### Filter matrix has more than one object. '...
100 'This seems to be a N-dimensional filtering problem that has to be solved with matrix/filter. '...
101 'Type help matrix/filter for more information ###']);
102 end
103 if isa(fobjs,'filterbank') % in case of filterbanks
104 pl.pset('bank', fobjs.type);
105 fobjs = fobjs.filters;
106 end
107 end
108 end
109
110
111 % decide to initialize or not
112 init = utils.prog.yes2true(find(pl, 'initialize'));
113
114 % check inputs
115 if ~isa(fobjs, 'miir') && ~isa(fobjs, 'mfir')
116 error('### the filter input should be an miir/mfir object.');
117 end
118
119 if numel(bs) > 1 && nargout > 1
120 error('### It is only possible to output a bank of filters when applied to a single AO.');
121 end
122
123 for j=1:numel(bs)
124
125 % Copy filter so we can change it
126 fobjs_copy = copy(fobjs, 1);
127 % keep the history to suppress the history of the intermediate steps
128 inhist = bs(j).hist;
129
130 if isa(bs(j).data, 'tsdata')
131 %------------------------------------------------------------------------
132 %------------------------ Time-series filter ------------------------
133 %------------------------------------------------------------------------
134 % get input data
135 if isa(fobjs_copy, 'mfir')
136 % apply filter
137 utils.helper.msg(msg.PROC1, 'filtering with FIR filter');
138 [bs(j).data.y, Zf] = filter(fobjs_copy.a, 1, bs(j).data.y, fobjs_copy.histout);
139 % remove group delay
140 if strcmpi(find(pl, 'gdoff'), 'no')
141 gd = floor(fobjs_copy.gd);
142 bs(j).data.setXY(bs(j).data.getX(1:end-gd),bs(j).data.getY(1+gd:end));
143 bs(j).data.collapseX;
144 end
145 % set units of the output data as we go
146 bs(j).data.setYunits(bs(j).data.yunits.*fobjs_copy.ounits./fobjs_copy.iunits);
147
148 else %if isa(fobjs_copy, 'miir')
149 utils.helper.msg(msg.PROC1, 'filtering with IIR filter');
150 % initialise data vector
151 bank = find(pl, 'bank');
152 switch lower(bank)
153 case 'parallel'
154 y = zeros(size(bs(j).data.getY));
155 case 'serial'
156 y = ones(size(bs(j).data.getY));
157 otherwise
158 error('### Unknown filter bank option. Choose ''serial'' or ''parallel''.');
159 end
160 % Loop over filters
161 iu = fobjs_copy(1).iunits;
162 ou = fobjs_copy(1).ounits;
163 for ff = 1:numel(fobjs_copy)
164
165 % check sample rate
166 if bs(j).data.fs ~= fobjs_copy(ff).fs
167 warning('!!! Filter is designed for a different sample rate of data.');
168 % Adjust/redesign if this is a standard filter
169 fobjs_copy(ff) = fobjs_copy(ff).redesign(bs(j).data.fs);
170 end
171
172 % Choose filtering type
173 switch lower(bank)
174
175 case 'parallel'
176 % check units
177 if iu ~= fobjs_copy(ff).iunits
178 error('### Input units of each filter must match for a parallel filter bank.');
179 end
180 if ou ~= fobjs_copy(ff).ounits
181 error('### Output units of each filter must match for a parallel filter bank.');
182 end
183 % Initialise the state to avoid transients if necessary and
184 % explicitely required
185 if ((~any(fobjs_copy(ff).histout) || isempty(fobjs_copy(ff).histout)) && init)
186 zi = utils.math.iirinit(fobjs_copy(ff).a,fobjs_copy(ff).b);
187 % setting new histout
188 fobjs_copy(ff).setHistout(zi*bs(j).data.y(1));
189 end
190 % filter data
191 [yf, Zf] = filter(fobjs_copy(ff).a, fobjs_copy(ff).b, bs(j).data.y, fobjs_copy(ff).histout);
192 if ~isequal(size(yf),size(y))
193 yf = yf.';
194 end
195 y = y + yf;
196
197 case 'serial'
198 if ff == 1
199 y = bs(j).data.y;
200 end
201 % Initialise the state to avoid transients if necessary
202 if ~any(fobjs_copy(ff).histout) || isempty(fobjs_copy(ff).histout)
203 zi = utils.math.iirinit(fobjs_copy(ff).a,fobjs_copy(ff).b);
204 % setting new histout
205 fobjs_copy(ff).setHistout(zi*y(1));
206 end
207 % filter data
208 [yf, Zf] = filter(fobjs_copy(ff).a, fobjs_copy(ff).b, y, fobjs_copy(ff).histout);
209 if ~isequal(size(yf),size(y))
210 y = yf.';
211 else
212 y = yf;
213 end
214 % set units of the output data as we go
215 bs(j).data.setYunits(bs(j).data.yunits.*fobjs_copy(ff).ounits./fobjs_copy(ff).iunits);
216 otherwise
217 error('### Unknown filter bank option. Choose ''serial'' or ''parallel''.');
218 end
219 % set filter output history
220 fobjs_copy(ff).setHistout(Zf);
221 end % End loop over filters
222
223 % set output data
224 bs(j).data.setY(y);
225 % clear errors
226 bs(j).clearErrors;
227
228 % if this was a parallel filter bank, we should set the units now
229 if strcmpi(bank, 'parallel')
230 % set units of the output data
231 bs(j).data.setYunits(bs(j).data.yunits.*fobjs_copy(1).ounits./fobjs_copy(1).iunits);
232 bs(j).data.yunits.simplify;
233 end
234
235 end % End filter type
236
237 elseif isa(bs(j).data, 'fsdata')
238 %------------------------------------------------------------------------
239 %---------------------- Frequency-series filter ---------------------
240 %------------------------------------------------------------------------
241
242 utils.helper.msg(msg.PROC1, 'filtering with %s filter', upper(class(fobjs_copy)));
243
244 % apply filter
245 if numel(fobjs_copy)==1
246 bs(j) = bs(j).*resp(fobjs_copy, plist('f', bs(j).x));
247 else
248 bank = find(pl, 'bank');
249 iu = fobjs_copy(1).iunits;
250 ou = fobjs_copy(1).ounits;
251 switch lower(bank)
252 case 'parallel'
253 sfr = resp(fobjs_copy, plist('f', bs(j).x));
254 fr = sfr(1);
255 for jj = 2:numel(fobjs_copy)
256 if iu ~= fobjs_copy(jj).iunits
257 error('### Input units of each filter must match for a parallel filter bank.');
258 end
259 if ou ~= fobjs_copy(jj).ounits
260 error('### Output units of each filter must match for a parallel filter bank.');
261 end
262 fr = fr + sfr(jj);
263 end
264 bs(j) = bs(j).*fr;
265 case 'serial'
266 sfr = resp(fobjs_copy, plist('f', bs(j).x));
267 fr = sfr(1);
268 for jj = 2:numel(fobjs_copy)
269 fr = fr.*sfr(jj);
270 end
271 bs(j) = bs(j).*fr;
272 end
273 end
274
275 else
276 error('### unknown data type.');
277 end
278
279 % name for this object
280 bs(j).name = sprintf('%s(%s)', fobjs_copy.name, ao_invars{j});
281 % Collect the filters into procinfo
282 bs(j).procinfo = plist('filter', fobjs_copy);
283 % add history
284 bs(j).addHistory(getInfo('None'), pl, ao_invars(j), [inhist fobjs_copy(:).hist]);
285 end
286
287 % Set outputs
288 if nargout == 1
289 varargout{1} = bs;
290 elseif nargout == 2
291 varargout{1} = bs;
292 varargout{2} = fobjs_copy;
293 elseif nargout > 2
294 error('### wrong number of output arguments.');
295 end
296 end
297
298 %--------------------------------------------------------------------------
299 % Get Info Object
300 %--------------------------------------------------------------------------
301 function ii = getInfo(varargin)
302 if nargin == 1 && strcmpi(varargin{1}, 'None')
303 sets = {};
304 pls = [];
305 else
306 sets = {'Default'};
307 pls = getDefaultPlist;
308 end
309 % Build info object
310 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: filter.m,v 1.86 2011/04/08 08:56:14 hewitson Exp $', sets, pls);
311 end
312
313 %--------------------------------------------------------------------------
314 % Get Default Plist
315 %--------------------------------------------------------------------------
316 function plout = getDefaultPlist()
317 persistent pl;
318 if exist('pl', 'var')==0 || isempty(pl)
319 pl = buildplist();
320 end
321 plout = pl;
322 end
323
324 function pl = buildplist()
325
326 pl = plist();
327
328 % Filter
329 p = param({'filter', 'The filter(s) to apply to the data.'}, paramValue.EMPTY_STRING);
330 pl.append(p);
331
332 % GDoff
333 p = param({'GDOFF', 'Switch off correction for group delay.'}, paramValue.YES_NO);
334 p.val.setValIndex(2);
335 pl.append(p);
336
337 % Bank
338 p = param({'bank', 'Specify what type of filter bank is being applied.'}, {1, {'parallel', 'serial'}, paramValue.SINGLE});
339 pl.append(p);
340
341 % Initialize
342 p = param({'initialize', 'Initialize the filter to avoid startup transients.'}, {1, {false, true}, paramValue.SINGLE});
343 pl.append(p);
344
345 end
346
347 % PARAMETERS: filter - the filter object to use to filter the data
348 % bank - For IIR filtering, specify if the bank of filters
349 % is intended to be 'serial' or 'parallel' [default]
350 % initialize - true or false if you want the filter being
351 % automatically initialized or not.