From: Paul Brossier Date: Fri, 25 Dec 2009 03:57:49 +0000 (+0100) Subject: test_*.py: switch to mono, add tests for cvec X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1a6ef2cc59efef576596a70614b44f301bf649f9;p=aubio.git test_*.py: switch to mono, add tests for cvec --- diff --git a/interfaces/python/test_cvec.py b/interfaces/python/test_cvec.py new file mode 100644 index 00000000..99051a98 --- /dev/null +++ b/interfaces/python/test_cvec.py @@ -0,0 +1,50 @@ +from numpy.testing import TestCase, run_module_suite +from numpy.testing import assert_equal, assert_almost_equal +from _aubio import cvec +from numpy import array, shape, pi + +class aubio_cvec_test_case(TestCase): + + def test_vector_created_with_zeroes(self): + a = cvec(10) + a + shape(a.norm) + shape(a.phas) + a.norm[0] + assert_equal(a.norm, 0.) + assert_equal(a.phas, 0.) + + def test_vector_assign_element(self): + a = cvec() + a.norm[0] = 1 + assert_equal(a.norm[0], 1) + a.phas[0] = 1 + assert_equal(a.phas[0], 1) + + def test_vector_assign_element_end(self): + a = cvec() + a.norm[-1] = 1 + assert_equal(a.norm[-1], 1) + assert_equal(a.norm[len(a.norm)-1], 1) + a.phas[-1] = 1 + assert_equal(a.phas[-1], 1) + assert_equal(a.phas[len(a.phas)-1], 1) + + def test_assign_cvec_norm_slice(self): + spec = cvec(1024) + spec.norm[40:100] = 100 + assert_equal (spec.norm[0:40], 0) + assert_equal (spec.norm[40:100], 100) + assert_equal (spec.norm[100:-1], 0) + assert_equal (spec.phas, 0) + + def test_assign_cvec_phas_slice(self): + spec = cvec(1024) + spec.phas[39:-1] = -pi + assert_equal (spec.phas[0:39], 0) + assert_equal (spec.phas[39:-1], -pi) + assert_equal (spec.norm, 0) + +if __name__ == '__main__': + from unittest import main + main() diff --git a/interfaces/python/test_fft.py b/interfaces/python/test_fft.py index a8bebde1..4ac92f15 100644 --- a/interfaces/python/test_fft.py +++ b/interfaces/python/test_fft.py @@ -1,7 +1,8 @@ from numpy.testing import TestCase, run_module_suite from numpy.testing import assert_equal, assert_almost_equal # WARNING: numpy also has an fft object -from _aubio import fft, fvec, cvec +from _aubio import fft, cvec +from aubio import fvec from numpy import array, shape from math import pi @@ -9,28 +10,24 @@ class aubio_fft_test_case(TestCase): def test_members(self): f = fft() - assert_equal ([f.win_s, f.channels], [1024, 1]) - f = fft(2048, 4) - assert_equal ([f.win_s, f.channels], [2048, 4]) + assert_equal (f.win_s, 1024) def test_output_dimensions(self): """ check the dimensions of output """ - win_s, chan = 1024, 3 - timegrain = fvec(win_s, chan) - f = fft(win_s, chan) + win_s = 1024 + timegrain = fvec(win_s) + f = fft(win_s) fftgrain = f (timegrain) - assert_equal (array(fftgrain), 0) - assert_equal (shape(fftgrain), (chan * 2, win_s/2+1)) assert_equal (fftgrain.norm, 0) - assert_equal (shape(fftgrain.norm), (chan, win_s/2+1)) + assert_equal (shape(fftgrain.norm), (win_s/2+1,)) assert_equal (fftgrain.phas, 0) - assert_equal (shape(fftgrain.phas), (chan, win_s/2+1)) + assert_equal (shape(fftgrain.phas), (win_s/2+1,)) def test_zeros(self): """ check the transform of zeros """ - win_s, chan = 512, 3 - timegrain = fvec(win_s, chan) - f = fft(win_s, chan) + win_s = 512 + timegrain = fvec(win_s) + f = fft(win_s) fftgrain = f(timegrain) assert_equal ( fftgrain.norm == 0, True ) assert_equal ( fftgrain.phas == 0, True ) @@ -39,14 +36,14 @@ class aubio_fft_test_case(TestCase): """ check the transform of one impulse at a random place """ from random import random from math import floor - win_s, chan = 256, 1 + win_s = 256 i = floor(random()*win_s) impulse = pi * random() - f = fft(win_s, chan) - timegrain = fvec(win_s, chan) - timegrain[0][i] = impulse + f = fft(win_s) + timegrain = fvec(win_s) + timegrain[i] = impulse fftgrain = f ( timegrain ) - #self.plot_this ( fftgrain.phas[0] ) + #self.plot_this ( fftgrain.phas ) assert_almost_equal ( fftgrain.norm, impulse, decimal = 6 ) assert_equal ( fftgrain.phas <= pi, True) assert_equal ( fftgrain.phas >= -pi, True) @@ -55,24 +52,24 @@ class aubio_fft_test_case(TestCase): """ check the transform of one impulse at a random place """ from random import random from math import floor - win_s, chan = 256, 1 + win_s = 256 i = 0 impulse = -10. - f = fft(win_s, chan) - timegrain = fvec(win_s, chan) - timegrain[0][i] = impulse + f = fft(win_s) + timegrain = fvec(win_s) + timegrain[i] = impulse fftgrain = f ( timegrain ) - #self.plot_this ( fftgrain.phas[0] ) + #self.plot_this ( fftgrain.phas ) assert_almost_equal ( fftgrain.norm, abs(impulse), decimal = 6 ) if impulse < 0: # phase can be pi or -pi, as it is not unwrapped - assert_almost_equal ( abs(fftgrain.phas[0][1:-1]) , pi, decimal = 6 ) - assert_almost_equal ( fftgrain.phas[0][0], pi, decimal = 6) - assert_almost_equal ( fftgrain.phas[0][-1], pi, decimal = 6) + assert_almost_equal ( abs(fftgrain.phas[1:-1]) , pi, decimal = 6 ) + assert_almost_equal ( fftgrain.phas[0], pi, decimal = 6) + assert_almost_equal ( fftgrain.phas[-1], pi, decimal = 6) else: - assert_equal ( fftgrain.phas[0][1:-1] == 0, True) - assert_equal ( fftgrain.phas[0][0] == 0, True) - assert_equal ( fftgrain.phas[0][-1] == 0, True) + assert_equal ( fftgrain.phas[1:-1] == 0, True) + assert_equal ( fftgrain.phas[0] == 0, True) + assert_equal ( fftgrain.phas[-1] == 0, True) # now check the resynthesis synthgrain = f.rdo ( fftgrain ) #self.plot_this ( fftgrain.phas.T ) @@ -82,25 +79,24 @@ class aubio_fft_test_case(TestCase): assert_almost_equal ( synthgrain, timegrain, decimal = 6 ) def test_impulse_at_zero(self): - """ check the transform of one impulse at a index 0 in one channel """ - win_s, chan = 1024, 2 + """ check the transform of one impulse at a index 0 """ + win_s = 1024 impulse = pi - f = fft(win_s, chan) - timegrain = fvec(win_s, chan) - timegrain[0][0] = impulse + f = fft(win_s) + timegrain = fvec(win_s) + timegrain[0] = impulse fftgrain = f ( timegrain ) #self.plot_this ( fftgrain.phas ) assert_equal ( fftgrain.phas[0], 0) assert_equal ( fftgrain.phas[1], 0) - assert_almost_equal ( fftgrain.norm[0], impulse, decimal = 6 ) - assert_equal ( fftgrain.norm[0], impulse) + assert_almost_equal (fftgrain.norm[0], impulse, decimal = 6 ) def test_rdo_before_do(self): """ check running fft.rdo before fft.do works """ - win_s, chan = 1024, 2 + win_s = 1024 impulse = pi - f = fft(win_s, chan) - fftgrain = cvec(win_s, chan) + f = fft(win_s) + fftgrain = cvec(win_s) t = f.rdo( fftgrain ) assert_equal ( t, 0 ) diff --git a/interfaces/python/test_filter.py b/interfaces/python/test_filter.py index b3bcac1d..443e21f1 100644 --- a/interfaces/python/test_filter.py +++ b/interfaces/python/test_filter.py @@ -1,6 +1,7 @@ from numpy.testing import TestCase, run_module_suite from numpy.testing import assert_equal, assert_almost_equal from _aubio import * +from aubio import fvec from numpy import array def array_from_text_file(filename, dtype = 'float'): @@ -11,49 +12,49 @@ class aubio_filter_test_case(TestCase): def test_members(self): f = digital_filter() - assert_equal ([f.channels, f.order], [1, 7]) - f = digital_filter(5, 2) - assert_equal ([f.channels, f.order], [2, 5]) + assert_equal (f.order, 7) + f = digital_filter(5) + assert_equal (f.order, 5) f(fvec()) def test_cweighting_error(self): - f = digital_filter (2, 1) + f = digital_filter (2) self.assertRaises ( ValueError, f.set_c_weighting, 44100 ) - f = digital_filter (8, 1) + f = digital_filter (8) self.assertRaises ( ValueError, f.set_c_weighting, 44100 ) - f = digital_filter (5, 1) + f = digital_filter (5) self.assertRaises ( ValueError, f.set_c_weighting, 4000 ) - f = digital_filter (5, 1) + f = digital_filter (5) self.assertRaises ( ValueError, f.set_c_weighting, 193000 ) - f = digital_filter (7, 1) + f = digital_filter (7) self.assertRaises ( ValueError, f.set_a_weighting, 193000 ) - f = digital_filter (5, 1) + f = digital_filter (5) self.assertRaises ( ValueError, f.set_a_weighting, 192000 ) def test_c_weighting(self): expected = array_from_text_file('c_weighting_test_simple.expected') - f = digital_filter(5, 1) + f = digital_filter(5) f.set_c_weighting(44100) v = fvec(32) - v[0][12] = .5 + v[12] = .5 u = f(v) assert_almost_equal (expected[1], u) def test_a_weighting(self): expected = array_from_text_file('a_weighting_test_simple.expected') - f = digital_filter(7, 1) + f = digital_filter(7) f.set_a_weighting(44100) v = fvec(32) - v[0][12] = .5 + v[12] = .5 u = f(v) assert_almost_equal (expected[1], u) def test_a_weighting_parted(self): expected = array_from_text_file('a_weighting_test_simple.expected') - f = digital_filter(7, 1) + f = digital_filter(7) f.set_a_weighting(44100) v = fvec(16) - v[0][12] = .5 + v[12] = .5 u = f(v) assert_almost_equal (expected[1][:16], u) # one more time diff --git a/interfaces/python/test_filterbank.py b/interfaces/python/test_filterbank.py index 0a718f56..e95e87c6 100644 --- a/interfaces/python/test_filterbank.py +++ b/interfaces/python/test_filterbank.py @@ -2,6 +2,7 @@ from numpy.testing import TestCase, run_module_suite from numpy.testing import assert_equal, assert_almost_equal from numpy import array, shape from _aubio import * +#from aubio import cvec class aubio_filter_test_case(TestCase): @@ -10,6 +11,7 @@ class aubio_filter_test_case(TestCase): f.set_mel_coeffs_slaney(16000) a = f.get_coeffs() a.T + assert_equal(shape (a), (40, 512/2 + 1) ) def test_other_slaney(self): f = filterbank(40, 512*2) @@ -22,16 +24,25 @@ class aubio_filter_test_case(TestCase): a = f.get_coeffs() #print "sum is", sum(sum(a)) - def test_triangle_freqs(self): + def test_triangle_freqs_zeros(self): + f = filterbank(9, 1024) + freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] + freqs = array(freq_list, dtype = 'float32') + f.set_triangle_bands(freqs, 48000) + f.get_coeffs().T + assert_equal ( f(cvec(1024)), 0) + + def test_triangle_freqs_ones(self): f = filterbank(9, 1024) freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] freqs = array(freq_list, dtype = 'float32') f.set_triangle_bands(freqs, 48000) f.get_coeffs().T - assert_equal ( f(cvec(1024)), [0] * 9) spec = cvec(1024) - spec[0][40:100] = 100 - #print f(spec) + spec.norm[:] = 1 + assert_almost_equal ( f(spec), + [ 0.02070313, 0.02138672, 0.02127604, 0.02135417, + 0.02133301, 0.02133301, 0.02133311, 0.02133334, 0.02133345]) if __name__ == '__main__': from unittest import main diff --git a/interfaces/python/test_fvec.py b/interfaces/python/test_fvec.py index ba124e9e..bd94af66 100644 --- a/interfaces/python/test_fvec.py +++ b/interfaces/python/test_fvec.py @@ -1,13 +1,17 @@ from numpy.testing import TestCase, run_module_suite from numpy.testing import assert_equal, assert_almost_equal from _aubio import * -from numpy import array +from aubio import fvec +from numpy import array, shape class aubio_fvec_test_case(TestCase): - def test_vector_created_with_zeroes(self): - a = fvec() + a = fvec(10) + a + shape(a) + a[0] + #del a assert_equal(array(a), 0.) def test_vector_assign_element(self): @@ -19,17 +23,18 @@ class aubio_fvec_test_case(TestCase): a = fvec() a[-1] = 1 assert_equal(a[-1], 1) - assert_equal(a[a.length-1], 1) + assert_equal(a[len(a)-1], 1) def test_vector(self): a = fvec() - a, a.length + a, len(a) #a.length a[0] array(a) a = fvec(10) a = fvec(1) + a.T array(a).T - a[0] = range(a.length) + a = range(len(a)) def test_wrong_values(self): self.assertRaises (ValueError, fvec, -10) diff --git a/interfaces/python/test_onsetdetection.py b/interfaces/python/test_onsetdetection.py index 35a8af9f..23b70ba6 100644 --- a/interfaces/python/test_onsetdetection.py +++ b/interfaces/python/test_onsetdetection.py @@ -1,7 +1,7 @@ from numpy.testing import TestCase, run_module_suite from numpy.testing import assert_equal, assert_almost_equal # WARNING: numpy also has an fft object -from _aubio import cvec, specdesc +from _aubio import specdesc, cvec from numpy import array, shape, arange, zeros, log from math import pi @@ -9,11 +9,8 @@ class aubio_specdesc(TestCase): def test_members(self): o = specdesc() - assert_equal ([o.buf_size, o.channels, o.method], - [1024, 1, "default"]) - o = specdesc("complex", 512, 2) - assert_equal ([o.buf_size, o.channels, o.method], - [512, 2, "complex"]) + assert_equal ([o.buf_size, o.method], + [1024, "default"]) def test_hfc(self): o = specdesc("hfc") @@ -21,7 +18,7 @@ class aubio_specdesc(TestCase): assert_equal( 0., o(c)) a = arange(c.length, dtype='float32') c.norm = a - assert_equal (a, c.norm[0]) + assert_equal (a, c.norm) assert_equal ( sum(a*(a+1)), o(c)) def test_complex(self): @@ -30,7 +27,7 @@ class aubio_specdesc(TestCase): assert_equal( 0., o(c)) a = arange(c.length, dtype='float32') c.norm = a - assert_equal (a, c.norm[0]) + assert_equal (a, c.norm) # the previous run was on zeros, so previous frames are still 0 # so we have sqrt ( abs ( r2 ^ 2) ) == r2 assert_equal ( sum(a), o(c)) diff --git a/interfaces/python/test_phasevoc.py b/interfaces/python/test_phasevoc.py index c1fc7148..e1153d50 100644 --- a/interfaces/python/test_phasevoc.py +++ b/interfaces/python/test_phasevoc.py @@ -1,5 +1,6 @@ from numpy.testing import TestCase, run_module_suite from numpy.testing import assert_equal, assert_almost_equal +from aubio import fvec from _aubio import * from numpy import array, shape @@ -25,13 +26,13 @@ class aubio_pvoc_test_case(TestCase): def test_steps_two_channels(self): """ check the resynthesis of steps is correct """ - f = pvoc(1024, 512, 2) - t1 = fvec(512, 2) - t2 = fvec(512, 2) + f = pvoc(1024, 512) + t1 = fvec(512) + t2 = fvec(512) # positive step in first channel - t1[0][100:200] = .1 + t1[100:200] = .1 # positive step in second channel - t1[1][20:50] = -.1 + t1[20:50] = -.1 s1 = f(t1) r1 = f.rdo(s1) s2 = f(t2) @@ -41,12 +42,11 @@ class aubio_pvoc_test_case(TestCase): def test_steps_three_random_channels(self): from random import random - f = pvoc(64, 16, 3) - t0 = fvec(16, 3) - t1 = fvec(16, 3) - for i in xrange(3): - for j in xrange(16): - t1[i][j] = random() * 2. - 1. + f = pvoc(64, 16) + t0 = fvec(16) + t1 = fvec(16) + for i in xrange(16): + t1[i] = random() * 2. - 1. t2 = f.rdo(f(t1)) t2 = f.rdo(f(t0)) t2 = f.rdo(f(t0))