comparison m-toolbox/classes/@ao/removeVal.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 % REMOVEVAL removes values from the input AO(s).
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: REMOVEVAL removes user specified value(s) from the input AO(s),
5 % such as NaNs, Infs, or 0s. Depending on the values set in the plist,
6 % it will replace them with nothing or with an interpolated value
7 %
8 % CALL: b = removeVal(a, pl)
9 %
10 % INPUTS: a - input array of AOs
11 % pl - parameter list with the keys 'axis' and 'method'
12 %
13 % OUTPUTS: b - output array of AOs
14 %
15 % REMARKs:
16 %
17 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'removeVal')">Parameters Description</a>
18 %
19 % VERSION: $Id: removeVal.m,v 1.10 2011/05/27 10:30:43 mauro Exp $
20 %
21 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22
23 function varargout = removeVal(varargin)
24
25 % Check if this is a call for parameters
26 if utils.helper.isinfocall(varargin{:})
27 varargout{1} = getInfo(varargin{3});
28 return
29 end
30
31 import utils.const.*
32 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
33
34 % Collect input variable names
35 in_names = cell(size(varargin));
36 for ii = 1:nargin,in_names{ii} = inputname(ii);end
37
38 % Collect all AOs and plists
39 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
40 pl = utils.helper.collect_objects(varargin(:), 'plist', in_names);
41
42 % Decide on a deep copy or a modify
43 bs = copy(as, nargout);
44
45 % Combine plists
46 pl = parse(pl, getDefaultPlist);
47
48 % Get parameters
49 ax = find(pl, 'axis');
50 method = find(pl, 'method');
51 value = find(pl, 'value');
52 interp_method = find(pl, 'interpolation');
53
54 %-----------------------
55 % Loop over input AOs
56 for jj = 1:numel(bs)
57 % record input history
58 hin = bs(jj).hist;
59
60 switch ax
61 case 'y'
62 % Find the index position of the elements to remove
63 validY = true(size(bs(jj).y));
64 for kk = 1:numel(value)
65 if isnumeric(value(kk))
66 if isfinite(value(kk))
67 validY = validY & bs(jj).y ~= value(kk);
68 else
69 if isnan(value(kk))
70 validY = validY & ~isnan(bs(jj).y);
71 end
72 if isinf(value(kk))
73 validY = validY & ~isinf(bs(jj).y);
74 end
75 end
76 end
77 end
78
79 % Go ahead and act on the data
80 switch method
81 case 'remove'
82 if ~isa(bs(jj).data, 'cdata')
83 % for tsdata, fsdata and xydata objects
84 % Set X,Y
85 x = bs(jj).x;
86 bs(jj).setXY(...
87 x(validY), bs(jj).data.y(validY));
88 % Set DY
89 if ~isempty(bs(jj).dy) && numel(bs(jj).dy) > 1
90 bs(jj).setDy(bs(jj).data.dy(validY));
91 end
92 clear x;
93 else
94 % for cdata objects
95 % Set Y
96 bs(jj).setY(...
97 bs(jj).data.y(validY));
98 % Set DY
99 if ~isempty(bs(jj).dy) && numel(bs(jj).dy) > 1
100 bs(jj).setDy(bs(jj).data.dy(validY));
101 end
102 end
103
104 % Set ENBW
105 if isprop(bs(jj).data, 'enbw')
106 bs(jj).data.setEnbw(bs(jj).enbw(validY));
107 end
108
109 % Set FS
110 if isprop(bs(jj).data, 'fs') && isa(bs(jj).data, 'tsdata')
111 bs(jj).data.setFs(bs(jj).fs); % TODO: we should recalculate it ...
112 end
113
114 case 'interp'
115 if ~isa(bs(jj).data, 'cdata')
116 % for tsdata, fsdata and xydata objects
117 % Set Y
118 x = bs(jj).x;
119 y = bs(jj).y;
120
121 % We need at least two valid points to interpolate on
122 if sum(validY) >= 2
123 % Set Y
124 bs(jj).setY(...
125 interp1(x(validY), y(validY), x, interp_method, 'extrap'));
126 if ~isempty(bs(jj).dy) && numel(bs(jj).dy) > 1
127 % Set DY
128 bs(jj).setDy(interp1(x(validY), bs(jj).data.dy(validY), x, interp_method, 'extrap'));
129 end
130 if isprop(bs(jj).data, 'enbw')
131 % Set ENBW
132 if ~isempty(bs(jj).data.enbw) && numel(bs(jj).data.enbw) > 1
133 bs(jj).data.setEnbw(interp1(x(validY), bs(jj).enbw(validY), x, interp_method, 'extrap'));
134 end
135 end
136 else
137 % If we have 0 or 1 valid points, just set them
138 % Set Y
139 bs(jj).setXY(...
140 x(validY), bs(jj).data.y(validY));
141 % Set DY
142 if ~isempty(bs(jj).dy) && numel(bs(jj).dy) > 1
143 bs(jj).setDy(bs(jj).data.dy(validY));
144 end
145 % Set ENBW
146 if isprop(bs(jj).data, 'enbw')
147 bs(jj).data.setEnbw(bs(jj).enbw(validY));
148 end
149 % Set FS
150 if isprop(bs(jj).data, 'fs') && isa(bs(jj).data, 'tsdata')
151 bs(jj).data.setFs(bs(jj).fs); % TODO: we should recalculate it ...
152 end
153 end
154 clear x y;
155 else
156 % for cdata object
157 % We need at least two valid points to interpolate on
158 if sum(validY) >= 2
159 % Set Y
160 bs(jj).setY(interp1(bs(jj).data.y(validY), [1:lenght(bs(jj).y)], method, 'extrap'));
161 if ~isempty(bs(jj).dy) && numel(bs(jj).dy) > 1
162 % Set DY
163 bs(jj).setDy(interp1(bs(jj).x(validY), bs(jj).data.dy(validY), [1:lenght(bs(jj).y)], interp_method, 'extrap'));
164 end
165 else
166 % If we have 0 or 1 valid points, just set them
167 % Set Y
168 bs(jj).setY(...
169 bs(jj).data.y(validY));
170 % Set DY
171 if ~isempty(bs(jj).dy) && numel(bs(jj).dy) > 1
172 bs(jj).setDy(bs(jj).data.dy(validY));
173 end
174 end
175 end
176
177 otherwise
178 error('### Unrecognised method %s', method);
179 end
180 case {'x', 'xy'}
181 utils.helper.msg(msg.IMPORTANT, 'Option %s not avalable yet, sorry.', ax);
182 otherwise
183 utils.helper.msg(msg.IMPORTANT, 'Option %s not recognised, sorry.', ax);
184 end
185
186 % set name
187 bs(jj).name = sprintf('%s(%s)', mfilename, ao_invars{jj});
188 % Add history
189 bs(jj).addHistory(getInfo('None'), pl, ao_invars, hin);
190 end
191
192 % Set output
193 varargout = utils.helper.setoutputs(nargout, bs);
194 end
195
196 %--------------------------------------------------------------------------
197 % Get Info Object
198 %--------------------------------------------------------------------------
199 function ii = getInfo(varargin)
200 if nargin == 1 && strcmpi(varargin{1}, 'None')
201 sets = {};
202 pl = [];
203 else
204 sets = {'Default'};
205 pl = getDefaultPlist();
206 end
207 % Build info object
208 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: removeVal.m,v 1.10 2011/05/27 10:30:43 mauro Exp $', sets, pl);
209 end
210
211 %--------------------------------------------------------------------------
212 % Get Default Plist
213 %--------------------------------------------------------------------------
214 function plout = getDefaultPlist()
215 persistent pl;
216 if ~exist('pl', 'var') || isempty(pl)
217 pl = buildplist();
218 end
219 plout = pl;
220 end
221
222 function pl = buildplist()
223
224 pl = plist();
225
226 % Value
227 p = param({'value', ['The value(s) to remove. Multiple values can be input in a vector.<br>' ...
228 'Accepted values are:<ul>' ...
229 '<li>NaN</li>' ...
230 '<li>Inf</li>' ...
231 '<li>Numbers</li></ul>']}, paramValue.EMPTY_DOUBLE);
232 pl.append(p);
233
234 % Vertices
235 p = param({'axis', 'The axis to check on.'}, ...
236 {2, {'x', 'y'}, paramValue.SINGLE});
237 pl.append(p);
238
239 % Method
240 p = param({'method', 'The operation to perform on the values curresponding to NaN.'}, ...
241 {1, {'remove', 'interp'}, paramValue.SINGLE});
242 pl.append(p);
243
244 % Interpolation
245 pli = ao.getInfo('interp').plists;
246 p = setKey(pli.params(pli.getIndexForKey('method')), 'interpolation');
247 pl.append(p);
248 end