[6a50b9e] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
[0b6d23d] | 3 | from unittest import main |
---|
[afceccd] | 4 | from numpy.testing import TestCase, assert_equal |
---|
[143cc43] | 5 | from numpy import array, arange, isnan, isinf |
---|
[6a50b9e] | 6 | from aubio import bintomidi, miditobin, freqtobin, bintofreq, freqtomidi, miditofreq |
---|
[6514bb6] | 7 | from aubio import unwrap2pi |
---|
[143cc43] | 8 | from aubio import fvec |
---|
| 9 | from math import pi |
---|
[6a50b9e] | 10 | |
---|
| 11 | class aubio_mathutils(TestCase): |
---|
| 12 | |
---|
[6514bb6] | 13 | def test_unwrap2pi(self): |
---|
[143cc43] | 14 | unwrap2pi(int(23)) |
---|
| 15 | unwrap2pi(float(23.)) |
---|
[689106e] | 16 | unwrap2pi(int(23.)) |
---|
[143cc43] | 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") |
---|
[0b6d23d] | 27 | unwrap2pi(a) |
---|
[143cc43] | 28 | #print zip(a, b) |
---|
[6514bb6] | 29 | |
---|
[0b6d23d] | 30 | def test_unwrap2pi_fails_on_list(self): |
---|
[a6f9ebf] | 31 | with self.assertRaises((TypeError, NotImplementedError)): |
---|
[0b6d23d] | 32 | unwrap2pi(["23.","24.",25.]) |
---|
[6a50b9e] | 33 | |
---|
[143cc43] | 34 | def test_unwrap2pi_takes_fvec(self): |
---|
| 35 | a = fvec(10) |
---|
| 36 | b = unwrap2pi(a) |
---|
| 37 | #print zip(a, b) |
---|
| 38 | assert ( b > -pi ).all() |
---|
| 39 | assert ( b <= pi ).all() |
---|
[6a50b9e] | 40 | |
---|
[143cc43] | 41 | def test_unwrap2pi_takes_array_of_float(self): |
---|
| 42 | a = arange(-10., 10.).astype("float") |
---|
| 43 | b = unwrap2pi(a) |
---|
| 44 | #print zip(a, b) |
---|
| 45 | assert ( b > -pi ).all() |
---|
| 46 | assert ( b <= pi ).all() |
---|
[6a50b9e] | 47 | |
---|
[143cc43] | 48 | def test_unwrap2pi_takes_array_of_float32(self): |
---|
| 49 | a = arange(-10, 10).astype("float32") |
---|
| 50 | b = unwrap2pi(a) |
---|
| 51 | #print zip(a, b) |
---|
| 52 | assert ( b > -pi ).all() |
---|
| 53 | assert ( b <= pi ).all() |
---|
[6a50b9e] | 54 | |
---|
| 55 | def test_freqtomidi(self): |
---|
[689106e] | 56 | a = array(list(range(-20, 50000, 100)) + [ -1e32, 1e32 ]) |
---|
[143cc43] | 57 | b = freqtomidi(a) |
---|
| 58 | #print zip(a, b) |
---|
| 59 | assert_equal ( isnan(array(b)), False ) |
---|
| 60 | assert_equal ( isinf(array(b)), False ) |
---|
| 61 | assert_equal ( array(b) < 0, False ) |
---|
[6a50b9e] | 62 | |
---|
| 63 | def test_miditofreq(self): |
---|
[689106e] | 64 | a = list(range(-30, 200)) + [-100000, 10000] |
---|
[143cc43] | 65 | b = miditofreq(a) |
---|
| 66 | #print zip(a, b) |
---|
| 67 | assert_equal ( isnan(b), False ) |
---|
| 68 | assert_equal ( isinf(b), False ) |
---|
| 69 | assert_equal ( b < 0, False ) |
---|
| 70 | |
---|
| 71 | def test_miditobin(self): |
---|
[689106e] | 72 | a = list(range(-30, 200)) + [-100000, 10000] |
---|
[ddfa6be] | 73 | b = [ miditobin(x, 44100, 512) for x in a ] |
---|
[143cc43] | 74 | #print zip(a, b) |
---|
| 75 | assert_equal ( isnan(array(b)), False ) |
---|
| 76 | assert_equal ( isinf(array(b)), False ) |
---|
| 77 | assert_equal ( array(b) < 0, False ) |
---|
| 78 | |
---|
| 79 | def test_bintomidi(self): |
---|
[689106e] | 80 | a = list(range(-100, 512)) |
---|
[143cc43] | 81 | b = [ bintomidi(x, 44100, 512) for x in a ] |
---|
| 82 | #print zip(a, b) |
---|
| 83 | assert_equal ( isnan(array(b)), False ) |
---|
| 84 | assert_equal ( isinf(array(b)), False ) |
---|
| 85 | assert_equal ( array(b) < 0, False ) |
---|
| 86 | |
---|
| 87 | def test_freqtobin(self): |
---|
[689106e] | 88 | a = list(range(-20, 50000, 100)) + [ -1e32, 1e32 ] |
---|
[143cc43] | 89 | b = [ freqtobin(x, 44100, 512) for x in a ] |
---|
| 90 | #print zip(a, b) |
---|
| 91 | assert_equal ( isnan(array(b)), False ) |
---|
| 92 | assert_equal ( isinf(array(b)), False ) |
---|
| 93 | assert_equal ( array(b) < 0, False ) |
---|
| 94 | |
---|
| 95 | def test_bintofreq(self): |
---|
[689106e] | 96 | a = list(range(-20, 148)) |
---|
[143cc43] | 97 | b = [ bintofreq(x, 44100, 512) for x in a ] |
---|
| 98 | #print zip(a, b) |
---|
| 99 | assert_equal ( isnan(array(b)), False ) |
---|
| 100 | assert_equal ( isinf(array(b)), False ) |
---|
| 101 | assert_equal ( array(b) < 0, False ) |
---|
[6a50b9e] | 102 | |
---|
| 103 | if __name__ == '__main__': |
---|
| 104 | main() |
---|