Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/tests/test_specdesc.py

    r0b6d23d r8e9cb57  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43from numpy.testing import TestCase, assert_equal, assert_almost_equal
    54from numpy import random, arange, log, zeros
    6 from aubio import specdesc, cvec, float_type
     5from aubio import specdesc, cvec
     6from math import pi
    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(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.)
     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)))
    4457
    4558    def test_phase(self):
     
    4861        # phase of zeros is zero
    4962        assert_equal (o(spec), 0.)
    50         spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
     63        spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
    5164        # phase of random is not zero
    5265        spec.norm[:] = 1
     
    5871        # specdiff of zeros is zero
    5972        assert_equal (o(spec), 0.)
    60         spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
     73        spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
    6174        # phase of random is not zero
    6275        spec.norm[:] = 1
     
    6780        c = cvec()
    6881        assert_equal( 0., o(c))
    69         a = arange(c.length, dtype=float_type)
     82        a = arange(c.length, dtype='float32')
    7083        c.norm = a
    7184        assert_equal (a, c.norm)
     
    7689        c = cvec()
    7790        assert_equal( 0., o(c))
    78         a = arange(c.length, dtype=float_type)
     91        a = arange(c.length, dtype='float32')
    7992        c.norm = a
    8093        assert_equal (a, c.norm)
     
    89102        c = cvec()
    90103        assert_equal( 0., o(c))
    91         a = arange(c.length, dtype=float_type)
     104        a = arange(c.length, dtype='float32')
    92105        c.norm = a
    93106        assert_almost_equal( sum(a * log(1.+ a/1.e-1 ) ) / o(c), 1., decimal=6)
     
    97110        c = cvec()
    98111        assert_equal( 0., o(c))
    99         a = arange(c.length, dtype=float_type)
     112        a = arange(c.length, dtype='float32')
    100113        c.norm = a
    101114        assert_almost_equal( sum(log(1.+ a/1.e-1 ) ) / o(c), 1, decimal=6)
     
    105118        c = cvec()
    106119        assert_equal( 0., o(c))
    107         a = arange(c.length, dtype=float_type)
     120        a = arange(c.length, dtype='float32')
    108121        c.norm = a
    109122        assert_equal( sum(a), o(c))
    110123        assert_equal( 0, o(c))
    111         c.norm = zeros(c.length, dtype=float_type)
     124        c.norm = zeros(c.length, dtype='float32')
    112125        assert_equal( 0, o(c))
    113126
     
    117130        # make sure centroid of zeros is zero
    118131        assert_equal( 0., o(c))
    119         a = arange(c.length, dtype=float_type)
     132        a = arange(c.length, dtype='float32')
    120133        c.norm = a
    121134        centroid = sum(a*a) / sum(a)
     
    128141        o = specdesc("spread")
    129142        c = cvec(2048)
    130         ramp = arange(c.length, dtype=float_type)
     143        ramp = arange(c.length, dtype='float32')
    131144        assert_equal( 0., o(c))
    132145
     
    141154        c = cvec()
    142155        assert_equal( 0., o(c))
    143         a = arange(c.length, dtype=float_type)
     156        a = arange(c.length, dtype='float32')
    144157        c.norm = a
    145158        centroid = sum(a*a) / sum(a)
     
    155168        c = cvec()
    156169        assert_equal( 0., o(c))
    157         a = arange(c.length, dtype=float_type)
     170        a = arange(c.length, dtype='float32')
    158171        c.norm = a
    159172        centroid = sum(a*a) / sum(a)
     
    166179        c = cvec()
    167180        assert_equal( 0., o(c))
    168         a = arange(c.length * 2, 0, -2, dtype=float_type)
    169         k = arange(c.length, dtype=float_type)
     181        a = arange(c.length * 2, 0, -2, dtype='float32')
     182        k = arange(c.length, dtype='float32')
    170183        c.norm = a
    171184        num = len(a) * sum(k*a) - sum(k)*sum(a)
     
    174187        assert_almost_equal (slope, o(c), decimal = 5)
    175188
    176         a = arange(0, c.length * 2, +2, dtype=float_type)
     189        a = arange(0, c.length * 2, +2, dtype='float32')
    177190        c.norm = a
    178191        num = len(a) * sum(k*a) - sum(k)*sum(a)
     
    181194        assert_almost_equal (slope, o(c), decimal = 5)
    182195
    183         a = arange(0, c.length * 2, +2, dtype=float_type)
     196        a = arange(0, c.length * 2, +2, dtype='float32')
    184197        c.norm = a * 2
    185198        assert_almost_equal (slope, o(c), decimal = 5)
     
    189202        c = cvec()
    190203        assert_equal( 0., o(c))
    191         a = arange(c.length * 2, 0, -2, dtype=float_type)
    192         k = arange(c.length, dtype=float_type)
     204        a = arange(c.length * 2, 0, -2, dtype='float32')
     205        k = arange(c.length, dtype='float32')
    193206        c.norm = a
    194207        decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:])
    195208        assert_almost_equal (decrease, o(c), decimal = 5)
    196209
    197         a = arange(0, c.length * 2, +2, dtype=float_type)
     210        a = arange(0, c.length * 2, +2, dtype='float32')
    198211        c.norm = a
    199212        decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:])
    200213        assert_almost_equal (decrease, o(c), decimal = 5)
    201214
    202         a = arange(0, c.length * 2, +2, dtype=float_type)
     215        a = arange(0, c.length * 2, +2, dtype='float32')
    203216        c.norm = a * 2
    204217        decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:])
     
    209222        c = cvec()
    210223        assert_equal( 0., o(c))
    211         a = arange(c.length * 2, 0, -2, dtype=float_type)
     224        a = arange(c.length * 2, 0, -2, dtype='float32')
     225        k = arange(c.length, dtype='float32')
    212226        c.norm = a
    213227        cumsum = .95*sum(a*a)
    214228        i = 0; rollsum = 0
    215229        while rollsum < cumsum:
    216             rollsum += a[i]*a[i]
    217             i+=1
     230          rollsum += a[i]*a[i]
     231          i+=1
    218232        rolloff = i
    219233        assert_equal (rolloff, o(c))
    220234
    221 class 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')
    232235
    233236if __name__ == '__main__':
     237    from unittest import main
    234238    main()
Note: See TracChangeset for help on using the changeset viewer.