source: python/tests/test_specdesc.py @ 929d76c

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since 929d76c was 12e91a6, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/tests/test_specdesc.py: RuntimeError? is now raised on wrong mode

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