Showing the changes to python/tests (13 of 21 files). Show all
Unified · Split
| @@ -0,0 +1,14 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from numpy.testing import TestCase, run_module_suite | ||
| 4 | |||
| 5 | class aubiomodule_test_case(TestCase): | ||
| 6 | |||
| 7 | def test_import(self): | ||
| 8 | """ try importing aubio """ | ||
| 9 | import aubio | ||
| 10 | |||
| 11 | if __name__ == '__main__': | ||
| 12 | from unittest import main | ||
| 13 | main() | ||
| 14 | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | from numpy.testing import TestCase, run_module_suite | ||
| 2 | from numpy.testing import assert_equal, assert_almost_equal | ||
| 3 | from aubio import cvec | ||
| 4 | from numpy import array, shape, pi | ||
| 5 | |||
| 6 | class aubio_cvec_test_case(TestCase): | ||
| 7 | |||
| 8 | def test_vector_created_with_zeroes(self): | ||
| 9 | a = cvec(10) | ||
| 10 | a | ||
| 11 | shape(a.norm) | ||
| 12 | shape(a.phas) | ||
| 13 | a.norm[0] | ||
| 14 | assert_equal(a.norm, 0.) | ||
| 15 | assert_equal(a.phas, 0.) | ||
| 16 | |||
| 17 | def test_vector_assign_element(self): | ||
| 18 | a = cvec() | ||
| 19 | a.norm[0] = 1 | ||
| 20 | assert_equal(a.norm[0], 1) | ||
| 21 | a.phas[0] = 1 | ||
| 22 | assert_equal(a.phas[0], 1) | ||
| 23 | |||
| 24 | def test_vector_assign_element_end(self): | ||
| 25 | a = cvec() | ||
| 26 | a.norm[-1] = 1 | ||
| 27 | assert_equal(a.norm[-1], 1) | ||
| 28 | assert_equal(a.norm[len(a.norm)-1], 1) | ||
| 29 | a.phas[-1] = 1 | ||
| 30 | assert_equal(a.phas[-1], 1) | ||
| 31 | assert_equal(a.phas[len(a.phas)-1], 1) | ||
| 32 | |||
| 33 | def test_assign_cvec_norm_slice(self): | ||
| 34 | spec = cvec(1024) | ||
| 35 | spec.norm[40:100] = 100 | ||
| 36 | assert_equal (spec.norm[0:40], 0) | ||
| 37 | assert_equal (spec.norm[40:100], 100) | ||
| 38 | assert_equal (spec.norm[100:-1], 0) | ||
| 39 | assert_equal (spec.phas, 0) | ||
| 40 | |||
| 41 | def test_assign_cvec_phas_slice(self): | ||
| 42 | spec = cvec(1024) | ||
| 43 | spec.phas[39:-1] = -pi | ||
| 44 | assert_equal (spec.phas[0:39], 0) | ||
| 45 | assert_equal (spec.phas[39:-1], -pi) | ||
| 46 | assert_equal (spec.norm, 0) | ||
| 47 | |||
| 48 | if __name__ == '__main__': | ||
| 49 | from unittest import main | ||
| 50 | main() | ||
| @@ -0,0 +1,113 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from numpy.testing import TestCase, run_module_suite | ||
| 4 | from numpy.testing import assert_equal, assert_almost_equal | ||
| 5 | # WARNING: numpy also has an fft object | ||
| 6 | from aubio import fvec, fft, cvec | ||
| 7 | from numpy import array, shape | ||
| 8 | from math import pi | ||
| 9 | |||
| 10 | class aubio_fft_test_case(TestCase): | ||
| 11 | |||
| 12 | def test_members(self): | ||
| 13 | f = fft() | ||
| 14 | assert_equal (f.win_s, 1024) | ||
| 15 | |||
| 16 | def test_output_dimensions(self): | ||
| 17 | """ check the dimensions of output """ | ||
| 18 | win_s = 1024 | ||
| 19 | timegrain = fvec(win_s) | ||
| 20 | f = fft(win_s) | ||
| 21 | fftgrain = f (timegrain) | ||
| 22 | assert_equal (fftgrain.norm, 0) | ||
| 23 | assert_equal (shape(fftgrain.norm), (win_s/2+1,)) | ||
| 24 | assert_equal (fftgrain.phas, 0) | ||
| 25 | assert_equal (shape(fftgrain.phas), (win_s/2+1,)) | ||
| 26 | |||
| 27 | def test_zeros(self): | ||
| 28 | """ check the transform of zeros """ | ||
| 29 | win_s = 512 | ||
| 30 | timegrain = fvec(win_s) | ||
| 31 | f = fft(win_s) | ||
| 32 | fftgrain = f(timegrain) | ||
| 33 | assert_equal ( fftgrain.norm == 0, True ) | ||
| 34 | assert_equal ( fftgrain.phas == 0, True ) | ||
| 35 | |||
| 36 | def test_impulse(self): | ||
| 37 | """ check the transform of one impulse at a random place """ | ||
| 38 | from random import random | ||
| 39 | from math import floor | ||
| 40 | win_s = 256 | ||
| 41 | i = floor(random()*win_s) | ||
| 42 | impulse = pi * random() | ||
| 43 | f = fft(win_s) | ||
| 44 | timegrain = fvec(win_s) | ||
| 45 | timegrain[i] = impulse | ||
| 46 | fftgrain = f ( timegrain ) | ||
| 47 | #self.plot_this ( fftgrain.phas ) | ||
| 48 | assert_almost_equal ( fftgrain.norm, impulse, decimal = 6 ) | ||
| 49 | assert_equal ( fftgrain.phas <= pi, True) | ||
| 50 | assert_equal ( fftgrain.phas >= -pi, True) | ||
| 51 | |||
| 52 | def test_impulse_negative(self): | ||
| 53 | """ check the transform of one impulse at a random place """ | ||
| 54 | from random import random | ||
| 55 | from math import floor | ||
| 56 | win_s = 256 | ||
| 57 | i = 0 | ||
| 58 | impulse = -10. | ||
| 59 | f = fft(win_s) | ||
| 60 | timegrain = fvec(win_s) | ||
| 61 | timegrain[i] = impulse | ||
| 62 | fftgrain = f ( timegrain ) | ||
| 63 | #self.plot_this ( fftgrain.phas ) | ||
| 64 | assert_almost_equal ( fftgrain.norm, abs(impulse), decimal = 6 ) | ||
| 65 | if impulse < 0: | ||
| 66 | # phase can be pi or -pi, as it is not unwrapped | ||
| 67 | assert_almost_equal ( abs(fftgrain.phas[1:-1]) , pi, decimal = 6 ) | ||
| 68 | assert_almost_equal ( fftgrain.phas[0], pi, decimal = 6) | ||
| 69 | assert_almost_equal ( fftgrain.phas[-1], pi, decimal = 6) | ||
| 70 | else: | ||
| 71 | assert_equal ( fftgrain.phas[1:-1] == 0, True) | ||
| 72 | assert_equal ( fftgrain.phas[0] == 0, True) | ||
| 73 | assert_equal ( fftgrain.phas[-1] == 0, True) | ||
| 74 | # now check the resynthesis | ||
| 75 | synthgrain = f.rdo ( fftgrain ) | ||
| 76 | #self.plot_this ( fftgrain.phas.T ) | ||
| 77 | assert_equal ( fftgrain.phas <= pi, True) | ||
| 78 | assert_equal ( fftgrain.phas >= -pi, True) | ||
| 79 | #self.plot_this ( synthgrain - timegrain ) | ||
| 80 | assert_almost_equal ( synthgrain, timegrain, decimal = 6 ) | ||
| 81 | |||
| 82 | def test_impulse_at_zero(self): | ||
| 83 | """ check the transform of one impulse at a index 0 """ | ||
| 84 | win_s = 1024 | ||
| 85 | impulse = pi | ||
| 86 | f = fft(win_s) | ||
| 87 | timegrain = fvec(win_s) | ||
| 88 | timegrain[0] = impulse | ||
| 89 | fftgrain = f ( timegrain ) | ||
| 90 | #self.plot_this ( fftgrain.phas ) | ||
| 91 | assert_equal ( fftgrain.phas[0], 0) | ||
| 92 | # could be 0 or -0 depending on fft implementation (0 for fftw3, -0 for ooura) | ||
| 93 | assert_almost_equal ( fftgrain.phas[1], 0) | ||
| 94 | assert_almost_equal ( fftgrain.norm[0], impulse, decimal = 6 ) | ||
| 95 | |||
| 96 | def test_rdo_before_do(self): | ||
| 97 | """ check running fft.rdo before fft.do works """ | ||
| 98 | win_s = 1024 | ||
| 99 | impulse = pi | ||
| 100 | f = fft(win_s) | ||
| 101 | fftgrain = cvec(win_s) | ||
| 102 | t = f.rdo( fftgrain ) | ||
| 103 | assert_equal ( t, 0 ) | ||
| 104 | |||
| 105 | def plot_this(self, this): | ||
| 106 | from pylab import plot, show | ||
| 107 | plot ( this ) | ||
| 108 | show () | ||
| 109 | |||
| 110 | if __name__ == '__main__': | ||
| 111 | from unittest import main | ||
| 112 | main() | ||
| 113 | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from numpy.testing import TestCase, assert_equal, assert_almost_equal | ||
| 4 | from aubio import fvec, digital_filter | ||
| 5 | from numpy import array | ||
| 6 | |||
| 7 | def array_from_text_file(filename, dtype = 'float'): | ||
| 8 | return array([line.split() for line in open(filename).readlines()], | ||
| 9 | dtype = dtype) | ||
| 10 | |||
| 11 | class aubio_filter_test_case(TestCase): | ||
| 12 | |||
| 13 | def test_members(self): | ||
| 14 | f = digital_filter() | ||
| 15 | assert_equal (f.order, 7) | ||
| 16 | f = digital_filter(5) | ||
| 17 | assert_equal (f.order, 5) | ||
| 18 | f(fvec()) | ||
| 19 | |||
| 20 | def test_cweighting_error(self): | ||
| 21 | f = digital_filter (2) | ||
| 22 | self.assertRaises ( ValueError, f.set_c_weighting, 44100 ) | ||
| 23 | f = digital_filter (8) | ||
| 24 | self.assertRaises ( ValueError, f.set_c_weighting, 44100 ) | ||
| 25 | f = digital_filter (5) | ||
| 26 | self.assertRaises ( ValueError, f.set_c_weighting, 4000 ) | ||
| 27 | f = digital_filter (5) | ||
| 28 | self.assertRaises ( ValueError, f.set_c_weighting, 193000 ) | ||
| 29 | f = digital_filter (7) | ||
| 30 | self.assertRaises ( ValueError, f.set_a_weighting, 193000 ) | ||
| 31 | f = digital_filter (5) | ||
| 32 | self.assertRaises ( ValueError, f.set_a_weighting, 192000 ) | ||
| 33 | |||
| 34 | def test_c_weighting(self): | ||
| 35 | expected = array_from_text_file('c_weighting_test_simple.expected') | ||
| 36 | f = digital_filter(5) | ||
| 37 | f.set_c_weighting(44100) | ||
| 38 | v = fvec(32) | ||
| 39 | v[12] = .5 | ||
| 40 | u = f(v) | ||
| 41 | assert_almost_equal (expected[1], u) | ||
| 42 | |||
| 43 | def test_a_weighting(self): | ||
| 44 | expected = array_from_text_file('a_weighting_test_simple.expected') | ||
| 45 | f = digital_filter(7) | ||
| 46 | f.set_a_weighting(44100) | ||
| 47 | v = fvec(32) | ||
| 48 | v[12] = .5 | ||
| 49 | u = f(v) | ||
| 50 | assert_almost_equal (expected[1], u) | ||
| 51 | |||
| 52 | def test_a_weighting_parted(self): | ||
| 53 | expected = array_from_text_file('a_weighting_test_simple.expected') | ||
| 54 | f = digital_filter(7) | ||
| 55 | f.set_a_weighting(44100) | ||
| 56 | v = fvec(16) | ||
| 57 | v[12] = .5 | ||
| 58 | u = f(v) | ||
| 59 | assert_almost_equal (expected[1][:16], u) | ||
| 60 | # one more time | ||
| 61 | v = fvec(16) | ||
| 62 | u = f(v) | ||
| 63 | assert_almost_equal (expected[1][16:], u) | ||
| 64 | |||
| 65 | if __name__ == '__main__': | ||
| 66 | from unittest import main | ||
| 67 | main() | ||
| 68 | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from numpy.testing import TestCase, run_module_suite | ||
| 4 | from numpy.testing import assert_equal, assert_almost_equal | ||
| 5 | from numpy import random | ||
| 6 | from aubio import cvec, filterbank | ||
| 7 | |||
| 8 | class aubio_filterbank_test_case(TestCase): | ||
| 9 | |||
| 10 | def test_members(self): | ||
| 11 | f = filterbank(40, 512) | ||
| 12 | assert_equal ([f.n_filters, f.win_s], [40, 512]) | ||
| 13 | |||
| 14 | def test_set_coeffs(self): | ||
| 15 | f = filterbank(40, 512) | ||
| 16 | r = random.random([40, 512 / 2 + 1]).astype('float32') | ||
| 17 | f.set_coeffs(r) | ||
| 18 | assert_equal (r, f.get_coeffs()) | ||
| 19 | |||
| 20 | if __name__ == '__main__': | ||
| 21 | from unittest import main | ||
| 22 | main() | ||
| 23 | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from numpy.testing import TestCase, run_module_suite | ||
| 4 | from numpy.testing import assert_equal, assert_almost_equal | ||
| 5 | from numpy import array, shape | ||
| 6 | from aubio import cvec, filterbank | ||
| 7 | |||
| 8 | class aubio_filterbank_mel_test_case(TestCase): | ||
| 9 | |||
| 10 | def test_slaney(self): | ||
| 11 | f = filterbank(40, 512) | ||
| 12 | f.set_mel_coeffs_slaney(16000) | ||
| 13 | a = f.get_coeffs() | ||
| 14 | assert_equal(shape (a), (40, 512/2 + 1) ) | ||
| 15 | |||
| 16 | def test_other_slaney(self): | ||
| 17 | f = filterbank(40, 512*2) | ||
| 18 | f.set_mel_coeffs_slaney(44100) | ||
| 19 | a = f.get_coeffs() | ||
| 20 | #print "sum is", sum(sum(a)) | ||
| 21 | for win_s in [256, 512, 1024, 2048, 4096]: | ||
| 22 | f = filterbank(40, win_s) | ||
| 23 | f.set_mel_coeffs_slaney(320000) | ||
| 24 | a = f.get_coeffs() | ||
| 25 | #print "sum is", sum(sum(a)) | ||
| 26 | |||
| 27 | def test_triangle_freqs_zeros(self): | ||
| 28 | f = filterbank(9, 1024) | ||
| 29 | freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] | ||
| 30 | freqs = array(freq_list, dtype = 'float32') | ||
| 31 | f.set_triangle_bands(freqs, 48000) | ||
| 32 | f.get_coeffs().T | ||
| 33 | assert_equal ( f(cvec(1024)), 0) | ||
| 34 | |||
| 35 | def test_triangle_freqs_ones(self): | ||
| 36 | f = filterbank(9, 1024) | ||
| 37 | freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] | ||
| 38 | freqs = array(freq_list, dtype = 'float32') | ||
| 39 | f.set_triangle_bands(freqs, 48000) | ||
| 40 | f.get_coeffs().T | ||
| 41 | spec = cvec(1024) | ||
| 42 | spec.norm[:] = 1 | ||
| 43 | assert_almost_equal ( f(spec), | ||
| 44 | [ 0.02070313, 0.02138672, 0.02127604, 0.02135417, | ||
| 45 | 0.02133301, 0.02133301, 0.02133311, 0.02133334, 0.02133345]) | ||
| 46 | |||
| 47 | if __name__ == '__main__': | ||
| 48 | from unittest import main | ||
| 49 | main() | ||
| 50 | |||
| 51 | |||
| @@ -0,0 +1,135 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from numpy.testing import TestCase, run_module_suite | ||
| 4 | from numpy.testing import assert_equal, assert_almost_equal | ||
| 5 | from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal | ||
| 6 | from numpy import array, shape | ||
| 7 | |||
| 8 | class aubio_fvec_test_case(TestCase): | ||
| 9 | |||
| 10 | def test_vector_created_with_zeroes(self): | ||
| 11 | a = fvec(10) | ||
| 12 | a | ||
| 13 | shape(a) | ||
| 14 | a[0] | ||
| 15 | #del a | ||
| 16 | assert_equal(array(a), 0.) | ||
| 17 | |||
| 18 | def test_vector_create_with_list(self): | ||
| 19 | a = fvec([0,1,2,3]) | ||
| 20 | assert_equal (range(4), a) | ||
| 21 | |||
| 22 | def test_vector_assign_element(self): | ||
| 23 | a = fvec() | ||
| 24 | a[0] = 1 | ||
| 25 | assert_equal(a[0], 1) | ||
| 26 | |||
| 27 | def test_vector_assign_element_end(self): | ||
| 28 | a = fvec() | ||
| 29 | a[-1] = 1 | ||
| 30 | assert_equal(a[-1], 1) | ||
| 31 | assert_equal(a[len(a)-1], 1) | ||
| 32 | |||
| 33 | def test_vector(self): | ||
| 34 | a = fvec() | ||
| 35 | a, len(a) #a.length | ||
| 36 | a[0] | ||
| 37 | array(a) | ||
| 38 | a = fvec(10) | ||
| 39 | a = fvec(1) | ||
| 40 | a.T | ||
| 41 | array(a).T | ||
| 42 | a = range(len(a)) | ||
| 43 | |||
| 44 | def test_wrong_values(self): | ||
| 45 | self.assertRaises (ValueError, fvec, -10) | ||
| 46 | |||
| 47 | a = fvec(2) | ||
| 48 | self.assertRaises (IndexError, a.__getitem__, 3) | ||
| 49 | self.assertRaises (IndexError, a.__getitem__, 2) | ||
| 50 | |||
| 51 | def test_alpha_norm_of_fvec(self): | ||
| 52 | a = fvec(2) | ||
| 53 | self.assertEquals (alpha_norm(a, 1), 0) | ||
| 54 | a[0] = 1 | ||
| 55 | self.assertEquals (alpha_norm(a, 1), 0.5) | ||
| 56 | a[1] = 1 | ||
| 57 | self.assertEquals (alpha_norm(a, 1), 1) | ||
| 58 | a = array([0, 1], dtype='float32') | ||
| 59 | from math import sqrt | ||
| 60 | assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.) | ||
| 61 | |||
| 62 | def test_alpha_norm_of_none(self): | ||
| 63 | self.assertRaises (ValueError, alpha_norm, None, 1) | ||
| 64 | |||
| 65 | def test_alpha_norm_of_array_of_float32(self): | ||
| 66 | # check scalar fails | ||
| 67 | a = array(1, dtype = 'float32') | ||
| 68 | self.assertRaises (ValueError, alpha_norm, a, 1) | ||
| 69 | # check 2d array fails | ||
| 70 | a = array([[2],[4]], dtype = 'float32') | ||
| 71 | self.assertRaises (ValueError, alpha_norm, a, 1) | ||
| 72 | # check 1d array | ||
| 73 | a = array(range(10), dtype = 'float32') | ||
| 74 | self.assertEquals (alpha_norm(a, 1), 4.5) | ||
| 75 | |||
| 76 | def test_alpha_norm_of_array_of_int(self): | ||
| 77 | a = array(1, dtype = 'int') | ||
| 78 | self.assertRaises (ValueError, alpha_norm, a, 1) | ||
| 79 | a = array([[[1,2],[3,4]]], dtype = 'int') | ||
| 80 | self.assertRaises (ValueError, alpha_norm, a, 1) | ||
| 81 | a = array(range(10), dtype = 'int') | ||
| 82 | self.assertRaises (ValueError, alpha_norm, a, 1) | ||
| 83 | |||
| 84 | def test_alpha_norm_of_array_of_string (self): | ||
| 85 | a = "hello" | ||
| 86 | self.assertRaises (ValueError, alpha_norm, a, 1) | ||
| 87 | |||
| 88 | def test_zero_crossing_rate(self): | ||
| 89 | a = array([0,1,-1], dtype='float32') | ||
| 90 | assert_almost_equal (zero_crossing_rate(a), 1./3. ) | ||
| 91 | a = array([0.]*100, dtype='float32') | ||
| 92 | self.assertEquals (zero_crossing_rate(a), 0 ) | ||
| 93 | a = array([-1.]*100, dtype='float32') | ||
| 94 | self.assertEquals (zero_crossing_rate(a), 0 ) | ||
| 95 | a = array([1.]*100, dtype='float32') | ||
| 96 | self.assertEquals (zero_crossing_rate(a), 0 ) | ||
| 97 | |||
| 98 | def test_alpha_norm_of_array_of_float64(self): | ||
| 99 | # check scalar fail | ||
| 100 | a = array(1, dtype = 'float64') | ||
| 101 | self.assertRaises (ValueError, alpha_norm, a, 1) | ||
| 102 | # check 3d array fail | ||
| 103 | a = array([[[1,2],[3,4]]], dtype = 'float64') | ||
| 104 | self.assertRaises (ValueError, alpha_norm, a, 1) | ||
| 105 | # check float64 1d array fail | ||
| 106 | a = array(range(10), dtype = 'float64') | ||
| 107 | self.assertRaises (ValueError, alpha_norm, a, 1) | ||
| 108 | # check float64 2d array fail | ||
| 109 | a = array([range(10), range(10)], dtype = 'float64') | ||
| 110 | self.assertRaises (ValueError, alpha_norm, a, 1) | ||
| 111 | |||
| 112 | def test_fvec_min_removal_of_array(self): | ||
| 113 | a = array([20,1,19], dtype='float32') | ||
| 114 | b = min_removal(a) | ||
| 115 | assert_equal (array(b), [19, 0, 18]) | ||
| 116 | assert_equal (b, [19, 0, 18]) | ||
| 117 | assert_equal (a, b) | ||
| 118 | a[0] = 0 | ||
| 119 | assert_equal (a, b) | ||
| 120 | |||
| 121 | def test_fvec_min_removal_of_array_float64(self): | ||
| 122 | a = array([20,1,19], dtype='float64') | ||
| 123 | self.assertRaises (ValueError, min_removal, a) | ||
| 124 | |||
| 125 | def test_fvec_min_removal_of_fvec(self): | ||
| 126 | a = fvec(3) | ||
| 127 | a = array([20, 1, 19], dtype = 'float32') | ||
| 128 | b = min_removal(a) | ||
| 129 | assert_equal (array(b), [19, 0, 18]) | ||
| 130 | assert_equal (b, [19, 0, 18]) | ||
| 131 | assert_equal (a, b) | ||
| 132 | |||
| 133 | if __name__ == '__main__': | ||
| 134 | from unittest import main | ||
| 135 | main() | ||
| @@ -0,0 +1,20 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from numpy.testing import TestCase, run_module_suite | ||
| 4 | from numpy.testing import assert_equal, assert_almost_equal | ||
| 5 | # WARNING: numpy also has an fft object | ||
| 6 | from aubio import onset, cvec | ||
| 7 | from numpy import array, shape, arange, zeros, log | ||
| 8 | from math import pi | ||
| 9 | |||
| 10 | class aubio_onset(TestCase): | ||
| 11 | |||
| 12 | def test_members(self): | ||
| 13 | o = onset() | ||
| 14 | assert_equal ([o.buf_size, o.hop_size, o.method, o.samplerate], | ||
| 15 | [1024,512,'default',44100]) | ||
| 16 | |||
| 17 | |||
| 18 | if __name__ == '__main__': | ||
| 19 | from unittest import main | ||
| 20 | main() | ||
| @@ -0,0 +1,115 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from numpy.testing import TestCase, assert_equal, assert_almost_equal | ||
| 4 | from aubio import peakpicker, fvec | ||
| 5 | |||
| 6 | class aubio_peakpicker(TestCase): | ||
| 7 | |||
| 8 | def test_members(self): | ||
| 9 | o = peakpicker() | ||
| 10 | |||
| 11 | def test_peakpicker_zeroes(self): | ||
| 12 | o = peakpicker() | ||
| 13 | assert_equal(o.get_thresholded_input(), 0.) | ||
| 14 | |||
| 15 | def test_peakpick_set_threshold(self): | ||
| 16 | o = peakpicker() | ||
| 17 | new_threshold = threshold | ||
| 18 | o.set_threshold(new_threshold) | ||
| 19 | assert_almost_equal(new_threshold, o.get_threshold()) | ||
| 20 | |||
| 21 | def test_peakpicker_get_threshold(self): | ||
| 22 | o = peakpicker() | ||
| 23 | new_threshold = o.get_threshold() | ||
| 24 | o.set_threshold(new_threshold) | ||
| 25 | assert_equal(new_threshold, o.get_threshold()) | ||
| 26 | |||
| 27 | buf_size = 1024 | ||
| 28 | slice_size = 5 | ||
| 29 | delay = 1 | ||
| 30 | threshold = .9 | ||
| 31 | |||
| 32 | class aubio_peakpicker_peaks(TestCase): | ||
| 33 | |||
| 34 | def setUp(self): | ||
| 35 | self.o = peakpicker() | ||
| 36 | self.o.set_threshold (threshold) | ||
| 37 | self.vec = fvec(buf_size) | ||
| 38 | |||
| 39 | def test_peakpicker_impulse(self): | ||
| 40 | vec = self.vec; o = self.o | ||
| 41 | a = 345 | ||
| 42 | vec[a] = 1000. | ||
| 43 | self.peaks = [a] | ||
| 44 | |||
| 45 | def test_peakpicker_ramp_up(self): | ||
| 46 | vec = self.vec; o = self.o | ||
| 47 | a = 345 | ||
| 48 | vec[a] = 1000. / 4. * 1. | ||
| 49 | vec[a+1] = 1000. / 4. * 2. | ||
| 50 | vec[a+2] = 1000. / 4. * 3. | ||
| 51 | vec[a+3] = 1000. | ||
| 52 | self.peaks = [a+1] | ||
| 53 | |||
| 54 | def test_peakpicker_ramp_down(self): | ||
| 55 | vec = self.vec; o = self.o | ||
| 56 | a = 345 | ||
| 57 | vec[a] = 1000. | ||
| 58 | vec[a+1] = 1000. / 4. * 3. | ||
| 59 | vec[a+2] = 1000. / 4. * 2. | ||
| 60 | vec[a+3] = 1000. / 4. * 1. | ||
| 61 | self.peaks = [a] | ||
| 62 | |||
| 63 | def test_peakpicker_plateau(self): | ||
| 64 | vec = self.vec; o = self.o | ||
| 65 | a = 345 | ||
| 66 | vec[a] = 1000. / 2 | ||
| 67 | vec[a+1] = 1000. | ||
| 68 | vec[a+2] = 1000. | ||
| 69 | vec[a+3] = 1000. | ||
| 70 | vec[a+4] = 1000. / 2 | ||
| 71 | self.peaks = [a+1] | ||
| 72 | |||
| 73 | def test_peakpicker_consecutive_peaks(self): | ||
| 74 | vec = self.vec; o = self.o | ||
| 75 | a = 345 | ||
| 76 | vec[a] = 1000. / 2 | ||
| 77 | vec[a+1] = 1000. | ||
| 78 | vec[a+3] = 1000. | ||
| 79 | vec[a+4] = 1000. / 2 | ||
| 80 | self.peaks = [a] | ||
| 81 | |||
| 82 | def test_peakpicker_distant_peaks(self): | ||
| 83 | vec = self.vec; o = self.o | ||
| 84 | a = 345 | ||
| 85 | vec[a] = 1000. | ||
| 86 | vec[a+7] = 1000. | ||
| 87 | self.peaks = [a, a+7] | ||
| 88 | |||
| 89 | def test_peakpicker_very_distant_peaks(self): | ||
| 90 | vec = self.vec; o = self.o | ||
| 91 | a = 345 | ||
| 92 | vec[a] = 1000. | ||
| 93 | vec[a+67] = 1000. | ||
| 94 | self.peaks = [a, a+67] | ||
| 95 | |||
| 96 | def tearDown(self): | ||
| 97 | fpeaks = [] | ||
| 98 | for index in range(0,buf_size-slice_size): | ||
| 99 | sliced = self.vec[index:index+slice_size] | ||
| 100 | findex = self.o(sliced) | ||
| 101 | if findex: | ||
| 102 | # we found a peak | ||
| 103 | fpeak = index - findex - delay | ||
| 104 | #print self.peaks, index, '-', findex, '-', delay, '=', fpeak | ||
| 105 | if not round(index - findex - delay) in self.peaks: | ||
| 106 | self.fail('missing peak ' + str(fpeak)) | ||
| 107 | fpeaks.append(fpeak) | ||
| 108 | if len(fpeaks) != len(self.peaks): | ||
| 109 | self.fail('some peaks of ' + str(self.peaks) + 'were not found, got only ' + str(fpeaks)) | ||
| 110 | |||
| 111 | #print fpeaks, self.peaks | ||
| 112 | |||
| 113 | if __name__ == '__main__': | ||
| 114 | from unittest import main | ||
| 115 | main() | ||
| @@ -0,0 +1,65 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from numpy.testing import TestCase, run_module_suite | ||
| 4 | from numpy.testing import assert_equal, assert_almost_equal | ||
| 5 | from aubio import fvec, cvec, pvoc | ||
| 6 | from numpy import array, shape | ||
| 7 | |||
| 8 | class aubio_pvoc_test_case(TestCase): | ||
| 9 | |||
| 10 | def test_members(self): | ||
| 11 | f = pvoc() | ||
| 12 | assert_equal ([f.win_s, f.hop_s], [1024, 512]) | ||
| 13 | f = pvoc(2048, 128) | ||
| 14 | assert_equal ([f.win_s, f.hop_s], [2048, 128]) | ||
| 15 | |||
| 16 | def test_zeros(self): | ||
| 17 | win_s, hop_s = 1024, 256 | ||
| 18 | f = pvoc (win_s, hop_s) | ||
| 19 | t = fvec (hop_s) | ||
| 20 | for time in range( 4 * win_s / hop_s ): | ||
| 21 | s = f(t) | ||
| 22 | r = f.rdo(s) | ||
| 23 | assert_equal ( array(t), 0) | ||
| 24 | assert_equal ( s.norm, 0) | ||
| 25 | assert_equal ( s.phas, 0) | ||
| 26 | assert_equal ( r, 0) | ||
| 27 | |||
| 28 | def test_steps_two_channels(self): | ||
| 29 | """ check the resynthesis of steps is correct """ | ||
| 30 | f = pvoc(1024, 512) | ||
| 31 | t1 = fvec(512) | ||
| 32 | t2 = fvec(512) | ||
| 33 | # positive step in first channel | ||
| 34 | t1[100:200] = .1 | ||
| 35 | # positive step in second channel | ||
| 36 | t1[20:50] = -.1 | ||
| 37 | s1 = f(t1) | ||
| 38 | r1 = f.rdo(s1) | ||
| 39 | s2 = f(t2) | ||
| 40 | r2 = f.rdo(s2) | ||
| 41 | #self.plot_this ( s1.norm.T ) | ||
| 42 | assert_almost_equal ( t1, r2, decimal = 6 ) | ||
| 43 | |||
| 44 | def test_steps_three_random_channels(self): | ||
| 45 | from random import random | ||
| 46 | f = pvoc(64, 16) | ||
| 47 | t0 = fvec(16) | ||
| 48 | t1 = fvec(16) | ||
| 49 | for i in xrange(16): | ||
| 50 | t1[i] = random() * 2. - 1. | ||
| 51 | t2 = f.rdo(f(t1)) | ||
| 52 | t2 = f.rdo(f(t0)) | ||
| 53 | t2 = f.rdo(f(t0)) | ||
| 54 | t2 = f.rdo(f(t0)) | ||
| 55 | assert_almost_equal( t1, t2, decimal = 6 ) | ||
| 56 | |||
| 57 | def plot_this( self, this ): | ||
| 58 | from pylab import semilogy, show | ||
| 59 | semilogy ( this ) | ||
| 60 | show () | ||
| 61 | |||
| 62 | if __name__ == '__main__': | ||
| 63 | from unittest import main | ||
| 64 | main() | ||
| 65 | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from numpy.testing import TestCase | ||
| 4 | from numpy.testing import assert_equal, assert_almost_equal | ||
| 5 | from numpy import random, sin, arange, mean, median | ||
| 6 | from math import pi | ||
| 7 | from aubio import fvec, pitch | ||
| 8 | |||
| 9 | class aubio_mathutils_test_case(TestCase): | ||
| 10 | |||
| 11 | def test_members(self): | ||
| 12 | p = pitch() | ||
| 13 | assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate], | ||
| 14 | ['default', 1024, 512, 44100]) | ||
| 15 | |||
| 16 | def test_members_not_default(self): | ||
| 17 | p = pitch('mcomb', 2048, 512, 32000) | ||
| 18 | assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate], | ||
| 19 | ['mcomb', 2048, 512, 32000]) | ||
| 20 | |||
| 21 | def test_run_on_zeros(self): | ||
| 22 | p = pitch('mcomb', 2048, 512, 32000) | ||
| 23 | f = fvec (512) | ||
| 24 | assert_equal ( p(f), 0. ) | ||
| 25 | |||
| 26 | def test_run_on_ones(self): | ||
| 27 | p = pitch('mcomb', 2048, 512, 32000) | ||
| 28 | f = fvec (512) | ||
| 29 | f[:] = 1 | ||
| 30 | assert( p(f) != 0. ) | ||
| 31 | |||
| 32 | def test_run_default_on_sinusoid(self): | ||
| 33 | method = 'default' | ||
| 34 | buf_size = 2048 | ||
| 35 | hop_size = 512 | ||
| 36 | samplerate = 32000 | ||
| 37 | freq = 450. | ||
| 38 | self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) | ||
| 39 | |||
| 40 | def test_run_schmitt_on_sinusoid(self): | ||
| 41 | method = 'schmitt' | ||
| 42 | buf_size = 4096 | ||
| 43 | hop_size = 512 | ||
| 44 | samplerate = 44100 | ||
| 45 | freq = 800. | ||
| 46 | self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) | ||
| 47 | |||
| 48 | def test_run_mcomb_on_sinusoid(self): | ||
| 49 | method = 'mcomb' | ||
| 50 | buf_size = 2048 | ||
| 51 | hop_size = 512 | ||
| 52 | samplerate = 44100 | ||
| 53 | freq = 10000. | ||
| 54 | self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) | ||
| 55 | |||
| 56 | def test_run_fcomb_on_sinusoid(self): | ||
| 57 | method = 'fcomb' | ||
| 58 | buf_size = 2048 | ||
| 59 | hop_size = 512 | ||
| 60 | samplerate = 32000 | ||
| 61 | freq = 440. | ||
| 62 | self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) | ||
| 63 | |||
| 64 | def test_run_yin_on_sinusoid(self): | ||
| 65 | method = 'yin' | ||
| 66 | buf_size = 4096 | ||
| 67 | hop_size = 512 | ||
| 68 | samplerate = 32000 | ||
| 69 | freq = 880. | ||
| 70 | self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) | ||
| 71 | |||
| 72 | def test_run_yinfft_on_sinusoid(self): | ||
| 73 | method = 'yinfft' | ||
| 74 | buf_size = 2048 | ||
| 75 | hop_size = 512 | ||
| 76 | samplerate = 32000 | ||
| 77 | freq = 640. | ||
| 78 | self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) | ||
| 79 | |||
| 80 | def run_pitch_on_sinusoid(self, method, buf_size, hop_size, samplerate, freq): | ||
| 81 | p = pitch(method, buf_size, hop_size, samplerate) | ||
| 82 | sinvec = self.build_sinusoid(hop_size * 100, freq, samplerate) | ||
| 83 | self.run_pitch(p, sinvec, freq) | ||
| 84 | |||
| 85 | def build_sinusoid(self, length, freq, samplerate): | ||
| 86 | return sin( 2. * pi * arange(length).astype('float32') * freq / samplerate) | ||
| 87 | |||
| 88 | def run_pitch(self, p, input_vec, freq): | ||
| 89 | count = 0 | ||
| 90 | pitches, errors = [], [] | ||
| 91 | for vec_slice in input_vec.reshape((-1, p.hop_size)): | ||
| 92 | pitch = p(vec_slice) | ||
| 93 | pitches.append(pitch) | ||
| 94 | errors.append(1. - pitch / freq) | ||
| 95 | # check that the mean of all relative errors is less than 10% | ||
| 96 | assert_almost_equal (mean(errors), 0., decimal = 2) | ||
| 97 | |||
| 98 | if __name__ == '__main__': | ||
| 99 | from unittest import main | ||
| 100 | main() | ||
| 101 | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from numpy.testing import TestCase, assert_equal, assert_almost_equal | ||
| 4 | from aubio import fvec, source | ||
| 5 | from numpy import array | ||
| 6 | |||
| 7 | path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav" | ||
| 8 | |||
| 9 | class aubio_filter_test_case(TestCase): | ||
| 10 | |||
| 11 | def test_members(self): | ||
| 12 | f = source(path) | ||
| 13 | print dir(f) | ||
| 14 | |||
| 15 | def test_read(self): | ||
| 16 | f = source(path) | ||
| 17 | total_frames = 0 | ||
| 18 | while True: | ||
| 19 | vec, read = f() | ||
| 20 | total_frames += read | ||
| 21 | if read < f.hop_size: break | ||
| 22 | print "read", total_frames / float(f.samplerate), " seconds from", path | ||
| 23 | |||
| 24 | if __name__ == '__main__': | ||
| 25 | from unittest import main | ||
| 26 | main() | ||
| 27 | |||
| @@ -0,0 +1,238 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from numpy.testing import TestCase, assert_equal, assert_almost_equal | ||
| 4 | from numpy import random, arange, log, zeros | ||
| 5 | from aubio import specdesc, cvec | ||
| 6 | from math import pi | ||
| 7 | |||
| 8 | methods = ["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"] | ||
| 24 | buf_size = 2048 | ||
| 25 | |||
| 26 | class aubio_specdesc(TestCase): | ||
| 27 | |||
| 28 | def test_members(self): | ||
| 29 | o = specdesc() | ||
| 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 | |||
| 78 | def test_hfc(self): | ||
| 79 | o = specdesc("hfc") | ||
| 80 | c = cvec() | ||
| 81 | assert_equal( 0., o(c)) | ||
| 82 | a = arange(c.length, dtype='float32') | ||
| 83 | c.norm = a | ||
| 84 | assert_equal (a, c.norm) | ||
| 85 | assert_equal ( sum(a*(a+1)), o(c)) | ||
| 86 | |||
| 87 | def test_complex(self): | ||
| 88 | o = specdesc("complex") | ||
| 89 | c = cvec() | ||
| 90 | assert_equal( 0., o(c)) | ||
| 91 | a = arange(c.length, dtype='float32') | ||
| 92 | c.norm = a | ||
| 93 | assert_equal (a, c.norm) | ||
| 94 | # the previous run was on zeros, so previous frames are still 0 | ||
| 95 | # so we have sqrt ( abs ( r2 ^ 2) ) == r2 | ||
| 96 | assert_equal ( sum(a), o(c)) | ||
| 97 | # second time. c.norm = a, so, r1 = r2, and the euclidian distance is 0 | ||
| 98 | assert_equal ( 0, o(c)) | ||
| 99 | |||
| 100 | def test_kl(self): | ||
| 101 | o = specdesc("kl") | ||
| 102 | c = cvec() | ||
| 103 | assert_equal( 0., o(c)) | ||
| 104 | a = arange(c.length, dtype='float32') | ||
| 105 | c.norm = a | ||
| 106 | assert_almost_equal( sum(a * log(1.+ a/1.e-10 ) ) / o(c), 1., decimal=6) | ||
| 107 | |||
| 108 | def test_mkl(self): | ||
| 109 | o = specdesc("mkl") | ||
| 110 | c = cvec() | ||
| 111 | assert_equal( 0., o(c)) | ||
| 112 | a = arange(c.length, dtype='float32') | ||
| 113 | c.norm = a | ||
| 114 | assert_almost_equal( sum(log(1.+ a/1.e-10 ) ) / o(c), 1, decimal=6) | ||
| 115 | |||
| 116 | def test_specflux(self): | ||
| 117 | o = specdesc("specflux") | ||
| 118 | c = cvec() | ||
| 119 | assert_equal( 0., o(c)) | ||
| 120 | a = arange(c.length, dtype='float32') | ||
| 121 | c.norm = a | ||
| 122 | assert_equal( sum(a), o(c)) | ||
| 123 | assert_equal( 0, o(c)) | ||
| 124 | c.norm = zeros(c.length, dtype='float32') | ||
| 125 | assert_equal( 0, o(c)) | ||
| 126 | |||
| 127 | def test_centroid(self): | ||
| 128 | o = specdesc("centroid") | ||
| 129 | c = cvec() | ||
| 130 | # make sure centroid of zeros is zero | ||
| 131 | assert_equal( 0., o(c)) | ||
| 132 | a = arange(c.length, dtype='float32') | ||
| 133 | c.norm = a | ||
| 134 | centroid = sum(a*a) / sum(a) | ||
| 135 | assert_almost_equal (centroid, o(c), decimal = 2) | ||
| 136 | |||
| 137 | c.norm = a * .5 | ||
| 138 | assert_almost_equal (centroid, o(c), decimal = 2) | ||
| 139 | |||
| 140 | def test_spread(self): | ||
| 141 | o = specdesc("spread") | ||
| 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) | ||
| 151 | |||
| 152 | def test_skewness(self): | ||
| 153 | o = specdesc("skewness") | ||
| 154 | c = cvec() | ||
| 155 | assert_equal( 0., o(c)) | ||
| 156 | a = arange(c.length, dtype='float32') | ||
| 157 | c.norm = a | ||
| 158 | centroid = sum(a*a) / sum(a) | ||
| 159 | spread = sum( (a - centroid)**2 *a) / sum(a) | ||
| 160 | skewness = sum( (a - centroid)**3 *a) / sum(a) / spread **1.5 | ||
| 161 | assert_almost_equal (skewness, o(c), decimal = 2) | ||
| 162 | |||
| 163 | c.norm = a * 3 | ||
| 164 | assert_almost_equal (skewness, o(c), decimal = 2) | ||
| 165 | |||
| 166 | def test_kurtosis(self): | ||
| 167 | o = specdesc("kurtosis") | ||
| 168 | c = cvec() | ||
| 169 | assert_equal( 0., o(c)) | ||
| 170 | a = arange(c.length, dtype='float32') | ||
| 171 | c.norm = a | ||
| 172 | centroid = sum(a*a) / sum(a) | ||
| 173 | spread = sum( (a - centroid)**2 *a) / sum(a) | ||
| 174 | kurtosis = sum( (a - centroid)**4 *a) / sum(a) / spread **2 | ||
| 175 | assert_almost_equal (kurtosis, o(c), decimal = 2) | ||
| 176 | |||
| 177 | def test_slope(self): | ||
| 178 | o = specdesc("slope") | ||
| 179 | c = cvec() | ||
| 180 | assert_equal( 0., o(c)) | ||
| 181 | a = arange(c.length * 2, 0, -2, dtype='float32') | ||
| 182 | k = arange(c.length, dtype='float32') | ||
| 183 | c.norm = a | ||
| 184 | num = len(a) * sum(k*a) - sum(k)*sum(a) | ||
| 185 | den = (len(a) * sum(k**2) - sum(k)**2) | ||
| 186 | slope = num/den/sum(a) | ||
| 187 | assert_almost_equal (slope, o(c), decimal = 5) | ||
| 188 | |||
| 189 | a = arange(0, c.length * 2, +2, dtype='float32') | ||
| 190 | c.norm = a | ||
| 191 | num = len(a) * sum(k*a) - sum(k)*sum(a) | ||
| 192 | den = (len(a) * sum(k**2) - sum(k)**2) | ||
| 193 | slope = num/den/sum(a) | ||
| 194 | assert_almost_equal (slope, o(c), decimal = 5) | ||
| 195 | |||
| 196 | a = arange(0, c.length * 2, +2, dtype='float32') | ||
| 197 | c.norm = a * 2 | ||
| 198 | assert_almost_equal (slope, o(c), decimal = 5) | ||
| 199 | |||
| 200 | def test_decrease(self): | ||
| 201 | o = specdesc("decrease") | ||
| 202 | c = cvec() | ||
| 203 | assert_equal( 0., o(c)) | ||
| 204 | a = arange(c.length * 2, 0, -2, dtype='float32') | ||
| 205 | k = arange(c.length, dtype='float32') | ||
| 206 | c.norm = a | ||
| 207 | decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) | ||
| 208 | assert_almost_equal (decrease, o(c), decimal = 5) | ||
| 209 | |||
| 210 | a = arange(0, c.length * 2, +2, dtype='float32') | ||
| 211 | c.norm = a | ||
| 212 | decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) | ||
| 213 | assert_almost_equal (decrease, o(c), decimal = 5) | ||
| 214 | |||
| 215 | a = arange(0, c.length * 2, +2, dtype='float32') | ||
| 216 | c.norm = a * 2 | ||
| 217 | decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) | ||
| 218 | assert_almost_equal (decrease, o(c), decimal = 5) | ||
| 219 | |||
| 220 | def test_rolloff(self): | ||
| 221 | o = specdesc("rolloff") | ||
| 222 | c = cvec() | ||
| 223 | assert_equal( 0., o(c)) | ||
| 224 | a = arange(c.length * 2, 0, -2, dtype='float32') | ||
| 225 | k = arange(c.length, dtype='float32') | ||
| 226 | c.norm = a | ||
| 227 | cumsum = .95*sum(a*a) | ||
| 228 | i = 0; rollsum = 0 | ||
| 229 | while rollsum < cumsum: | ||
| 230 | rollsum += a[i]*a[i] | ||
| 231 | i+=1 | ||
| 232 | rolloff = i | ||
| 233 | assert_equal (rolloff, o(c)) | ||
| 234 | |||
| 235 | |||
| 236 | if __name__ == '__main__': | ||
| 237 | from unittest import main | ||
| 238 | main() | ||