[2886984] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | from unittest import main |
---|
| 4 | from numpy.testing import TestCase |
---|
| 5 | from numpy.testing import assert_equal, assert_almost_equal |
---|
[ab8e838] | 6 | from _tools import assert_warns |
---|
[099237f] | 7 | from utils import is32bit |
---|
[2886984] | 8 | import numpy as np |
---|
| 9 | import aubio |
---|
| 10 | |
---|
| 11 | from aubio import hztomel, meltohz |
---|
| 12 | from aubio import hztomel_htk, meltohz_htk |
---|
| 13 | |
---|
| 14 | class aubio_hztomel_test_case(TestCase): |
---|
| 15 | |
---|
| 16 | def test_hztomel(self): |
---|
| 17 | assert_equal(hztomel(0.), 0.) |
---|
| 18 | assert_almost_equal(hztomel(400. / 3.), 2., decimal=5) |
---|
[0d07a2a] | 19 | assert_almost_equal(hztomel(1000. / 3), 5.) |
---|
[099237f] | 20 | # on 32bit, some of these tests fails unless compiling with -ffloat-store |
---|
| 21 | try: |
---|
| 22 | assert_equal(hztomel(200.), 3.) |
---|
| 23 | except AssertionError: |
---|
| 24 | if not is32bit(): raise |
---|
| 25 | assert_almost_equal(hztomel(200.), 3., decimal=5) |
---|
[2886984] | 26 | assert_almost_equal(hztomel(1000.), 15) |
---|
[5039244] | 27 | assert_almost_equal(hztomel(6400), 42, decimal=5) |
---|
| 28 | assert_almost_equal(hztomel(40960), 69, decimal=5) |
---|
[2886984] | 29 | |
---|
| 30 | for m in np.linspace(0, 1000, 100): |
---|
| 31 | assert_almost_equal(hztomel(meltohz(m)) - m, 0, decimal=3) |
---|
| 32 | |
---|
| 33 | def test_meltohz(self): |
---|
| 34 | assert_equal(meltohz(0.), 0.) |
---|
| 35 | assert_almost_equal(meltohz(2), 400. / 3., decimal=4) |
---|
[099237f] | 36 | try: |
---|
| 37 | assert_equal(meltohz(3.), 200.) |
---|
| 38 | except AssertionError: |
---|
| 39 | if not is32bit(): raise |
---|
| 40 | assert_almost_equal(meltohz(3.), 200., decimal=5) |
---|
[2886984] | 41 | assert_almost_equal(meltohz(5), 1000. / 3., decimal=4) |
---|
| 42 | assert_almost_equal(meltohz(15), 1000., decimal=4) |
---|
| 43 | assert_almost_equal(meltohz(42), 6400., decimal=2) |
---|
| 44 | assert_almost_equal(meltohz(69), 40960., decimal=1) |
---|
| 45 | |
---|
| 46 | for f in np.linspace(0, 20000, 1000): |
---|
| 47 | assert_almost_equal(meltohz(hztomel(f)) - f, 0, decimal=1) |
---|
| 48 | |
---|
| 49 | def test_meltohz_negative(self): |
---|
[ab8e838] | 50 | with assert_warns(UserWarning): |
---|
| 51 | assert_equal(meltohz(-1), 0) |
---|
[2886984] | 52 | |
---|
| 53 | def test_hztomel_negative(self): |
---|
[ab8e838] | 54 | with assert_warns(UserWarning): |
---|
| 55 | assert_equal(hztomel(-1), 0) |
---|
[2886984] | 56 | |
---|
| 57 | |
---|
| 58 | class aubio_hztomel_htk_test_case(TestCase): |
---|
| 59 | |
---|
| 60 | def test_meltohz(self): |
---|
| 61 | assert_equal(meltohz(0, htk=True), 0) |
---|
| 62 | assert_almost_equal(meltohz(2595, htk=True), 6300., decimal=1) |
---|
| 63 | |
---|
| 64 | def test_hztomel(self): |
---|
| 65 | assert_equal(hztomel(0, htk=True), 0) |
---|
| 66 | assert_almost_equal(hztomel(3428.7, htk=True), 2000., decimal=1) |
---|
| 67 | assert_almost_equal(hztomel(6300, htk=True), 2595., decimal=1) |
---|
| 68 | |
---|
| 69 | def test_meltohz_negative(self): |
---|
[ab8e838] | 70 | with assert_warns(UserWarning): |
---|
| 71 | assert_equal(meltohz(-1, htk=True), 0) |
---|
[2886984] | 72 | assert_almost_equal(meltohz(2000, htk=True), 3428.7, decimal=1) |
---|
| 73 | assert_almost_equal(meltohz(1000, htk=True), 1000., decimal=1) |
---|
| 74 | |
---|
| 75 | def test_hztomel_negative(self): |
---|
[ab8e838] | 76 | with assert_warns(UserWarning): |
---|
| 77 | assert_equal(meltohz(-1, htk=True), 0) |
---|
| 78 | with assert_warns(UserWarning): |
---|
| 79 | assert_equal(hztomel(-1, htk=True), 0) |
---|
[2886984] | 80 | assert_almost_equal(hztomel(1000, htk=True), 1000., decimal=1) |
---|
| 81 | |
---|
| 82 | def test_hztomel_htk(self): |
---|
| 83 | for f in np.linspace(0, 20000, 1000): |
---|
| 84 | assert_almost_equal(meltohz_htk(hztomel_htk(f)) - f, 0, decimal=1) |
---|
| 85 | for f in np.linspace(0, 20000, 1000): |
---|
| 86 | assert_almost_equal(hztomel_htk(meltohz_htk(f)) - f, 0, decimal=1) |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | class aubio_hztomel_wrong_values(TestCase): |
---|
| 90 | """ more tests to cover all branches """ |
---|
| 91 | |
---|
| 92 | def test_hztomel_wrong_values(self): |
---|
| 93 | with self.assertRaises(TypeError): |
---|
| 94 | hztomel('s') |
---|
| 95 | |
---|
| 96 | def test_meltohz_wrong_values(self): |
---|
| 97 | with self.assertRaises(TypeError): |
---|
| 98 | meltohz(bytes('ad')) |
---|
| 99 | |
---|
| 100 | def test_meltohz_no_arg(self): |
---|
| 101 | with self.assertRaises(TypeError): |
---|
| 102 | meltohz() |
---|
| 103 | |
---|
| 104 | def test_meltohz_htk_no_arg(self): |
---|
| 105 | with self.assertRaises(TypeError): |
---|
| 106 | meltohz_htk() |
---|
| 107 | |
---|
| 108 | def test_hztomel_htk_wrong_values(self): |
---|
| 109 | with self.assertRaises(TypeError): |
---|
| 110 | hztomel_htk('0') |
---|
| 111 | |
---|
| 112 | def test_hztomel_htk_false(self): |
---|
| 113 | assert hztomel(120, htk=False) == hztomel(120) |
---|
| 114 | |
---|
| 115 | def test_meltohz_htk_false(self): |
---|
| 116 | assert meltohz(12, htk=False) == meltohz(12) |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | if __name__ == '__main__': |
---|
| 120 | from unittest import main |
---|
| 121 | main() |
---|