1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | from numpy.testing import TestCase, assert_equal, assert_array_less |
---|
4 | from aubio import fvec, cvec, pvoc, float_type |
---|
5 | from numpy import array, shape |
---|
6 | from numpy.random import random |
---|
7 | import numpy as np |
---|
8 | |
---|
9 | max_sq_error = 1.e-12 |
---|
10 | |
---|
11 | def create_sine(hop_s, freq, samplerate): |
---|
12 | t = np.arange(hop_s).astype(float_type) |
---|
13 | return np.sin( 2. * np.pi * t * freq / samplerate) |
---|
14 | |
---|
15 | def create_noise(hop_s): |
---|
16 | return np.random.rand(hop_s).astype(float_type) * 2. - 1. |
---|
17 | |
---|
18 | class aubio_pvoc_test_case(TestCase): |
---|
19 | """ pvoc object test case """ |
---|
20 | |
---|
21 | def test_members_automatic_sizes_default(self): |
---|
22 | """ check object creation with default parameters """ |
---|
23 | f = pvoc() |
---|
24 | assert_equal ([f.win_s, f.hop_s], [1024, 512]) |
---|
25 | |
---|
26 | def test_members_unnamed_params(self): |
---|
27 | """ check object creation with unnamed parameters """ |
---|
28 | f = pvoc(2048, 128) |
---|
29 | assert_equal ([f.win_s, f.hop_s], [2048, 128]) |
---|
30 | |
---|
31 | def test_members_named_params(self): |
---|
32 | """ check object creation with named parameters """ |
---|
33 | f = pvoc(hop_s = 128, win_s = 2048) |
---|
34 | assert_equal ([f.win_s, f.hop_s], [2048, 128]) |
---|
35 | |
---|
36 | def test_zeros(self): |
---|
37 | """ check the resynthesis of zeros gives zeros """ |
---|
38 | win_s, hop_s = 1024, 256 |
---|
39 | f = pvoc (win_s, hop_s) |
---|
40 | t = fvec (hop_s) |
---|
41 | for time in range( int ( 4 * win_s / hop_s ) ): |
---|
42 | s = f(t) |
---|
43 | r = f.rdo(s) |
---|
44 | assert_equal ( array(t), 0) |
---|
45 | assert_equal ( s.norm, 0) |
---|
46 | assert_equal ( s.phas, 0) |
---|
47 | assert_equal ( r, 0) |
---|
48 | |
---|
49 | def test_resynth_8_steps_sine(self): |
---|
50 | """ check the resynthesis of is correct with 87.5% overlap """ |
---|
51 | hop_s = 1024 |
---|
52 | ratio = 8 |
---|
53 | freq = 445; samplerate = 22050 |
---|
54 | sigin = create_sine(hop_s, freq, samplerate) |
---|
55 | self.reconstruction( sigin, hop_s, ratio) |
---|
56 | |
---|
57 | def test_resynth_8_steps(self): |
---|
58 | """ check the resynthesis of is correct with 87.5% overlap """ |
---|
59 | hop_s = 1024 |
---|
60 | ratio = 8 |
---|
61 | sigin = create_noise(hop_s) |
---|
62 | self.reconstruction(sigin, hop_s, ratio) |
---|
63 | |
---|
64 | def test_resynth_4_steps_sine(self): |
---|
65 | """ check the resynthesis of is correct with 87.5% overlap """ |
---|
66 | hop_s = 1024 |
---|
67 | ratio = 4 |
---|
68 | freq = 445; samplerate = 22050 |
---|
69 | sigin = create_sine(hop_s, freq, samplerate) |
---|
70 | self.reconstruction(sigin, hop_s, ratio) |
---|
71 | |
---|
72 | def test_resynth_4_steps(self): |
---|
73 | """ check the resynthesis of is correct with 75% overlap """ |
---|
74 | hop_s = 1024 |
---|
75 | ratio = 4 |
---|
76 | sigin = create_noise(hop_s) |
---|
77 | self.reconstruction(sigin, hop_s, ratio) |
---|
78 | |
---|
79 | def test_resynth_2_steps_sine(self): |
---|
80 | """ check the resynthesis of is correct with 50% overlap """ |
---|
81 | hop_s = 1024 |
---|
82 | ratio = 2 |
---|
83 | freq = 445; samplerate = 22050 |
---|
84 | sigin = create_sine(hop_s, freq, samplerate) |
---|
85 | self.reconstruction(sigin, hop_s, ratio) |
---|
86 | |
---|
87 | def test_resynth_2_steps(self): |
---|
88 | """ check the resynthesis of is correct with 50% overlap """ |
---|
89 | hop_s = 1024 |
---|
90 | ratio = 2 |
---|
91 | sigin = create_noise(hop_s) |
---|
92 | self.reconstruction(sigin, hop_s, ratio) |
---|
93 | |
---|
94 | def reconstruction(self, sigin, hop_s, ratio): |
---|
95 | buf_s = hop_s * ratio |
---|
96 | f = pvoc(buf_s, hop_s) |
---|
97 | zeros = fvec(hop_s) |
---|
98 | r2 = f.rdo( f(sigin) ) |
---|
99 | for i in range(1, ratio): |
---|
100 | r2 = f.rdo( f(zeros) ) |
---|
101 | # compute square errors |
---|
102 | sq_error = (r2 - sigin)**2 |
---|
103 | # make sure all square errors are less than desired precision |
---|
104 | assert_array_less(sq_error, max_sq_error) |
---|
105 | |
---|
106 | if __name__ == '__main__': |
---|
107 | from unittest import main |
---|
108 | main() |
---|
109 | |
---|