Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/tests/test_specdesc.py

    r8e9cb57 r0b6d23d  
    11#! /usr/bin/env python
    22
     3from unittest import main
    34from numpy.testing import TestCase, assert_equal, assert_almost_equal
    45from numpy import random, arange, log, zeros
    5 from aubio import specdesc, cvec
    6 from math import pi
     6from aubio import specdesc, cvec, float_type
    77
    88methods = ["default",
     
    3030
    3131        for method in methods:
    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('float32')
    41           spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
    42           #print "%20s" % method, str(o(spec))
    43           assert (o(spec) != 0.)
    44 
    45     def test_hfc(self):
    46         o = specdesc("hfc", buf_size)
    47         spec = cvec(buf_size)
    48         # hfc of zeros is zero
    49         assert_equal (o(spec), 0.)
    50         # hfc of ones is sum of all bin numbers
    51         spec.norm[:] = 1
    52         expected = sum(range(buf_size/2 + 2))
    53         assert_equal (o(spec), expected)
    54         # changing phase doesn't change anything
    55         spec.phas[:] = 1
    56         assert_equal (o(spec), sum(range(buf_size/2 + 2)))
     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.)
    5744
    5845    def test_phase(self):
     
    6148        # phase of zeros is zero
    6249        assert_equal (o(spec), 0.)
    63         spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
     50        spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
    6451        # phase of random is not zero
    6552        spec.norm[:] = 1
     
    7158        # specdiff of zeros is zero
    7259        assert_equal (o(spec), 0.)
    73         spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
     60        spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
    7461        # phase of random is not zero
    7562        spec.norm[:] = 1
     
    8067        c = cvec()
    8168        assert_equal( 0., o(c))
    82         a = arange(c.length, dtype='float32')
     69        a = arange(c.length, dtype=float_type)
    8370        c.norm = a
    8471        assert_equal (a, c.norm)
     
    8976        c = cvec()
    9077        assert_equal( 0., o(c))
    91         a = arange(c.length, dtype='float32')
     78        a = arange(c.length, dtype=float_type)
    9279        c.norm = a
    9380        assert_equal (a, c.norm)
     
    10289        c = cvec()
    10390        assert_equal( 0., o(c))
    104         a = arange(c.length, dtype='float32')
     91        a = arange(c.length, dtype=float_type)
    10592        c.norm = a
    10693        assert_almost_equal( sum(a * log(1.+ a/1.e-1 ) ) / o(c), 1., decimal=6)
     
    11097        c = cvec()
    11198        assert_equal( 0., o(c))
    112         a = arange(c.length, dtype='float32')
     99        a = arange(c.length, dtype=float_type)
    113100        c.norm = a
    114101        assert_almost_equal( sum(log(1.+ a/1.e-1 ) ) / o(c), 1, decimal=6)
     
    118105        c = cvec()
    119106        assert_equal( 0., o(c))
    120         a = arange(c.length, dtype='float32')
     107        a = arange(c.length, dtype=float_type)
    121108        c.norm = a
    122109        assert_equal( sum(a), o(c))
    123110        assert_equal( 0, o(c))
    124         c.norm = zeros(c.length, dtype='float32')
     111        c.norm = zeros(c.length, dtype=float_type)
    125112        assert_equal( 0, o(c))
    126113
     
    130117        # make sure centroid of zeros is zero
    131118        assert_equal( 0., o(c))
    132         a = arange(c.length, dtype='float32')
     119        a = arange(c.length, dtype=float_type)
    133120        c.norm = a
    134121        centroid = sum(a*a) / sum(a)
     
    141128        o = specdesc("spread")
    142129        c = cvec(2048)
    143         ramp = arange(c.length, dtype='float32')
     130        ramp = arange(c.length, dtype=float_type)
    144131        assert_equal( 0., o(c))
    145132
     
    154141        c = cvec()
    155142        assert_equal( 0., o(c))
    156         a = arange(c.length, dtype='float32')
     143        a = arange(c.length, dtype=float_type)
    157144        c.norm = a
    158145        centroid = sum(a*a) / sum(a)
     
    168155        c = cvec()
    169156        assert_equal( 0., o(c))
    170         a = arange(c.length, dtype='float32')
     157        a = arange(c.length, dtype=float_type)
    171158        c.norm = a
    172159        centroid = sum(a*a) / sum(a)
     
    179166        c = cvec()
    180167        assert_equal( 0., o(c))
    181         a = arange(c.length * 2, 0, -2, dtype='float32')
    182         k = arange(c.length, dtype='float32')
     168        a = arange(c.length * 2, 0, -2, dtype=float_type)
     169        k = arange(c.length, dtype=float_type)
    183170        c.norm = a
    184171        num = len(a) * sum(k*a) - sum(k)*sum(a)
     
    187174        assert_almost_equal (slope, o(c), decimal = 5)
    188175
    189         a = arange(0, c.length * 2, +2, dtype='float32')
     176        a = arange(0, c.length * 2, +2, dtype=float_type)
    190177        c.norm = a
    191178        num = len(a) * sum(k*a) - sum(k)*sum(a)
     
    194181        assert_almost_equal (slope, o(c), decimal = 5)
    195182
    196         a = arange(0, c.length * 2, +2, dtype='float32')
     183        a = arange(0, c.length * 2, +2, dtype=float_type)
    197184        c.norm = a * 2
    198185        assert_almost_equal (slope, o(c), decimal = 5)
     
    202189        c = cvec()
    203190        assert_equal( 0., o(c))
    204         a = arange(c.length * 2, 0, -2, dtype='float32')
    205         k = arange(c.length, dtype='float32')
     191        a = arange(c.length * 2, 0, -2, dtype=float_type)
     192        k = arange(c.length, dtype=float_type)
    206193        c.norm = a
    207194        decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:])
    208195        assert_almost_equal (decrease, o(c), decimal = 5)
    209196
    210         a = arange(0, c.length * 2, +2, dtype='float32')
     197        a = arange(0, c.length * 2, +2, dtype=float_type)
    211198        c.norm = a
    212199        decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:])
    213200        assert_almost_equal (decrease, o(c), decimal = 5)
    214201
    215         a = arange(0, c.length * 2, +2, dtype='float32')
     202        a = arange(0, c.length * 2, +2, dtype=float_type)
    216203        c.norm = a * 2
    217204        decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:])
     
    222209        c = cvec()
    223210        assert_equal( 0., o(c))
    224         a = arange(c.length * 2, 0, -2, dtype='float32')
    225         k = arange(c.length, dtype='float32')
     211        a = arange(c.length * 2, 0, -2, dtype=float_type)
    226212        c.norm = a
    227213        cumsum = .95*sum(a*a)
    228214        i = 0; rollsum = 0
    229215        while rollsum < cumsum:
    230           rollsum += a[i]*a[i]
    231           i+=1
     216            rollsum += a[i]*a[i]
     217            i+=1
    232218        rolloff = i
    233219        assert_equal (rolloff, o(c))
    234220
     221class aubio_specdesc_wrong(TestCase):
     222
     223    def test_negative(self):
     224        with self.assertRaises(ValueError):
     225            specdesc("default", -10)
     226
     227    def test_unknown(self):
     228        # FIXME should fail?
     229        with self.assertRaises(ValueError):
     230            specdesc("unknown", 512)
     231            self.skipTest('todo: new_specdesc should fail on wrong method')
    235232
    236233if __name__ == '__main__':
    237     from unittest import main
    238234    main()
Note: See TracChangeset for help on using the changeset viewer.