comparison testing/utp_1.1/utps/ao/utp_ao_fft.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_FFT a set of UTPs for the ao/fft method
2 %
3 % M Hewitson 06-08-08
4 %
5 % $Id: utp_ao_fft.m,v 1.13 2011/02/04 11:08:54 luigi Exp $
6 %
7
8 % <MethodDescription>
9 %
10 % The fft method of the ao class computes the fast fourier transform of
11 % time-series AOs.
12 %
13 % </MethodDescription>
14
15 function results = utp_ao_fft(varargin)
16
17 % Check the inputs
18 if nargin == 0
19
20 % Some keywords
21 class = 'ao';
22 mthd = 'fft';
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
39 results = [results utp_01]; % getInfo call
40
41 results = [results utp_02(mthd, atvec, @algo_test_y, [], ple3)]; % Vector input
42 results = [results utp_03(mthd, atmat, @algo_test_y, [], ple3)]; % Matrix input
43 results = [results utp_04(mthd, at1, at5, at6, @algo_test_y, [], ple3)]; % List input
44 results = [results utp_05(mthd, at1, atvec, atmat, @algo_test_y, [], ple3)]; % Test with mixed input
45 results = [results utp_06(mthd, at1, [], ple2)]; % Test history is working
46 results = [results utp_07(mthd, at1, plist('neval', true), 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
51 results = [results utp_12]; % Test against MATLAB's fft
52 results = [results utp_13]; % Test with different data types
53 results = [results utp_14]; % Test with plist: 'type' = 'two'
54
55 disp('Done.');
56 disp('******************************************************');
57
58 elseif nargin == 1 % Check for UTP functions
59 if strcmp(varargin{1}, 'isutp')
60 results = 1;
61 else
62 results = 0;
63 end
64 else
65 error('### Incorrect inputs')
66 end
67
68 %% Algorithm test for UTP 02,03,04,05
69
70 function atest = algo_test_y(in, out, pli)
71 atest = true;
72 fs = in.data.fs;
73 fmin = 0;
74 nfft = length(in.y);
75 ft = fft(in.y);
76 ft = ft(1:nfft/2+1);
77 f = linspace(fmin, fs/2, length(ft)).';
78 % Check y-axis
79 if ~isequal(out.y, ft), atest = false; end
80 % Check x-axis
81 if ~isequal(out.x, f), atest = false; end
82 % Check fs
83 if ~isequal(out.fs, fs), atest = false; end
84 % Check units
85 if ~eq(out.xunits, unit('Hz')), atest = false; end
86 if ~eq(out.yunits, in.yunits), atest = false; end
87 end
88
89 %% UTP_01
90
91 % <TestDescription>
92 %
93 % Tests that the getInfo call works for this method.
94 %
95 % </TestDescription>
96 function result = utp_01
97
98
99 % <SyntaxDescription>
100 %
101 % Test that the getInfo call works for no sets, all sets, and each set
102 % individually.
103 %
104 % </SyntaxDescription>
105
106 try
107 % <SyntaxCode>
108 % Call for no sets
109 io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']);
110 % Call for all sets
111 io(2) = eval([class '.getInfo(''' mthd ''')']);
112 % Call for each set
113 for kk=1:numel(io(2).sets)
114 io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']);
115 end
116 % </SyntaxCode>
117 stest = true;
118 catch err
119 disp(err.message)
120 stest = false;
121 end
122
123 % <AlgoDescription>
124 %
125 % 1) Check that getInfo call returned an minfo object in all cases.
126 % 2) Check that all plists have the correct parameters.
127 %
128 % </AlgoDescription>
129
130 atest = true;
131 if stest
132 % <AlgoCode>
133 % check we have minfo objects
134 if isa(io, 'minfo')
135 %%% SET 'None'
136 if ~isempty(io(1).sets), atest = false; end
137 if ~isempty(io(1).plists), atest = false; end
138 %%% Check all Sets
139 if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
140 if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
141 %%%%%%%%%% SET 'Default'
142 if io(3).plists.nparams ~= 2, atest = false; end
143 % Check key
144 if ~io(3).plists.isparam('type'), atest = false; end
145 % Check default value
146 if ~isequal(io(3).plists.find('type'), 'one'), atest = false; end
147 % Check options
148 if ~isequal(io(3).plists.getOptionsForParam('type'), {'plain', 'one', 'two'}), atest = false; end
149 % Check key
150 if ~io(3).plists.isparam('scale'), atest = false; end
151 % Check default value
152 if ~isequal(io(3).plists.find('scale'), false), atest = false; end
153 % Check options
154 if ~isequal(io(3).plists.getOptionsForParam('scale'), paramValue.FALSE_TRUE{2}), atest = false; end
155 end
156 % </AlgoCode>
157 else
158 atest = false;
159 end
160
161 % Return a result structure
162 result = utp_prepare_result(atest, stest, dbstack, mfilename);
163 end % END UTP_01
164
165 %% UTP_12
166
167 % <TestDescription>
168 %
169 % Tests that the fft method agrees with MATLAB's fft when
170 % configured to use the same parameters.
171 %
172 % </TestDescription>
173 function result = utp_12
174
175 % <SyntaxDescription>
176 %
177 % Test that the applying fft works on a single AO.
178 %
179 % </SyntaxDescription>
180
181 try
182 % <SyntaxCode>
183 % Construct two test AOs
184 nsecs = 10;
185 fs = 1000;
186 pl = plist('nsecs', nsecs, 'fs', fs, 'tsfcn', 'randn(size(t))');
187 a1 = ao(pl);
188 % Compute fft
189 out = fft(a1);
190 % </SyntaxCode>
191 stest = true;
192 catch err
193 disp(err.message)
194 stest = false;
195 end
196
197 % <AlgoDescription>
198 %
199 % 1) Check that output agrees with the output of MATLAB's fft.
200 %
201 % </AlgoDescription>
202
203 atest = true;
204 if stest
205 % <AlgoCode>
206 % Compute fft using MATLAB's fft
207 % ao/fft returns only one-sided fft (0-Nyquist)
208 yxx = fft(a1.data.y);
209 if ~isequal(yxx(1:length(yxx)/2+1), out.data.y), atest = false; end
210 % </AlgoCode>
211 else
212 atest = false;
213 end
214
215 % Return a result structure
216 result = utp_prepare_result(atest, stest, dbstack, mfilename);
217 end % END UTP_12
218
219 %% UTP_13
220
221 % <TestDescription>
222 %
223 % Tests that the fft method works with different data types. The testing of
224 % tsdata types are done before.
225 %
226 % </TestDescription>
227 function result = utp_13
228
229 % <SyntaxDescription>
230 %
231 % Test that the applying fft works on cdata and xydata.
232 %
233 % </SyntaxDescription>
234
235 try
236 % <SyntaxCode>
237 % Compute fft
238 out3 = fft(at3);
239 out4 = fft(at4);
240 % </SyntaxCode>
241 stest = true;
242 catch err
243 disp(err.message)
244 stest = false;
245 end
246
247 % <AlgoDescription>
248 %
249 % 1) Check that each output AO contains the correct data.
250 %
251 % </AlgoDescription>
252
253 atest = true;
254 if stest
255 % <AlgoCode>
256 % Compute fft for cdata and xydata
257 fs = 2+length(at3.y);
258 nfft = length(at3.y);
259 ft = fft(at3.y);
260 ft = ft(1:nfft/2+1);
261 f = (0:floor(nfft/2)).*fs./nfft;
262 % Check y-axis
263 if ~isequal(out3.y, ft), atest = false; end
264 % Check x-axis
265 if ~isequal(out3.x, f.'), atest = false; end
266 % Check fs
267 if ~isequal(out3.fs, fs), atest = false; end
268 % Check units
269 if ~eq(out3.xunits, unit('Hz')), atest = false; end
270 if ~eq(out3.yunits, at3.yunits), atest = false; end
271 % </AlgoCode>
272 else
273 atest = false;
274 end
275
276 % Return a result structure
277 result = utp_prepare_result(atest, stest, dbstack, mfilename);
278 end % END UTP_13
279
280 %% UTP_14
281
282 % <TestDescription>
283 %
284 % Tests that the fft method works with a plist which constains the key/value
285 % pair 'type'/'two'.
286 %
287 % </TestDescription>
288 function result = utp_14
289
290 % <SyntaxDescription>
291 %
292 % Test that the applying fft works with a plist.
293 %
294 % </SyntaxDescription>
295
296 try
297 % <SyntaxCode>
298 pl = plist('type', 'two');
299 % Compute fft
300 out = fft(at5, pl);
301 mout = rebuild(out);
302 % </SyntaxCode>
303 stest = true;
304 catch err
305 disp(err.message)
306 stest = false;
307 end
308
309 % <AlgoDescription>
310 %
311 % 1) Check that each output AO contains the correct data.
312 % 2) Check that the re-built object is the same object as 'out'.
313 %
314 % </AlgoDescription>
315
316 atest = true;
317 if stest
318 % <AlgoCode>
319 % Compute fft for cdata and xydata
320 fs = at5.data.fs;
321 nfft = length(at5.y);
322 ft = fft(at5.data.y);
323 ft = fftshift(ft);
324 f = fftshift([0:floor(nfft/2)-1 -floor(nfft/2):-1].*fs./nfft);
325 % Check y-axis
326 if ~isequal(out.y, ft), atest = false; end
327 % Check x-axis
328 if ~isequal(out.x, f.'), atest = false; end
329 % Check fs
330 if ~isequal(out.fs, fs), atest = false; end
331 % Check units
332 if ~eq(out.xunits, unit('Hz')), atest = false; end
333 if ~eq(out.yunits, at5.yunits), atest = false; end
334 % Check the re-built object
335 if ~eq(mout, out, ple2), atest = false; end
336 % </AlgoCode>
337 else
338 atest = false;
339 end
340
341 % Return a result structure
342 result = utp_prepare_result(atest, stest, dbstack, mfilename);
343 end % END UTP_14
344
345 end