source: interfaces/python/test_onsetdetection.py @ b8aaaff

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since b8aaaff was 4c01c0f, checked in by Paul Brossier <piem@piem.org>, 15 years ago

test_*.py: use aubio module

  • Property mode set to 100644
File size: 5.7 KB
Line 
1from numpy.testing import TestCase, run_module_suite
2from numpy.testing import assert_equal, assert_almost_equal
3# WARNING: numpy also has an fft object
4from aubio import specdesc, cvec
5from numpy import array, shape, arange, zeros, log
6from math import pi
7
8class aubio_specdesc(TestCase):
9
10    def test_members(self):
11        o = specdesc()
12        assert_equal ([o.buf_size, o.method],
13            [1024, "default"])
14
15    def test_hfc(self):
16        o = specdesc("hfc")
17        c = cvec()
18        assert_equal( 0., o(c))
19        a = arange(c.length, dtype='float32')
20        c.norm = a
21        assert_equal (a, c.norm)
22        assert_equal ( sum(a*(a+1)), o(c))
23
24    def test_complex(self):
25        o = specdesc("complex")
26        c = cvec()
27        assert_equal( 0., o(c))
28        a = arange(c.length, dtype='float32')
29        c.norm = a
30        assert_equal (a, c.norm)
31        # the previous run was on zeros, so previous frames are still 0
32        # so we have sqrt ( abs ( r2 ^ 2) ) == r2
33        assert_equal ( sum(a), o(c))
34        # second time. c.norm = a, so, r1 = r2, and the euclidian distance is 0
35        assert_equal ( 0, o(c))
36
37    def test_phase(self):
38        o = specdesc("phase")
39        c = cvec()
40        assert_equal( 0., o(c))
41
42    def test_kl(self):
43        o = specdesc("kl")
44        c = cvec()
45        assert_equal( 0., o(c))
46        a = arange(c.length, dtype='float32')
47        c.norm = a
48        assert_almost_equal( sum(a * log(1.+ a/1.e-10 ) ) / o(c), 1., decimal=6)
49
50    def test_mkl(self):
51        o = specdesc("mkl")
52        c = cvec()
53        assert_equal( 0., o(c))
54        a = arange(c.length, dtype='float32')
55        c.norm = a
56        assert_almost_equal( sum(log(1.+ a/1.e-10 ) ) / o(c), 1, decimal=6)
57
58    def test_specflux(self):
59        o = specdesc("specflux")
60        c = cvec()
61        assert_equal( 0., o(c))
62        a = arange(c.length, dtype='float32')
63        c.norm = a
64        assert_equal( sum(a), o(c))
65        assert_equal( 0, o(c))
66        c.norm = zeros(c.length, dtype='float32')
67        assert_equal( 0, o(c))
68
69    def test_centroid(self):
70        o = specdesc("centroid")
71        c = cvec()
72        # make sure centroid of zeros is zero
73        assert_equal( 0., o(c))
74        a = arange(c.length, dtype='float32')
75        c.norm = a
76        centroid = sum(a*a) / sum(a)
77        assert_almost_equal (centroid, o(c), decimal = 2)
78
79        c.norm = a * .5 
80        assert_almost_equal (centroid, o(c), decimal = 2)
81
82    def test_spread(self):
83        o = specdesc("spread")
84        c = cvec()
85        assert_equal( 0., o(c))
86        a = arange(c.length, dtype='float32')
87        c.norm = a
88        centroid = sum(a*a) / sum(a)
89        spread = sum( (a - centroid)**2 *a) / sum(a)
90        assert_almost_equal (spread, o(c), decimal = 2)
91
92        c.norm = a * 3
93        assert_almost_equal (spread, o(c), decimal = 2)
94
95    def test_skewness(self):
96        o = specdesc("skewness")
97        c = cvec()
98        assert_equal( 0., o(c))
99        a = arange(c.length, dtype='float32')
100        c.norm = a
101        centroid = sum(a*a) / sum(a)
102        spread = sum( (a - centroid)**2 *a) / sum(a)
103        skewness = sum( (a - centroid)**3 *a) / sum(a) / spread **1.5
104        assert_almost_equal (skewness, o(c), decimal = 2)
105
106        c.norm = a * 3
107        assert_almost_equal (skewness, o(c), decimal = 2)
108
109    def test_kurtosis(self):
110        o = specdesc("kurtosis")
111        c = cvec()
112        assert_equal( 0., o(c))
113        a = arange(c.length, dtype='float32')
114        c.norm = a
115        centroid = sum(a*a) / sum(a)
116        spread = sum( (a - centroid)**2 *a) / sum(a)
117        kurtosis = sum( (a - centroid)**4 *a) / sum(a) / spread **2
118        assert_almost_equal (kurtosis, o(c), decimal = 2)
119
120    def test_slope(self):
121        o = specdesc("slope")
122        c = cvec()
123        assert_equal( 0., o(c))
124        a = arange(c.length * 2, 0, -2, dtype='float32')
125        k = arange(c.length, dtype='float32')
126        c.norm = a
127        num = len(a) * sum(k*a) - sum(k)*sum(a)
128        den = (len(a) * sum(k**2) - sum(k)**2)
129        slope = num/den/sum(a)
130        assert_almost_equal (slope, o(c), decimal = 5)
131
132        a = arange(0, c.length * 2, +2, dtype='float32')
133        c.norm = a
134        num = len(a) * sum(k*a) - sum(k)*sum(a)
135        den = (len(a) * sum(k**2) - sum(k)**2)
136        slope = num/den/sum(a)
137        assert_almost_equal (slope, o(c), decimal = 5)
138
139        a = arange(0, c.length * 2, +2, dtype='float32')
140        c.norm = a * 2
141        assert_almost_equal (slope, o(c), decimal = 5)
142
143    def test_decrease(self):
144        o = specdesc("decrease")
145        c = cvec()
146        assert_equal( 0., o(c))
147        a = arange(c.length * 2, 0, -2, dtype='float32')
148        k = arange(c.length, dtype='float32')
149        c.norm = a
150        decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 
151        assert_almost_equal (decrease, o(c), decimal = 5)
152
153        a = arange(0, c.length * 2, +2, dtype='float32')
154        c.norm = a
155        decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 
156        assert_almost_equal (decrease, o(c), decimal = 5)
157
158        a = arange(0, c.length * 2, +2, dtype='float32')
159        c.norm = a * 2
160        decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 
161        assert_almost_equal (decrease, o(c), decimal = 5)
162
163    def test_rolloff(self):
164        o = specdesc("rolloff")
165        c = cvec()
166        assert_equal( 0., o(c))
167        a = arange(c.length * 2, 0, -2, dtype='float32')
168        k = arange(c.length, dtype='float32')
169        c.norm = a
170        cumsum = .95*sum(a*a)
171        i = 0; rollsum = 0
172        while rollsum < cumsum:
173          rollsum += a[i]*a[i]
174          i+=1
175        rolloff = i
176        assert_equal (rolloff, o(c))
177
178if __name__ == '__main__':
179    from unittest import main
180    main()
Note: See TracBrowser for help on using the repository browser.