[75e715f] | 1 | #! /usr/bin/env python |
---|
[1ebf8770] | 2 | |
---|
[215b33c] | 3 | from numpy.testing import TestCase, assert_equal, assert_array_less |
---|
[ac65a2f] | 4 | from aubio import fvec, cvec, pvoc, float_type |
---|
[fb434c4] | 5 | from nose2.tools import params |
---|
[ac65a2f] | 6 | import numpy as np |
---|
[467122d] | 7 | |
---|
[fb434c4] | 8 | if float_type == 'float32': |
---|
| 9 | max_sq_error = 1.e-12 |
---|
| 10 | else: |
---|
| 11 | max_sq_error = 1.e-29 |
---|
[215b33c] | 12 | |
---|
| 13 | def create_sine(hop_s, freq, samplerate): |
---|
| 14 | t = np.arange(hop_s).astype(float_type) |
---|
[fb434c4] | 15 | return np.sin( 2. * np.pi * freq * t / float(samplerate)) |
---|
[215b33c] | 16 | |
---|
| 17 | def create_noise(hop_s): |
---|
| 18 | return np.random.rand(hop_s).astype(float_type) * 2. - 1. |
---|
[0536612] | 19 | |
---|
| 20 | class aubio_pvoc_test_case(TestCase): |
---|
[467122d] | 21 | """ pvoc object test case """ |
---|
| 22 | |
---|
| 23 | def test_members_automatic_sizes_default(self): |
---|
| 24 | """ check object creation with default parameters """ |
---|
| 25 | f = pvoc() |
---|
| 26 | assert_equal ([f.win_s, f.hop_s], [1024, 512]) |
---|
[0536612] | 27 | |
---|
[467122d] | 28 | def test_members_unnamed_params(self): |
---|
| 29 | """ check object creation with unnamed parameters """ |
---|
| 30 | f = pvoc(2048, 128) |
---|
| 31 | assert_equal ([f.win_s, f.hop_s], [2048, 128]) |
---|
[86ad546] | 32 | |
---|
[467122d] | 33 | def test_members_named_params(self): |
---|
| 34 | """ check object creation with named parameters """ |
---|
| 35 | f = pvoc(hop_s = 128, win_s = 2048) |
---|
| 36 | assert_equal ([f.win_s, f.hop_s], [2048, 128]) |
---|
[0536612] | 37 | |
---|
[467122d] | 38 | def test_zeros(self): |
---|
| 39 | """ check the resynthesis of zeros gives zeros """ |
---|
| 40 | win_s, hop_s = 1024, 256 |
---|
| 41 | f = pvoc (win_s, hop_s) |
---|
| 42 | t = fvec (hop_s) |
---|
[376d5e9] | 43 | for time in range( int ( 4 * win_s / hop_s ) ): |
---|
[467122d] | 44 | s = f(t) |
---|
| 45 | r = f.rdo(s) |
---|
[fb434c4] | 46 | assert_equal ( t, 0.) |
---|
| 47 | assert_equal ( s.norm, 0.) |
---|
| 48 | assert_equal ( s.phas, 0.) |
---|
| 49 | assert_equal ( r, 0.) |
---|
[0536612] | 50 | |
---|
[fb434c4] | 51 | @params( |
---|
| 52 | ( 256, 8), |
---|
| 53 | ( 256, 4), |
---|
| 54 | ( 256, 2), |
---|
| 55 | ( 512, 8), |
---|
| 56 | ( 512, 4), |
---|
| 57 | ( 512, 2), |
---|
| 58 | (1024, 8), |
---|
| 59 | (1024, 4), |
---|
| 60 | (1024, 2), |
---|
| 61 | (2048, 8), |
---|
| 62 | (2048, 4), |
---|
| 63 | (2048, 2), |
---|
| 64 | (4096, 8), |
---|
| 65 | (4096, 4), |
---|
| 66 | (4096, 2), |
---|
| 67 | (8192, 8), |
---|
| 68 | (8192, 4), |
---|
| 69 | (8192, 2), |
---|
| 70 | ) |
---|
| 71 | def test_resynth_steps_noise(self, hop_s, ratio): |
---|
| 72 | """ check the resynthesis of a random signal is correct """ |
---|
| 73 | sigin = create_noise(hop_s) |
---|
| 74 | self.reconstruction(sigin, hop_s, ratio) |
---|
| 75 | |
---|
| 76 | @params( |
---|
| 77 | (44100, 256, 8, 441), |
---|
| 78 | (44100, 256, 4, 1203), |
---|
| 79 | (44100, 256, 2, 3045), |
---|
| 80 | (44100, 512, 8, 445), |
---|
| 81 | (44100, 512, 4, 445), |
---|
| 82 | (44100, 512, 2, 445), |
---|
| 83 | (44100, 1024, 8, 445), |
---|
| 84 | (44100, 1024, 4, 445), |
---|
| 85 | (44100, 1024, 2, 445), |
---|
| 86 | ( 8000, 1024, 2, 445), |
---|
| 87 | (22050, 1024, 2, 445), |
---|
| 88 | (22050, 256, 8, 445), |
---|
| 89 | (96000, 1024, 8, 47000), |
---|
| 90 | (96000, 1024, 8, 20), |
---|
| 91 | ) |
---|
| 92 | def test_resynth_steps_sine(self, samplerate, hop_s, ratio, freq): |
---|
| 93 | """ check the resynthesis of a sine is correct """ |
---|
| 94 | sigin = create_sine(hop_s, freq, samplerate) |
---|
| 95 | self.reconstruction(sigin, hop_s, ratio) |
---|
| 96 | |
---|
[6937842] | 97 | def reconstruction(self, sigin, hop_s, ratio): |
---|
[ac65a2f] | 98 | buf_s = hop_s * ratio |
---|
[467122d] | 99 | f = pvoc(buf_s, hop_s) |
---|
[ac65a2f] | 100 | zeros = fvec(hop_s) |
---|
| 101 | r2 = f.rdo( f(sigin) ) |
---|
| 102 | for i in range(1, ratio): |
---|
| 103 | r2 = f.rdo( f(zeros) ) |
---|
[215b33c] | 104 | # compute square errors |
---|
| 105 | sq_error = (r2 - sigin)**2 |
---|
| 106 | # make sure all square errors are less than desired precision |
---|
| 107 | assert_array_less(sq_error, max_sq_error) |
---|
[0536612] | 108 | |
---|
[dfada33] | 109 | def test_large_input_timegrain(self): |
---|
| 110 | win_s = 1024 |
---|
| 111 | f = pvoc(win_s) |
---|
| 112 | t = fvec(win_s + 1) |
---|
| 113 | with self.assertRaises(ValueError): |
---|
| 114 | f(t) |
---|
| 115 | |
---|
| 116 | def test_small_input_timegrain(self): |
---|
| 117 | win_s = 1024 |
---|
| 118 | f = pvoc(win_s) |
---|
| 119 | t = fvec(1) |
---|
| 120 | with self.assertRaises(ValueError): |
---|
| 121 | f(t) |
---|
| 122 | |
---|
| 123 | def test_large_input_fftgrain(self): |
---|
| 124 | win_s = 1024 |
---|
| 125 | f = pvoc(win_s) |
---|
| 126 | s = cvec(win_s + 5) |
---|
| 127 | with self.assertRaises(ValueError): |
---|
| 128 | f.rdo(s) |
---|
| 129 | |
---|
| 130 | def test_small_input_fftgrain(self): |
---|
| 131 | win_s = 1024 |
---|
| 132 | f = pvoc(win_s) |
---|
| 133 | s = cvec(16) |
---|
| 134 | with self.assertRaises(ValueError): |
---|
| 135 | f.rdo(s) |
---|
| 136 | |
---|
[0536612] | 137 | if __name__ == '__main__': |
---|
[fb434c4] | 138 | from nose2 import main |
---|
| 139 | main() |
---|
[0536612] | 140 | |
---|