comparison m-toolbox/test/test_ao_simplifyYunits.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 mc
2
3 %% Make test AOs
4
5 nsecs = 10000;
6 fs = 10;
7
8 pl = plist('nsecs', nsecs, 'fs', fs, 'tsfcn', 'sin(2*pi*7.433*t) + randn(size(t))');
9
10 a1 = ao(pl);
11 a1.setName('a1');
12 a1.setYunits('m');
13
14 % This should output N
15 iunits1 = unit('m');
16 ounits1 = unit('V');
17 iunits2 = unit('V');
18 ounits2 = unit('N');
19
20 lp = miir(plist('type', 'lowpass', 'fs', fs, 'fc', 0.1, 'order', 5,'iunits', iunits1, 'ounits', ounits1));
21 hp = miir(plist('type', 'highpass', 'fs', fs, 'fc', 1, 'order', 2,'iunits', iunits2, 'ounits', ounits2));
22
23 a1f = filter(a1, [lp hp], plist('bank', 'serial'));
24
25 %% Simplify
26
27 b = a1f.simplifyYunits()
28
29 %% Units with prefixes
30
31
32 a = ao(1:10);
33 a.setYunits('mm m^-1')
34
35 a.simplifyYunits
36
37
38 %% Units with prefixes
39
40
41 a = ao(1:10);
42 a.setYunits('mm m Hz um^-2')
43
44 a.simplifyYunits(plist('prefixes', true))
45
46 %% More tests on prefixes
47 N = 8;
48
49 test = true(N,1);
50
51 % Case 1
52 jj = 1;
53 % A unit is canceled out but there is a remaining prefix AND a remaining unit.
54 % Example: 'fN mN^-1 Hz'
55 a = ao([1:10]',[2:2:20]',plist('xunits','s'));
56 a.setYunits('fN mN^-1 Hz');
57
58 % The unit Newton will be canceled out and the remaining prefix (here 'p') will be added to the Hz
59 b = simplifyYunits(a, plist('prefixes', true));
60 c = simplifyYunits(a, plist('prefixes', false));
61
62 % Result: 'pHz'
63 if ne(b.yunits, unit('Hz')) || ne(c.yunits, unit('pHz'))
64 test(jj) = false;
65 end
66
67 % Case 2
68 jj = 2;
69 % A unit is canceled out but there is a remaining prefix AND NO remaining unit.
70 % Example: 'fN mN^-1'
71 a = ao([1:10]',[2:2:20]',plist('xunits','s'));
72 a.setYunits('fN mN^-1');
73
74 % In this case it is not possible to keep the remaining prefix
75 % --> Don't simplify the unit (it is only possible if we compute the prefix to the values)
76 b = simplifyYunits(a, plist('prefixes', true));
77 c = simplifyYunits(a, plist('prefixes', false));
78
79 % Result: 'fN mN^-1'
80 if ne(b.yunits, unit('')) || ne(c.yunits, unit('fN mN^-1'))
81 test(jj) = false;
82 end
83
84 % Case 3
85 jj = 3;
86 % The 'normal' case
87 % Example: 'fN m^-2 mN^-2 Hz mm'
88 a = ao([1:10]',[2:2:20]',plist('xunits','s'));
89 a.setYunits('fN m^-2 mN^-2 Hz mm');
90
91 b = simplifyYunits(a, plist('prefixes', true));
92 c = simplifyYunits(a, plist('prefixes', false));
93
94 % Result: 'GN^-1 km^-1 Hz'
95 if ne(b.yunits, unit('N^-1 m^-1 Hz')) || ne(c.yunits, unit('GN^-1 km^-1 Hz'))
96 test(jj) = false;
97 end
98
99 % Case 4
100 jj = 4;
101 % Power
102 % Example: ('mm')^2
103 a = ao([1:10]',[2:2:20]',plist('xunits','s'));
104 a.setYunits('mm');
105
106 b = simplifyYunits(a.^2, plist('prefixes', true));
107 c = simplifyYunits(a.^2, plist('prefixes', false));
108
109 % Result: 'mm^2'
110 if ne(b.yunits, unit('m^2')) || ne(c.yunits, unit('mm^2'))
111 test(jj) = false;
112 end
113
114 % Case 5
115 jj = 5;
116 % Power
117 n = utils.math.randelement([-4:-1 1:4],1);
118 % Example: ('mm^-1 nm^-1')^n
119 a = ao([1:10]',[2:2:20]',plist('xunits','s'));
120 a.setYunits('mm^-1 nm^-1');
121
122 b = simplifyYunits(a.^n, plist('prefixes', true));
123 c = simplifyYunits(a.^n, plist('prefixes', false));
124
125 % Result: 'um^-n'
126 if ne(b.yunits, unit(['m^' num2str(n*-2)])) || ne(c.yunits, unit(['um^' num2str(n*-2)]))
127 test(jj) = false;
128 end
129
130 % Case 6
131 jj = 6;
132 % A unit is canceled out but there is a remaining prefix AND a
133 % remaining unit.
134 % Example: ('mm um ks^-1 s')
135 a = ao([1:10]',[2:2:20]',plist('xunits','s'));
136 a.setYunits('mm um ks^-1 s');
137
138 b = simplifyYunits(a, plist('prefixes', true))
139 c = simplifyYunits(a, plist('prefixes', false))
140
141 % Result: 'um^2'
142 if ne(b.yunits, unit('m^2')) || ne(c.yunits, unit('um^2'))
143 test(jj) = false;
144 end
145
146 % Case 7
147 jj = 7;
148 % A unit is canceled out but there is a remaining prefix AND a
149 % remaining unit.
150 % Example: ('mm um ks^-1 s')
151 a = ao([1:10]',[2:2:20]',plist('xunits','s'));
152 a.setYunits('mm um ks^-1 s');
153
154 b = simplifyYunits(sqrt(a), plist('prefixes', true))
155 c = simplifyYunits(sqrt(a), plist('prefixes', false))
156
157 % Result: 'um^2'
158 if ne(b.yunits, unit('m')) || ne(c.yunits, unit('um'))
159 test(jj) = false;
160 end
161
162 % Case 8
163 jj = 8;
164 % Sqrt of Power
165 n = utils.math.randelement([-4:-2 2:4],1);
166 % Example: ('mm^-1 nm^-1')^2n
167 a = ao([1:10]',[2:2:20]',plist('xunits','s'));
168 a.setYunits('mm^-1 nm^-1');
169
170 b = simplifyYunits(sqrt(a.^(2*n)), plist('prefixes', true))
171 c = simplifyYunits(sqrt(a.^(2*n)), plist('prefixes', false))
172
173 % Result: 'um^-n'
174 if ne(b.yunits, unit(['m^' num2str(n*-2)])) || ne(c.yunits, unit(['um^' num2str(n*-2)]))
175 test(jj) = false;
176 end