Mercurial > hg > ltpda
comparison m-toolbox/test/test_ao_rotate.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 function test_ao_rotate() | |
2 % test the rotate method | |
3 | |
4 % assume that the math is done right and test only the method interface | |
5 | |
6 % construct input AOs | |
7 v1 = ao(1); | |
8 v2 = ao(1); | |
9 | |
10 % two AOs - no rotation | |
11 v = rotate(v1, v2); | |
12 v1r = index(v, 1); | |
13 v2r = index(v, 2); | |
14 assert(abs(v1r.y - 1) < 1e-15); | |
15 assert(abs(v2r.y - 1) < 1e-15); | |
16 | |
17 % two AOs and a scalar | |
18 v = rotate(v1, v2, pi); | |
19 v1r = index(v, 1); | |
20 v2r = index(v, 2); | |
21 assert(abs(v1r.y - -1) < 1e-15); | |
22 assert(abs(v2r.y - -1) < 1e-15); | |
23 | |
24 % three AOs | |
25 v = rotate(v1, v2, ao(pi)); | |
26 v1r = index(v, 1); | |
27 v2r = index(v, 2); | |
28 assert(abs(v1r.y - -1) < 1e-15); | |
29 assert(abs(v2r.y - -1) < 1e-15); | |
30 | |
31 % two AOs and a plist | |
32 v = rotate(v1, v2, plist('ang', pi)); | |
33 v1r = index(v, 1); | |
34 v2r = index(v, 2); | |
35 assert(abs(v1r.y - -1) < 1e-15); | |
36 assert(abs(v2r.y - -1) < 1e-15); | |
37 | |
38 % two AOs and a plist with an AO | |
39 v = rotate(v1, v2, plist('ang', ao(pi))); | |
40 v1r = index(v, 1); | |
41 v2r = index(v, 2); | |
42 assert(abs(v1r.y - -1) < 1e-15); | |
43 assert(abs(v2r.y - -1) < 1e-15); | |
44 | |
45 % check that original vectors didn't change | |
46 assert(v1.y == 1); | |
47 assert(v2.y == 1); | |
48 | |
49 % invalid call as modifier | |
50 try | |
51 rotate(v1, v2, plist('ang', ao(pi))); | |
52 assert(false, 'this call should fail'); | |
53 catch e | |
54 assert(strncmp(e.message, '### ao/rotate can not be used as a modifier method. Please give at least one output', ... | |
55 length('### ao/rotate can not be used as a modifier method. Please give at least one output'))); | |
56 end | |
57 | |
58 % invalid call with one AO | |
59 try | |
60 v = rotate(v1, plist('ang', ao(pi))); %#ok<NASGU,ASGLU> | |
61 assert(false, 'this call should fail'); | |
62 catch e | |
63 assert(strncmp(e.message, '### wrong number of input AOs', length('### wrong number of input AOs'))); | |
64 end | |
65 | |
66 % invalid call with four AOs | |
67 try | |
68 v = rotate(v1, v2, v1, v2); %#ok<NASGU,ASGLU> | |
69 assert(false, 'this call should fail'); | |
70 catch e | |
71 assert(strncmp(e.message, '### wrong number of input AOs', length('### wrong number of input AOs'))); | |
72 end | |
73 | |
74 % test with vectors | |
75 v1 = ao(ones(100, 1)); | |
76 v2 = ao(ones(100, 1)); | |
77 | |
78 % two AOs and a scalar | |
79 v = rotate(v1, v2, pi); | |
80 v1r = index(v, 1); | |
81 v2r = index(v, 2); | |
82 assert(all(abs(v1r.y - -1) < 1e-15)); | |
83 assert(all(abs(v2r.y - -1) < 1e-15)); | |
84 | |
85 % invalid call with two AOs and a scalar as a modifier | |
86 try | |
87 rotate(v1, v2, pi); | |
88 assert(false, 'this call should fail'); | |
89 catch e | |
90 assert(strncmp(e.message, '### ao/rotate can not be used as a modifier method. Please give at least one output', ... | |
91 length('### ao/rotate can not be used as a modifier method. Please give at least one output'))); | |
92 end | |
93 | |
94 % test with sine waves and different rotation angle | |
95 v1 = ao(plist('waveform', 'sine', 'fs', 10, 'nsecs', 100, 'f', 0.01)); | |
96 v2 = ao(plist('tsfcn', 'zeros(size(t))', 'fs', 10, 'nsecs', 100)); | |
97 % iplot(v1, v2); | |
98 | |
99 % rotate by 45 degrees | |
100 v = rotate(v1, v2, 0.25*pi); | |
101 v1r = index(v, 1); | |
102 v2r = index(v, 2); | |
103 % iplot(v1r, v2r); | |
104 assert(almostEqual(v1r, v1/sqrt(2))); | |
105 assert(almostEqual(v2r, v1/sqrt(2))); | |
106 | |
107 % rotate by 90 degrees | |
108 v = rotate(v1, v2, 0.5*pi); | |
109 v1r = index(v, 1); | |
110 v2r = index(v, 2); | |
111 % iplot(v1r, v2r); | |
112 assert(almostEqual(v1r, v2)); | |
113 assert(almostEqual(v2r, v1)); | |
114 | |
115 % rotate by 180 degrees | |
116 v = rotate(v1, v2, pi); | |
117 v1r = index(v, 1); | |
118 v2r = index(v, 2); | |
119 % iplot(v1r, v2r); | |
120 assert(almostEqual(v1r, -v1)); | |
121 assert(almostEqual(v2r, v2)); | |
122 | |
123 % rotate by -45 degrees | |
124 v = rotate(v1, v2, -0.25*pi); | |
125 v1r = index(v, 1); | |
126 v2r = index(v, 2); | |
127 % iplot(v1r, v2r); | |
128 assert(almostEqual(v1r, v1/sqrt(2))); | |
129 assert(almostEqual(v2r, -v1/sqrt(2))); | |
130 | |
131 end | |
132 | |
133 function rv = almostEqual(x, y, t) | |
134 if nargin == 2 | |
135 t = 1e-15; | |
136 end | |
137 rv = all(abs(x.y - y.y) < t); | |
138 end |