source: python/tests/test_hztomel.py @ 0a8f63f

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since 0a8f63f was 0d07a2a, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[test] pass checks in double precision, rename hztomel

  • Property mode set to 100755
File size: 3.5 KB
Line 
1#! /usr/bin/env python
2
3from unittest import main
4from numpy.testing import TestCase
5from numpy.testing import assert_equal, assert_almost_equal
6import numpy as np
7import aubio
8
9from aubio import hztomel, meltohz
10from aubio import hztomel_htk, meltohz_htk
11
12
13class aubio_hztomel_test_case(TestCase):
14
15    def test_hztomel(self):
16        assert_equal(hztomel(0.), 0.)
17        assert_almost_equal(hztomel(400. / 3.), 2., decimal=5)
18        assert_almost_equal(hztomel(1000. / 3), 5.)
19        assert_equal(hztomel(200.), 3.)
20        assert_almost_equal(hztomel(1000.), 15)
21        assert_almost_equal(hztomel(6400), 42)
22        assert_almost_equal(hztomel(40960), 69)
23
24        for m in np.linspace(0, 1000, 100):
25            assert_almost_equal(hztomel(meltohz(m)) - m, 0, decimal=3)
26
27    def test_meltohz(self):
28        assert_equal(meltohz(0.), 0.)
29        assert_almost_equal(meltohz(2), 400. / 3., decimal=4)
30        assert_equal(meltohz(3.), 200.)
31        assert_almost_equal(meltohz(5), 1000. / 3., decimal=4)
32        assert_almost_equal(meltohz(15), 1000., decimal=4)
33        assert_almost_equal(meltohz(42), 6400., decimal=2)
34        assert_almost_equal(meltohz(69), 40960., decimal=1)
35
36        for f in np.linspace(0, 20000, 1000):
37            assert_almost_equal(meltohz(hztomel(f)) - f, 0, decimal=1)
38
39    def test_meltohz_negative(self):
40        # TODO add assert_warns
41        assert_equal(meltohz(-1), 0)
42
43    def test_hztomel_negative(self):
44        # TODO add assert_warns
45        assert_equal(hztomel(-1), 0)
46
47
48class aubio_hztomel_htk_test_case(TestCase):
49
50    def test_meltohz(self):
51        assert_equal(meltohz(0, htk=True), 0)
52        assert_almost_equal(meltohz(2595, htk=True), 6300., decimal=1)
53
54    def test_hztomel(self):
55        assert_equal(hztomel(0, htk=True), 0)
56        assert_almost_equal(hztomel(3428.7, htk=True), 2000., decimal=1)
57        assert_almost_equal(hztomel(6300, htk=True), 2595., decimal=1)
58
59    def test_meltohz_negative(self):
60        # TODO add assert_warns
61        assert_equal(meltohz(-1, htk=True), 0)
62        assert_almost_equal(meltohz(2000, htk=True), 3428.7, decimal=1)
63        assert_almost_equal(meltohz(1000, htk=True), 1000., decimal=1)
64
65    def test_hztomel_negative(self):
66        # TODO add assert_warns
67        assert_equal(hztomel(-1, htk=True), 0)
68        assert_almost_equal(hztomel(1000, htk=True), 1000., decimal=1)
69
70    def test_hztomel_htk(self):
71        for f in np.linspace(0, 20000, 1000):
72            assert_almost_equal(meltohz_htk(hztomel_htk(f)) - f, 0, decimal=1)
73        for f in np.linspace(0, 20000, 1000):
74            assert_almost_equal(hztomel_htk(meltohz_htk(f)) - f, 0, decimal=1)
75
76
77class aubio_hztomel_wrong_values(TestCase):
78    """ more tests to cover all branches """
79
80    def test_hztomel_wrong_values(self):
81        with self.assertRaises(TypeError):
82            hztomel('s')
83
84    def test_meltohz_wrong_values(self):
85        with self.assertRaises(TypeError):
86            meltohz(bytes('ad'))
87
88    def test_meltohz_no_arg(self):
89        with self.assertRaises(TypeError):
90            meltohz()
91
92    def test_meltohz_htk_no_arg(self):
93        with self.assertRaises(TypeError):
94            meltohz_htk()
95
96    def test_hztomel_htk_wrong_values(self):
97        with self.assertRaises(TypeError):
98            hztomel_htk('0')
99
100    def test_hztomel_htk_false(self):
101        assert hztomel(120, htk=False) == hztomel(120)
102
103    def test_meltohz_htk_false(self):
104        assert meltohz(12, htk=False) == meltohz(12)
105
106
107if __name__ == '__main__':
108    from unittest import main
109    main()
Note: See TracBrowser for help on using the repository browser.