Mercurial > hg > ltpda
comparison m-toolbox/classes/@ao/filtfilt.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 % FILTFILT overrides the filtfilt function for analysis objects. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: FILTFILT overrides the filtfilt function for analysis objects. | |
5 % Applies the input digital IIR filter to the input analysis object | |
6 % forwards and backwards. 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] = filtfilt(a,pl) | |
17 % >> b = filtfilt(a,pl) | |
18 % | |
19 % INPUTS: pl - a parameter list | |
20 % a - input analysis object | |
21 % | |
22 % OUTPUTS: filt - a copy of the input filter object with the | |
23 % history values filled in. | |
24 % b - output analysis object containing the filtered data. | |
25 % | |
26 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'filtfilt')">Parameters Description</a> | |
27 % | |
28 % VERSION: $Id: filtfilt.m,v 1.48 2011/05/12 13:20:42 luigi Exp $ | |
29 % | |
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
31 | |
32 function varargout = filtfilt(varargin) | |
33 | |
34 % Check if this is a call for parameters | |
35 if utils.helper.isinfocall(varargin{:}) | |
36 varargout{1} = getInfo(varargin{3}); | |
37 return | |
38 end | |
39 | |
40 import utils.const.* | |
41 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename); | |
42 | |
43 % Collect input variable names | |
44 in_names = cell(size(varargin)); | |
45 for ii = 1:nargin,in_names{ii} = inputname(ii);end | |
46 | |
47 % Collect all AOs and plists | |
48 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names); | |
49 [pl, pl_invars] = utils.helper.collect_objects(varargin(:), 'plist', in_names); | |
50 [fbobjs, f_invars] = utils.helper.collect_objects(varargin(:), 'filterbank', in_names); | |
51 [fobj, f_invars] = utils.helper.collect_objects(varargin(:), 'ltpda_filter', in_names); | |
52 | |
53 % Make copies or handles to inputs | |
54 bs = copy(as, nargout); | |
55 | |
56 % combine plists | |
57 pl = parse(pl, getDefaultPlist()); | |
58 | |
59 | |
60 if isempty(fobj) && isempty(fbobjs) | |
61 fobj = find(pl, 'filter'); | |
62 if isa(fobj, 'filterbank') | |
63 fbobjs = fobj; | |
64 end | |
65 f_invars{1} = class(fobj); | |
66 end | |
67 | |
68 % check inputs | |
69 if ~isa(fobj, 'miir') && ~isa(fobj, 'mfir') && ~isa(fbobjs, 'filterbank') | |
70 error('### the filter input should be either an miir/mfir object or a filterbank object.'); | |
71 end | |
72 | |
73 fobj_out = []; | |
74 fp = []; | |
75 | |
76 % Loop over AOs | |
77 for jj = 1:numel(bs) | |
78 | |
79 % Copy filter so we can change it | |
80 if ~isempty(fobj) | |
81 fp = copy(fobj, 1); | |
82 elseif ~isempty(fbobjs) | |
83 fp = copy(fbobjs, 1); | |
84 end | |
85 % keep the history to suppress the history of the intermediate steps | |
86 inhist = bs(jj).hist; | |
87 | |
88 if isa(bs(jj).data, 'tsdata') | |
89 %------------------------------------------------------------------------ | |
90 %------------------------ Time-series filter ------------------------ | |
91 %------------------------------------------------------------------------ | |
92 | |
93 %%%%%%%%%%%%%%%%%%%%%%%%%%% filter %%%%%%%%%%%%%%%%%%%%%%%%%%% | |
94 if isa(fp,'ltpda_filter') | |
95 % redesign if needed | |
96 fs = bs(jj).data.fs; | |
97 if fs ~= fp.fs | |
98 warning('!!! Filter is designed for a different sample rate of data.'); | |
99 % Adjust/redesign if this is a standard filter | |
100 fp = redesign(fp, fs); | |
101 end | |
102 % apply filter | |
103 if isa(fp, 'miir') | |
104 bs(jj).data.y = filtfilt(fp.a, fp.b, bs(jj).data.y); | |
105 elseif isa(fp, 'mfir'); | |
106 bs(jj).data.y = filtfilt(fp.a, 1, bs(jj).data.y); | |
107 else | |
108 error('### Unknown filter object [%s]', class(fp)); | |
109 end | |
110 % set y-units = yunits.*ounits./iunits | |
111 bs(jj).data.setYunits(bs(jj).data.yunits.*fp.ounits./fp.iunits); | |
112 | |
113 %%%%%%%%%%%%%%%%%%%%%%%%%%% filter bank %%%%%%%%%%%%%%%%%%%%%%%%%%% | |
114 elseif isa(fp,'filterbank') | |
115 % redesign not implemented for filterbank | |
116 %%% | |
117 % utils.math routine to apply filtfilt properly | |
118 bs(jj).data.y = utils.math.filtfilt_filterbank(bs(jj),fp); | |
119 % not setting units yet | |
120 %%% | |
121 end | |
122 | |
123 elseif isa(bs(jj).data, 'fsdata') | |
124 %------------------------------------------------------------------------ | |
125 %---------------------- Frequency-series filter --------------------- | |
126 %------------------------------------------------------------------------ | |
127 | |
128 % apply filter | |
129 fil_resp = resp(fp, plist('f', bs(jj))); | |
130 bs(jj) = bs(jj).*fil_resp.*conj(fil_resp); | |
131 else | |
132 error('### unknown data type.'); | |
133 end | |
134 | |
135 | |
136 % name for this object | |
137 bs(jj).name = sprintf('%s(%s)', fp.name, ao_invars{jj}); | |
138 % add history | |
139 bs(jj).addHistory(getInfo('None'), pl, ao_invars(jj), [bs(jj).hist fp.hist]); | |
140 % clear errors | |
141 bs(jj).clearErrors; | |
142 % Collect the filters for the output | |
143 fobj_out = [fobj_out, fp]; | |
144 end | |
145 | |
146 % Set outputs | |
147 if nargout == 1 | |
148 varargout{1} = bs; | |
149 elseif nargout == 2 | |
150 varargout{1} = bs; | |
151 varargout{2} = fobj_out; | |
152 elseif nargout > 2 | |
153 error('### wrong number of output arguments.'); | |
154 end | |
155 end | |
156 | |
157 %-------------------------------------------------------------------------- | |
158 % Get Info Object | |
159 %-------------------------------------------------------------------------- | |
160 function ii = getInfo(varargin) | |
161 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
162 sets = {}; | |
163 pls = []; | |
164 else | |
165 sets = {'Default'}; | |
166 pls = getDefaultPlist; | |
167 end | |
168 % Build info object | |
169 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: filtfilt.m,v 1.48 2011/05/12 13:20:42 luigi Exp $', sets, pls); | |
170 end | |
171 | |
172 %-------------------------------------------------------------------------- | |
173 % Get Default Plist | |
174 %-------------------------------------------------------------------------- | |
175 function plout = getDefaultPlist() | |
176 persistent pl; | |
177 if exist('pl', 'var')==0 || isempty(pl) | |
178 pl = buildplist(); | |
179 end | |
180 plout = pl; | |
181 end | |
182 | |
183 function pl = buildplist() | |
184 pl = plist({'filter', 'The filter to apply to the data.'}, paramValue.EMPTY_STRING); | |
185 end | |
186 | |
187 |