source: python/tests/test_specdesc.py @ 18278232

feature/crepe_org
Last change on this file since 18278232 was bb63c19, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[tests] move unittest import to main

  • Property mode set to 100755
File size: 7.1 KB
RevLine 
[75e715f]1#! /usr/bin/env python
[312826c]2
3from numpy.testing import TestCase, assert_equal, assert_almost_equal
4from numpy import random, arange, log, zeros
[c4b2183]5from aubio import specdesc, cvec, float_type
[0536612]6
[312826c]7methods = ["default",
8     "energy",
9     "hfc",
10     "complex",
11     "phase",
12     "specdiff",
13     "kl",
14     "mkl",
15     "specflux",
16     "centroid",
17     "spread",
18     "skewness",
19     "kurtosis",
20     "slope",
21     "decrease",
22     "rolloff"]
23buf_size = 2048
24
[0536612]25class aubio_specdesc(TestCase):
26
27    def test_members(self):
28        o = specdesc()
[312826c]29
30        for method in methods:
[0b6d23d]31            o = specdesc(method, buf_size)
32            assert_equal ([o.buf_size, o.method], [buf_size, method])
33
34            spec = cvec(buf_size)
35            spec.norm[0] = 1
36            spec.norm[1] = 1./2.
37            #print "%20s" % method, str(o(spec))
38            o(spec)
39            spec.norm = random.random_sample((len(spec.norm),)).astype(float_type)
40            spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
41            #print "%20s" % method, str(o(spec))
42            assert (o(spec) != 0.)
[312826c]43
44    def test_phase(self):
45        o = specdesc("phase", buf_size)
46        spec = cvec(buf_size)
47        # phase of zeros is zero
48        assert_equal (o(spec), 0.)
[c4b2183]49        spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
[312826c]50        # phase of random is not zero
51        spec.norm[:] = 1
52        assert (o(spec) != 0.)
53
54    def test_specdiff(self):
55        o = specdesc("phase", buf_size)
56        spec = cvec(buf_size)
57        # specdiff of zeros is zero
58        assert_equal (o(spec), 0.)
[c4b2183]59        spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
[312826c]60        # phase of random is not zero
61        spec.norm[:] = 1
62        assert (o(spec) != 0.)
63   
64    def test_hfc(self):
[0536612]65        o = specdesc("hfc")
66        c = cvec()
67        assert_equal( 0., o(c))
[c4b2183]68        a = arange(c.length, dtype=float_type)
[0536612]69        c.norm = a
[1a6ef2c]70        assert_equal (a, c.norm)
[0536612]71        assert_equal ( sum(a*(a+1)), o(c))
72
73    def test_complex(self):
74        o = specdesc("complex")
75        c = cvec()
76        assert_equal( 0., o(c))
[c4b2183]77        a = arange(c.length, dtype=float_type)
[0536612]78        c.norm = a
[1a6ef2c]79        assert_equal (a, c.norm)
[0536612]80        # the previous run was on zeros, so previous frames are still 0
81        # so we have sqrt ( abs ( r2 ^ 2) ) == r2
82        assert_equal ( sum(a), o(c))
83        # second time. c.norm = a, so, r1 = r2, and the euclidian distance is 0
84        assert_equal ( 0, o(c))
85
86    def test_kl(self):
87        o = specdesc("kl")
88        c = cvec()
89        assert_equal( 0., o(c))
[c4b2183]90        a = arange(c.length, dtype=float_type)
[0536612]91        c.norm = a
[8e9cb57]92        assert_almost_equal( sum(a * log(1.+ a/1.e-1 ) ) / o(c), 1., decimal=6)
[0536612]93
94    def test_mkl(self):
95        o = specdesc("mkl")
96        c = cvec()
97        assert_equal( 0., o(c))
[c4b2183]98        a = arange(c.length, dtype=float_type)
[0536612]99        c.norm = a
[8e9cb57]100        assert_almost_equal( sum(log(1.+ a/1.e-1 ) ) / o(c), 1, decimal=6)
[0536612]101
102    def test_specflux(self):
103        o = specdesc("specflux")
104        c = cvec()
105        assert_equal( 0., o(c))
[c4b2183]106        a = arange(c.length, dtype=float_type)
[0536612]107        c.norm = a
108        assert_equal( sum(a), o(c))
109        assert_equal( 0, o(c))
[c4b2183]110        c.norm = zeros(c.length, dtype=float_type)
[0536612]111        assert_equal( 0, o(c))
112
113    def test_centroid(self):
114        o = specdesc("centroid")
115        c = cvec()
116        # make sure centroid of zeros is zero
117        assert_equal( 0., o(c))
[c4b2183]118        a = arange(c.length, dtype=float_type)
[0536612]119        c.norm = a
120        centroid = sum(a*a) / sum(a)
121        assert_almost_equal (centroid, o(c), decimal = 2)
122
123        c.norm = a * .5 
124        assert_almost_equal (centroid, o(c), decimal = 2)
125
126    def test_spread(self):
127        o = specdesc("spread")
[9530365]128        c = cvec(1024)
[c4b2183]129        ramp = arange(c.length, dtype=float_type)
[0536612]130        assert_equal( 0., o(c))
[312826c]131
132        a = ramp
[0536612]133        c.norm = a
134        centroid = sum(a*a) / sum(a)
[312826c]135        spread = sum( a * pow(ramp - centroid, 2.) ) / sum(a)
136        assert_almost_equal (o(c), spread, decimal = 1)
[0536612]137
138    def test_skewness(self):
139        o = specdesc("skewness")
140        c = cvec()
141        assert_equal( 0., o(c))
[c4b2183]142        a = arange(c.length, dtype=float_type)
[0536612]143        c.norm = a
144        centroid = sum(a*a) / sum(a)
145        spread = sum( (a - centroid)**2 *a) / sum(a)
146        skewness = sum( (a - centroid)**3 *a) / sum(a) / spread **1.5
147        assert_almost_equal (skewness, o(c), decimal = 2)
148
149        c.norm = a * 3
150        assert_almost_equal (skewness, o(c), decimal = 2)
151
152    def test_kurtosis(self):
153        o = specdesc("kurtosis")
154        c = cvec()
155        assert_equal( 0., o(c))
[c4b2183]156        a = arange(c.length, dtype=float_type)
[0536612]157        c.norm = a
158        centroid = sum(a*a) / sum(a)
159        spread = sum( (a - centroid)**2 *a) / sum(a)
160        kurtosis = sum( (a - centroid)**4 *a) / sum(a) / spread **2
161        assert_almost_equal (kurtosis, o(c), decimal = 2)
162
163    def test_slope(self):
164        o = specdesc("slope")
165        c = cvec()
166        assert_equal( 0., o(c))
[c4b2183]167        a = arange(c.length * 2, 0, -2, dtype=float_type)
168        k = arange(c.length, dtype=float_type)
[0536612]169        c.norm = a
170        num = len(a) * sum(k*a) - sum(k)*sum(a)
171        den = (len(a) * sum(k**2) - sum(k)**2)
172        slope = num/den/sum(a)
173        assert_almost_equal (slope, o(c), decimal = 5)
174
[c4b2183]175        a = arange(0, c.length * 2, +2, dtype=float_type)
[0536612]176        c.norm = a
177        num = len(a) * sum(k*a) - sum(k)*sum(a)
178        den = (len(a) * sum(k**2) - sum(k)**2)
179        slope = num/den/sum(a)
180        assert_almost_equal (slope, o(c), decimal = 5)
181
[c4b2183]182        a = arange(0, c.length * 2, +2, dtype=float_type)
[0536612]183        c.norm = a * 2
184        assert_almost_equal (slope, o(c), decimal = 5)
185
186    def test_decrease(self):
187        o = specdesc("decrease")
188        c = cvec()
189        assert_equal( 0., o(c))
[c4b2183]190        a = arange(c.length * 2, 0, -2, dtype=float_type)
191        k = arange(c.length, dtype=float_type)
[0536612]192        c.norm = a
193        decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 
194        assert_almost_equal (decrease, o(c), decimal = 5)
195
[c4b2183]196        a = arange(0, c.length * 2, +2, dtype=float_type)
[0536612]197        c.norm = a
198        decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 
199        assert_almost_equal (decrease, o(c), decimal = 5)
200
[c4b2183]201        a = arange(0, c.length * 2, +2, dtype=float_type)
[0536612]202        c.norm = a * 2
203        decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 
204        assert_almost_equal (decrease, o(c), decimal = 5)
205
206    def test_rolloff(self):
207        o = specdesc("rolloff")
208        c = cvec()
209        assert_equal( 0., o(c))
[c4b2183]210        a = arange(c.length * 2, 0, -2, dtype=float_type)
[0536612]211        c.norm = a
212        cumsum = .95*sum(a*a)
213        i = 0; rollsum = 0
214        while rollsum < cumsum:
[0b6d23d]215            rollsum += a[i]*a[i]
216            i+=1
[0536612]217        rolloff = i
218        assert_equal (rolloff, o(c))
219
[aba0279]220class aubio_specdesc_wrong(TestCase):
221
222    def test_negative(self):
223        with self.assertRaises(ValueError):
[0b6d23d]224            specdesc("default", -10)
[aba0279]225
226    def test_unknown(self):
[12e91a6]227        with self.assertRaises(RuntimeError):
[0b6d23d]228            specdesc("unknown", 512)
[312826c]229
[0536612]230if __name__ == '__main__':
[bb63c19]231    from unittest import main
[0536612]232    main()
Note: See TracBrowser for help on using the repository browser.