Mercurial > hg > ltpda
comparison testing/utp_1.1/utps/ao/utp_ao_diff.m @ 44:409a22968d5e default
Add unit tests
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Tue, 06 Dec 2011 18:42:11 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
43:bc767aaa99a8 | 44:409a22968d5e |
---|---|
1 % UTP_AO_DIFF a set of UTPs for the ao/diff method | |
2 % | |
3 % M Hewitson 06-08-08 | |
4 % | |
5 % $Id: utp_ao_diff.m,v 1.14 2009/09/20 16:51:33 hewitson Exp $ | |
6 % | |
7 | |
8 % <MethodDescription> | |
9 % | |
10 % The diff method of the ao class computes the derivative of the input data | |
11 % using different methods. | |
12 % | |
13 % </MethodDescription> | |
14 | |
15 function results = utp_ao_diff(varargin) | |
16 | |
17 % Check the inputs | |
18 if nargin == 0 | |
19 | |
20 % Some keywords | |
21 class = 'ao'; | |
22 mthd = 'diff'; | |
23 | |
24 results = []; | |
25 disp('******************************************************'); | |
26 disp(['**** Running UTPs for ' class '/' mthd]); | |
27 disp('******************************************************'); | |
28 | |
29 % Test AOs | |
30 [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]); | |
31 | |
32 % Exception list for the UTPs: | |
33 [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples(); | |
34 | |
35 % Run the tests | |
36 atvec = [at1 at5 at6]; | |
37 atmat = [at1 at5 at6; at5 at6 at1]; | |
38 pli = plist('method', '3point', 'neval', true); | |
39 | |
40 results = [results utp_01]; % getInfo call | |
41 results = [results utp_02(mthd, atvec, @algo_test_y, pli, ple3)]; % Vector input | |
42 results = [results utp_03(mthd, atmat, @algo_test_y, pli, ple3)]; % Matrix input | |
43 results = [results utp_04(mthd, at1, at5, at6, @algo_test_y, pli, ple3)]; % List input | |
44 results = [results utp_05(mthd, at1, atvec, atmat, @algo_test_y, pli, ple3)]; % Test with mixed input | |
45 results = [results utp_06(mthd, at1, pli, ple2)]; % Test history is working | |
46 results = [results utp_07(mthd, at1, pli, ple2)]; % Test the modify call works | |
47 results = [results utp_09(mthd, at5, at6)]; % Test input data shape == output data shape | |
48 results = [results utp_10(mthd, at1, at5, ple2)]; % Test output of the data | |
49 results = [results utp_11(mthd, at1, ple1)]; % Test plotinfo doesn't disappear | |
50 results = [results utp_12(mthd, at1, ple1)]; % Test errors are cleared | |
51 | |
52 results = [results utp_13]; % Test with plist: method = 'ORDER2' | |
53 results = [results utp_14]; % Test with plist: method = 'ORDER2SMOOTH' | |
54 results = [results utp_15]; % Test with plist: method = '5POINT' | |
55 | |
56 | |
57 disp('Done.'); | |
58 disp('******************************************************'); | |
59 | |
60 elseif nargin == 1 % Check for UTP functions | |
61 if strcmp(varargin{1}, 'isutp') | |
62 results = 1; | |
63 else | |
64 results = 0; | |
65 end | |
66 else | |
67 error('### Incorrect inputs') | |
68 end | |
69 | |
70 %% Algorithm test for UTP 02,03,04,05 | |
71 | |
72 function atest = algo_test_y(in, out, pli) | |
73 atest = true; | |
74 % 3 point derivative | |
75 x = in.data.getX; | |
76 dx = diff(x); | |
77 y = in.data.getY; | |
78 z = zeros(size(y)); | |
79 z(2:end-1) = (y(3:end)-y(1:end-2)) ./ (dx(2:end)+dx(1:end-1)); | |
80 z(1) = (y(2)-y(1)) ./ (dx(1)); | |
81 z(end) = 2*z(end-1)-z(end-2); | |
82 if ~isequal(out.y, z), atest = false; end | |
83 end | |
84 | |
85 %% UTP_01 | |
86 | |
87 % <TestDescription> | |
88 % | |
89 % Tests that the getInfo call works for this method. | |
90 % | |
91 % </TestDescription> | |
92 function result = utp_01 | |
93 | |
94 | |
95 % <SyntaxDescription> | |
96 % | |
97 % Test that the getInfo call works for no sets, all sets, and each set | |
98 % individually. | |
99 % | |
100 % </SyntaxDescription> | |
101 | |
102 try | |
103 % <SyntaxCode> | |
104 % Call for no sets | |
105 io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']); | |
106 % Call for all sets | |
107 io(2) = eval([class '.getInfo(''' mthd ''')']); | |
108 % Call for each set | |
109 for kk=1:numel(io(2).sets) | |
110 io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']); | |
111 end | |
112 % </SyntaxCode> | |
113 stest = true; | |
114 catch err | |
115 disp(err.message) | |
116 stest = false; | |
117 end | |
118 | |
119 % <AlgoDescription> | |
120 % | |
121 % 1) Check that getInfo call returned an minfo object in all cases. | |
122 % 2) Check that all plists have the correct parameters. | |
123 % | |
124 % </AlgoDescription> | |
125 | |
126 atest = true; | |
127 if stest | |
128 % <AlgoCode> | |
129 % check we have minfo objects | |
130 if isa(io, 'minfo') | |
131 %%% SET 'None' | |
132 if ~isempty(io(1).sets), atest = false; end | |
133 if ~isempty(io(1).plists), atest = false; end | |
134 %%% Check all Sets | |
135 if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end | |
136 if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end | |
137 %%%%%%%%%% SET 'Default' | |
138 if io(3).plists.nparams ~= 4, atest = false; end | |
139 % Check key | |
140 if ~io(3).plists.isparam('method'), atest = false; end | |
141 if ~io(3).plists.isparam('f0'), atest = false; end | |
142 if ~io(3).plists.isparam('order'), atest = false; end | |
143 if ~io(3).plists.isparam('coeff'), atest = false; end | |
144 % Check default value | |
145 if ~isequal(io(3).plists.find('method'), '2POINT'), atest = false; end | |
146 if ~isequal(io(3).plists.find('f0'), '1/Nsecs'), atest = false; end | |
147 if ~isequal(io(3).plists.find('order'), 'ZERO'), atest = false; end | |
148 if ~isEmptyDouble(io(3).plists.find('coeff')), atest = false; end | |
149 % Check options | |
150 if ~isequal(io(3).plists.getOptionsForParam('method'), {'2POINT', '3POINT', '5POINT', 'ORDER2', 'ORDER2SMOOTH', 'FILTER', 'FPS'}), atest = false; end | |
151 if ~isequal(io(3).plists.getOptionsForParam('f0'), {'1/Nsecs'}), atest = false; end | |
152 if ~isequal(io(3).plists.getOptionsForParam('order'), {'ZERO', 'FIRST', 'SECOND'}), atest = false; end | |
153 if ~isequal(io(3).plists.getOptionsForParam('coeff'), {[]}), atest = false; end | |
154 end | |
155 % </AlgoCode> | |
156 else | |
157 atest = false; | |
158 end | |
159 | |
160 % Return a result structure | |
161 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
162 end % END UTP_01 | |
163 | |
164 %% UTP_13 | |
165 | |
166 % <TestDescription> | |
167 % | |
168 % Control the method with a plist. | |
169 % | |
170 % </TestDescription> | |
171 function result = utp_13 | |
172 | |
173 % <SyntaxDescription> | |
174 % | |
175 % Test the computation of derivative using a 2nd order | |
176 % | |
177 % </SyntaxDescription> | |
178 | |
179 try | |
180 % <SyntaxCode> | |
181 pl = plist('method', 'ORDER2'); | |
182 out = diff(at5, pl); | |
183 mout = rebuild(out); | |
184 % </SyntaxCode> | |
185 stest = true; | |
186 catch err | |
187 disp(err.message) | |
188 stest = false; | |
189 end | |
190 | |
191 % <AlgoDescription> | |
192 % | |
193 % 1) Check that the diff method uses the 2nd order derivative. | |
194 % 2) Check that the re-built object is the same object as 'out'. | |
195 % | |
196 % </AlgoDescription> | |
197 | |
198 atest = true; | |
199 if stest | |
200 % <AlgoCode> | |
201 % Compute derivative using a 2nd order | |
202 x = at5.data.getX; | |
203 dx = diff(x); | |
204 y = at5.data.getY; | |
205 z = zeros(size(y)); | |
206 m = length(y); | |
207 % y'(x1) | |
208 z(1) = (1/dx(1)+1/dx(2))*(y(2)-y(1))+... | |
209 dx(1)/(dx(1)*dx(2)+dx(2)^2)*(y(1)-y(3)); | |
210 % y'(xm) | |
211 z(m) = (1/dx(m-2)+1/dx(m-1))*(y(m)-y(m-1))+... | |
212 dx(m-1)/(dx(m-1)*dx(m-2)+dx(m-2)^2)*(y(m-2)-y(m)); | |
213 % y'(xi) (i>1 & i<m) | |
214 dx1 = repmat(dx(1:m-2),1,1); | |
215 dx2 = repmat(dx(2:m-1),1,1); | |
216 y1 = y(1:m-2); y2 = y(2:m-1); y3 = y(3:m); | |
217 z(2:m-1) = 1./(dx1.*dx2.*(dx1+dx2)).*... | |
218 (-dx2.^2.*y1+(dx2.^2-dx1.^2).*y2+dx1.^2.*y3); | |
219 % Check the 2nd oder derivative | |
220 if ~isequal(out.y, z), atest = false; end | |
221 % Check the re-built object | |
222 if ~eq(mout, out, ple2), atest = false; end | |
223 % </AlgoCode> | |
224 else | |
225 atest = false; | |
226 end | |
227 | |
228 % Return a result structure | |
229 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
230 end % END UTP_13 | |
231 | |
232 %% UTP_14 | |
233 | |
234 % <TestDescription> | |
235 % | |
236 % Control the method with a plist. | |
237 % | |
238 % </TestDescription> | |
239 function result = utp_14 | |
240 | |
241 % <SyntaxDescription> | |
242 % | |
243 % Test the computation of derivative using a 2nd order with a parabolic fit | |
244 % | |
245 % </SyntaxDescription> | |
246 | |
247 try | |
248 % <SyntaxCode> | |
249 pl = plist('method', 'ORDER2SMOOTH'); | |
250 out = diff(at5, pl); | |
251 mout = rebuild(out); | |
252 % </SyntaxCode> | |
253 stest = true; | |
254 catch err | |
255 disp(err.message) | |
256 stest = false; | |
257 end | |
258 | |
259 % <AlgoDescription> | |
260 % | |
261 % 1) Check that the diff method uses the 2nd order derivative with a | |
262 % parabolic fit | |
263 % 2) Check that the re-built object is the same object as 'out'. | |
264 % | |
265 % </AlgoDescription> | |
266 | |
267 atest = true; | |
268 if stest | |
269 % <AlgoCode> | |
270 % Compute derivative using a 2nd order with a parabolic fit | |
271 x = at5.data.getX; | |
272 y = at5.data.getY; | |
273 dx = diff(x); | |
274 m = length(y); | |
275 h = mean(dx); | |
276 z = zeros(size(y)); | |
277 % y'(x1) | |
278 z(1) = sum(y(1:5).*[-54; 13; 40; 27; -26])/70/h; | |
279 % y'(x2) | |
280 z(2) = sum(y(1:5).*[-34; 3; 20; 17; -6])/70/h; | |
281 % y'(x{m-1}) | |
282 z(m-1) = sum(y(end-4:end).*[6; -17; -20; -3; 34])/70/h; | |
283 % y'(xm) | |
284 z(m) = sum(y(end-4:end).*[26; -27; -40; -13; 54])/70/h; | |
285 % y'(xi) (i>2 & i<(N-1)) | |
286 Dc = [2 1 0 -1 -2]; | |
287 tmp = convn(Dc,y)/10/h; | |
288 z(3:m-2) = tmp(5:m); | |
289 % Check the 2nd oder derivative | |
290 if ~isequal(out.y, z), atest = false; end | |
291 % Check the re-built object | |
292 if ~eq(mout, out, ple2), atest = false; end | |
293 % </AlgoCode> | |
294 else | |
295 atest = false; | |
296 end | |
297 | |
298 % Return a result structure | |
299 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
300 end % END UTP_14 | |
301 | |
302 %% UTP_15 | |
303 | |
304 % <TestDescription> | |
305 % | |
306 % Control the method with a plist. | |
307 % | |
308 % </TestDescription> | |
309 function result = utp_15 | |
310 | |
311 % <SyntaxDescription> | |
312 % | |
313 % Test the 5 point derivative. | |
314 % | |
315 % </SyntaxDescription> | |
316 | |
317 try | |
318 % <SyntaxCode> | |
319 pl = plist('method', '5POINT'); | |
320 out = diff(at5, pl); | |
321 mout = rebuild(out); | |
322 % </SyntaxCode> | |
323 stest = true; | |
324 catch err | |
325 disp(err.message) | |
326 stest = false; | |
327 end | |
328 | |
329 % <AlgoDescription> | |
330 % | |
331 % 1) Check that the diff method uses the 5 point derivative. | |
332 % 2) Check that the re-built object is the same object as 'out'. | |
333 % | |
334 % </AlgoDescription> | |
335 | |
336 atest = true; | |
337 if stest | |
338 % <AlgoCode> | |
339 % Compute 5 point derivative | |
340 x = at5.data.getX; | |
341 dx = diff(x); | |
342 y = at5.data.getY; | |
343 z = zeros(size(y)); | |
344 z(1) = (y(2)-y(1)) ./ (dx(1)); | |
345 z(2) = (y(3)-y(1))./(dx(2)+dx(1)); | |
346 z(3:end-2) = (-y(5:end) + 8.*y(4:end-1) - 8.*y(2:end-3) + y(1:end-4)) ./ (3.*(x(5:end)-x(1:end-4))); | |
347 z(end-1) = 2*z(end-2)-z(end-3); | |
348 z(end) = 2*z(end-1)-z(end-2); | |
349 % Check the 5 point derivative | |
350 if ~isequal(out.y, z), atest = false; end | |
351 % Check the re-built object | |
352 if ~eq(mout, out, ple2), atest = false; end | |
353 % </AlgoCode> | |
354 else | |
355 atest = false; | |
356 end | |
357 | |
358 % Return a result structure | |
359 result = utp_prepare_result(atest, stest, dbstack, mfilename); | |
360 end % END UTP_15 | |
361 | |
362 end |