comparison m-toolbox/classes/@ao/intersect.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 % INTERSECT overloads the intersect operator for Analysis objects.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: INTERSECT overloads the intersect operator for Analysis
5 % objects.
6 %
7 % CALL: out = intersect(a1, a2);
8 %
9 % EXAMPLE: This example shows only the x-values:
10 %
11 % a1: |-----------| |--------------------|
12 % a2: |-------------------------------------|
13 %
14 % The output have the x-values of a1 and the y-values of a2
15 %
16 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'intersect')">Parameters Description</a>
17 %
18 % VERSION: $Id: intersect.m,v 1.5 2011/04/08 08:56:12 hewitson Exp $
19 %
20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21
22 function varargout = intersect (varargin)
23
24 % Check if this is a call for parameters
25 if utils.helper.isinfocall(varargin{:})
26 varargout{1} = getInfo(varargin{3});
27 return
28 end
29
30 % Collect input variable names
31 in_names = cell(size(varargin));
32 for ii = 1:nargin,in_names{ii} = inputname(ii);end
33
34 % Collect all AOs
35 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
36 pl = utils.helper.collect_objects(varargin(:), 'plist');
37
38 % Check input arguments number
39 if length(as) < 2
40 error ('### Incorrect inputs. This method needs at least two input AOs.');
41 end
42
43 if nargout == 0
44 error('### Intersect cannot be used as a modifier. Please give an output variable.');
45 end
46
47 % Decide on a deep copy or a modify
48 bs = copy(as, nargout);
49
50 % Combine input PLIST with default PLIST
51 pl = combine(pl, getDefaultPlist());
52
53 % get options
54 axis = pl.find('axis');
55 tol = pl.find('tol');
56
57 % Get data object
58 out = [];
59 mask = bs(1);
60
61 for ii = 2:numel(bs)
62 a = bs(ii);
63
64 % Choos always the data type of the second input
65 dout = a.data.initObjectWithSize(1,1);
66
67 % Calculate and set values
68 if isa(dout, 'cdata')
69 if any(find(axis == 'y'))
70 [y, idx] = localIntersect(mask.data.getY, a.data.getY, tol);
71 dout.setY(y);
72 setDataValues(dout, a, idx);
73 else
74 x1 = localGetAbsoluteX(mask);
75 x2 = localGetAbsoluteX(a);
76 [y, idx] = localIntersect(x1, x2, tol);
77 dout.setY(y);
78 setDataValues(dout, a, idx);
79 end
80 else
81 if strcmpi(axis, 'y')
82 [y, idx] = localIntersect(mask.data.getY, a.data.getY, tol);
83 x = localGetAbsoluteX(a);
84 dout.setX(x(idx));
85 dout.setY(y);
86 setDataValues(dout, a, idx);
87 elseif strcmpi(axis, 'x')
88 x1 = localGetAbsoluteX(mask);
89 x2 = localGetAbsoluteX(a);
90
91 [x, idx] = localIntersect(x1, x2, tol);
92 dout.setX(x);
93 dout.setY(a.y(idx));
94 setDataValues(dout, a, idx);
95 else
96 error('### This axis is not allowed [%s]. Please use one of the following inputs ''x'' or ''y''');
97 end
98 end
99
100 % collapse dout if possible
101 if isa(dout, 'tsdata')
102 dout.collapseX();
103 end
104
105 % Set name
106 a.name = sprintf('%s(%s, %s)', mfilename, ao_invars{1}, ao_invars{ii});
107 % Set data object
108 a.data = dout;
109 % Set history
110 a.addHistory(getInfo('None'), pl, ao_invars([1 ii]), [mask.hist a.hist]);
111
112 out = [out a];
113 end
114
115 % Set output
116 if nargout == 1
117 varargout{1} = out;
118 end
119 end
120
121 %--------------------------------------------------------------------------
122 % Set all necessary values to the data object
123 %--------------------------------------------------------------------------
124 function setDataValues(dout, a, idx)
125
126 setErrors(dout, a, idx);
127 setUnits(dout, a);
128
129 % Set special values for the fsdata
130 if isa(dout, 'fsdata')
131 dout.setNavs(a.data.navs);
132 dout.setEnbw(a.data.enbw);
133 dout.setT0(a.data.t0);
134 dout.setFs(a.data.fs);
135 end
136 end
137
138 %--------------------------------------------------------------------------
139 % Set the units for the intersection
140 %--------------------------------------------------------------------------
141 function setUnits(dout, a)
142 dout.setYunits(a.yunits);
143 if ~isa(dout, 'cdata')
144 dout.setXunits(a.xunits);
145 end
146 end
147
148 %--------------------------------------------------------------------------
149 % Set the error(s) for the intersection
150 %--------------------------------------------------------------------------
151 function setErrors(dout, a, idx)
152 % Calculate dy
153 if ~isempty(a.dy)
154 if numel(a.dy) == 1
155 dout.setDy(a.dy);
156 else
157 dout.setDy(a.dy(idx));
158 end
159 end
160
161 % Calculate dx
162 if ~isa(dout, 'cdata') && ~isempty(a.dx)
163 if numel(a.dx) == 1
164 dout.setDx(a.dx);
165 else
166 dout.setDx(a.dx(idx));
167 end
168 end
169 end
170
171 %--------------------------------------------------------------------------
172 % Calculates the intersection of a and b with a tolerance
173 %--------------------------------------------------------------------------
174 function x = localGetAbsoluteX(a)
175 if isa(a.data, 'tsdata')
176 x = a.data.getX + a.data.t0.utc_epoch_milli/1e3;
177 else
178 x = a.data.getX;
179 end
180 end
181
182 %--------------------------------------------------------------------------
183 % Calculates the intersection of a and b with a tolerance
184 %--------------------------------------------------------------------------
185 function [out, idxB] = localIntersect(a,b,tol)
186 if isempty(tol)
187 [out, idxA, idxB] = intersect(a, b);
188 else
189 error('### Please code me up.');
190 end
191 end
192
193 %--------------------------------------------------------------------------
194 % Get Info Object
195 %--------------------------------------------------------------------------
196 function ii = getInfo(varargin)
197 if nargin == 1 && strcmpi(varargin{1}, 'None')
198 sets = {};
199 pl = [];
200 else
201 sets = {'Default'};
202 pl = getDefaultPlist;
203 end
204 % Build info object
205 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.trig, '$Id: intersect.m,v 1.5 2011/04/08 08:56:12 hewitson Exp $', sets, pl);
206 ii.setModifier(false);
207 ii.setArgsmin(2);
208 end
209
210 %--------------------------------------------------------------------------
211 % Get Default Plist
212 %--------------------------------------------------------------------------
213 function plout = getDefaultPlist()
214 persistent pl;
215 if exist('pl', 'var')==0 || isempty(pl)
216 pl = buildplist();
217 end
218 plout = pl;
219 end
220
221 function out = buildplist()
222 out = plist();
223
224 % AXIS
225 p = param({'axis', 'The axis on which to apply the method.'}, {1, {'x', 'y'}, paramValue.SINGLE});
226 out.append(p);
227
228 % TOL
229 p = param({'tol', 'Tolerance for the intersect method.'}, paramValue.EMPTY_DOUBLE);
230 out.append(p);
231
232 end
233