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