Showing the changes to python/tests/test_mathutils.py (1 of 1 file). Show all
Unified · Split
| @@ -2,34 +2,104 @@ · 1 unmodified line | |||
| 2 | 2 | ||
| 3 | from numpy.testing import TestCase, run_module_suite | 3 | from numpy.testing import TestCase, run_module_suite |
| 4 | from numpy.testing import assert_equal, assert_almost_equal | 4 | from numpy.testing import assert_equal, assert_almost_equal |
| 5 | from numpy import array, arange, isnan, isinf | ||
| 5 | from aubio import bintomidi, miditobin, freqtobin, bintofreq, freqtomidi, miditofreq | 6 | from aubio import bintomidi, miditobin, freqtobin, bintofreq, freqtomidi, miditofreq |
| 6 | from aubio import unwrap2pi | 7 | from aubio import unwrap2pi |
| 8 | from aubio import fvec | ||
| 9 | from math import pi | ||
| 7 | 10 | ||
| 8 | class aubio_mathutils(TestCase): | 11 | class aubio_mathutils(TestCase): |
| 9 | 12 | ||
| 10 | def test_unwrap2pi(self): | 13 | def test_unwrap2pi(self): |
| 11 | a = [ x/100. for x in range(-600,600,100) ] | 14 | unwrap2pi(int(23)) |
| 12 | b = [ unwrap2pi(x) for x in a ] | 15 | unwrap2pi(float(23.)) |
| 13 | #print b | 16 | unwrap2pi(long(23.)) |
| 17 | unwrap2pi(arange(10)) | ||
| 18 | unwrap2pi(arange(10).astype("int")) | ||
| 19 | unwrap2pi(arange(10).astype("float")) | ||
| 20 | unwrap2pi(arange(10).astype("float32")) | ||
| 21 | unwrap2pi([1,3,5]) | ||
| 22 | unwrap2pi([23.,24.,25.]) | ||
| 23 | a = fvec(10) | ||
| 24 | a[:] = 4. | ||
| 25 | unwrap2pi(a) | ||
| 26 | a = pi/100. * arange(-600,600).astype("float") | ||
| 27 | b = unwrap2pi (a) | ||
| 28 | #print zip(a, b) | ||
| 14 | 29 | ||
| 15 | def test_miditobin(self): | 30 | try: |
| 16 | a = [ miditobin(a, 44100, 512) for a in range(128) ] | 31 | print unwrap2pi(["23.","24.",25.]) |
| 32 | except TypeError: | ||
| 33 | pass | ||
| 17 | 34 | ||
| 18 | def test_bintomidi(self): | 35 | def test_unwrap2pi_takes_fvec(self): |
| 19 | a = [ bintomidi(a, 44100, 512) for a in range(128) ] | 36 | a = fvec(10) |
| 37 | b = unwrap2pi(a) | ||
| 38 | #print zip(a, b) | ||
| 39 | assert ( b > -pi ).all() | ||
| 40 | assert ( b <= pi ).all() | ||
| 20 | 41 | ||
| 21 | def test_freqtobin(self): | 42 | def test_unwrap2pi_takes_array_of_float(self): |
| 22 | a = [ freqtobin(a, 44100, 512) for a in range(128) ] | 43 | a = arange(-10., 10.).astype("float") |
| 44 | b = unwrap2pi(a) | ||
| 45 | #print zip(a, b) | ||
| 46 | assert ( b > -pi ).all() | ||
| 47 | assert ( b <= pi ).all() | ||
| 23 | 48 | ||
| 24 | def test_bintofreq(self): | 49 | def test_unwrap2pi_takes_array_of_float32(self): |
| 25 | a = [ bintofreq(a, 44100, 512) for a in range(128) ] | 50 | a = arange(-10, 10).astype("float32") |
| 51 | b = unwrap2pi(a) | ||
| 52 | #print zip(a, b) | ||
| 53 | assert ( b > -pi ).all() | ||
| 54 | assert ( b <= pi ).all() | ||
| 26 | 55 | ||
| 27 | def test_freqtomidi(self): | 56 | def test_freqtomidi(self): |
| 28 | a = [ freqtomidi(a) for a in range(128) ] | 57 | a = array(range(-20, 50000, 100) + [ -1e32, 1e32 ]) |
| 58 | b = freqtomidi(a) | ||
| 59 | #print zip(a, b) | ||
| 60 | assert_equal ( isnan(array(b)), False ) | ||
| 61 | assert_equal ( isinf(array(b)), False ) | ||
| 62 | assert_equal ( array(b) < 0, False ) | ||
| 29 | 63 | ||
| 30 | def test_miditofreq(self): | 64 | def test_miditofreq(self): |
| 31 | freqs = [ miditofreq(a) for a in range(128) ] | 65 | a = range(-30, 200) + [-100000, 10000] |
| 32 | midis = [ freqtomidi(a) for a in freqs ] | 66 | b = miditofreq(a) |
| 67 | #print zip(a, b) | ||
| 68 | assert_equal ( isnan(b), False ) | ||
| 69 | assert_equal ( isinf(b), False ) | ||
| 70 | assert_equal ( b < 0, False ) | ||
| 71 | |||
| 72 | def test_miditobin(self): | ||
| 73 | a = range(-30, 200) + [-100000, 10000] | ||
| 74 | b = [ bintomidi(x, 44100, 512) for x in a ] | ||
| 75 | #print zip(a, b) | ||
| 76 | assert_equal ( isnan(array(b)), False ) | ||
| 77 | assert_equal ( isinf(array(b)), False ) | ||
| 78 | assert_equal ( array(b) < 0, False ) | ||
| 79 | |||
| 80 | def test_bintomidi(self): | ||
| 81 | a = range(-100, 512) | ||
| 82 | b = [ bintomidi(x, 44100, 512) for x in a ] | ||
| 83 | #print zip(a, b) | ||
| 84 | assert_equal ( isnan(array(b)), False ) | ||
| 85 | assert_equal ( isinf(array(b)), False ) | ||
| 86 | assert_equal ( array(b) < 0, False ) | ||
| 87 | |||
| 88 | def test_freqtobin(self): | ||
| 89 | a = range(-20, 50000, 100) + [ -1e32, 1e32 ] | ||
| 90 | b = [ freqtobin(x, 44100, 512) for x in a ] | ||
| 91 | #print zip(a, b) | ||
| 92 | assert_equal ( isnan(array(b)), False ) | ||
| 93 | assert_equal ( isinf(array(b)), False ) | ||
| 94 | assert_equal ( array(b) < 0, False ) | ||
| 95 | |||
| 96 | def test_bintofreq(self): | ||
| 97 | a = range(-20, 148) | ||
| 98 | b = [ bintofreq(x, 44100, 512) for x in a ] | ||
| 99 | #print zip(a, b) | ||
| 100 | assert_equal ( isnan(array(b)), False ) | ||
| 101 | assert_equal ( isinf(array(b)), False ) | ||
| 102 | assert_equal ( array(b) < 0, False ) | ||
| 33 | 103 | ||
| 34 | if __name__ == '__main__': | 104 | if __name__ == '__main__': |
| 35 | from unittest import main | 105 | from unittest import main |