0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 function test_timeaverage()
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 % create a tsdata ao
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 a = ao(plist('waveform', 'noise', 'fs', 10, 'nsecs', 1000));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 % times vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 times = [ 0 100 200 300 400 500 ];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 % average
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 b1 = timeaverage(a, plist('times', times));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 % this should have produced a time series with 3 points
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 assert(len(b1) == 3);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 % check expected X values
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 assert(almostEqual(b1.x, [50 250 450], 0.055));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 % check expected Y values
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 assert(almostEqual(b1.y, [0; 0; 0], 0.1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 % average
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20 b2 = timeaverage(a, plist('start', 0, 'duration', 100, 'decay', 100, 'repetitions', 3));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 % this should have produced the same values
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 assert(len(b2) == 3);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 assert(almostEqual(b2.x, b1.x));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 assert(almostEqual(b2.y, b1.y));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 % average
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 b3 = timeaverage(a, plist('times', times, 'function', @mean));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 % this should have produced the same values
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 assert(len(b3) == 3);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 assert(almostEqual(b3.x, b1.x));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 assert(almostEqual(b3.y, b1.y));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 % average
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 b4 = timeaverage(a, plist('times', times, 'xfunction', @min, 'yfunction', @mean));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 % this should have produced the same values
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 assert(len(b4) == 3);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 assert(almostEqual(b4.x, [0 200 400]));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 assert(almostEqual(b4.y, b1.y));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 % average
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 b5 = timeaverage(a, plist('times', times, 'function', 'center'));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 % this should have produced similar values
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 assert(len(b5) == 3);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 % check expected X values
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 assert(almostEqual(b1.x, [50 250 450], 0.055));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 % check expected Y values
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 assert(almostEqual(b1.y, [0; 0; 0], 0.1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 function rv = almostEqual(x, y, delta)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 % fix dimensions
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57 x = x(:);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 y = y(:);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 % default tollerance
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 if nargin < 3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 delta = 2*eps(x);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 rv = all(abs(x - y) < delta);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 end |