Changeset 312826c


Ignore:
Timestamp:
Jul 11, 2012, 10:43:39 PM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/constantq, feature/pitchshift, feature/pydocstrings, feature/timestretch, master, pitchshift, sampler, timestretch, yinfft+
Children:
83b385f
Parents:
abbd910
Message:

test_onset.py, test_onsetdetection.py: split and update

Location:
interfaces/python
Files:
1 added
1 moved

Legend:

Unmodified
Added
Removed
  • interfaces/python/test_specdesc.py

    • Property mode changed from 100644 to 100755
    rabbd910 r312826c  
    1 from numpy.testing import TestCase, run_module_suite
    2 from numpy.testing import assert_equal, assert_almost_equal
    3 # WARNING: numpy also has an fft object
     1#! /usr/bin/python
     2
     3from numpy.testing import TestCase, assert_equal, assert_almost_equal
     4from numpy import random, arange, log, zeros
    45from aubio import specdesc, cvec
    5 from numpy import array, shape, arange, zeros, log
    66from math import pi
     7
     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
    725
    826class aubio_specdesc(TestCase):
     
    1028    def test_members(self):
    1129        o = specdesc()
    12         assert_equal ([o.buf_size, o.method],
    13             [1024, "default"])
    14 
     30
     31        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)))
     57
     58    def test_phase(self):
     59        o = specdesc("phase", buf_size)
     60        spec = cvec(buf_size)
     61        # phase of zeros is zero
     62        assert_equal (o(spec), 0.)
     63        spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
     64        # phase of random is not zero
     65        spec.norm[:] = 1
     66        assert (o(spec) != 0.)
     67
     68    def test_specdiff(self):
     69        o = specdesc("phase", buf_size)
     70        spec = cvec(buf_size)
     71        # specdiff of zeros is zero
     72        assert_equal (o(spec), 0.)
     73        spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
     74        # phase of random is not zero
     75        spec.norm[:] = 1
     76        assert (o(spec) != 0.)
     77   
    1578    def test_hfc(self):
    1679        o = specdesc("hfc")
     
    3598        assert_equal ( 0, o(c))
    3699
    37     def test_phase(self):
    38         o = specdesc("phase")
    39         c = cvec()
    40         assert_equal( 0., o(c))
    41 
    42100    def test_kl(self):
    43101        o = specdesc("kl")
     
    82140    def test_spread(self):
    83141        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)
     142        c = cvec(2048)
     143        ramp = arange(c.length, dtype='float32')
     144        assert_equal( 0., o(c))
     145
     146        a = ramp
     147        c.norm = a
     148        centroid = sum(a*a) / sum(a)
     149        spread = sum( a * pow(ramp - centroid, 2.) ) / sum(a)
     150        assert_almost_equal (o(c), spread, decimal = 1)
    94151
    95152    def test_skewness(self):
     
    176233        assert_equal (rolloff, o(c))
    177234
     235
    178236if __name__ == '__main__':
    179237    from unittest import main
Note: See TracChangeset for help on using the changeset viewer.