[0536612] | 1 | from numpy.testing import TestCase, run_module_suite |
---|
| 2 | from numpy.testing import assert_equal, assert_almost_equal |
---|
| 3 | # WARNING: numpy also has an fft object |
---|
| 4 | from _aubio import cvec, specdesc |
---|
| 5 | from numpy import array, shape, arange, zeros, log |
---|
| 6 | from math import pi |
---|
| 7 | |
---|
| 8 | class aubio_specdesc(TestCase): |
---|
| 9 | |
---|
| 10 | def test_members(self): |
---|
| 11 | o = specdesc() |
---|
| 12 | assert_equal ([o.buf_size, o.channels, o.method], |
---|
| 13 | [1024, 1, "default"]) |
---|
| 14 | o = specdesc("complex", 512, 2) |
---|
| 15 | assert_equal ([o.buf_size, o.channels, o.method], |
---|
| 16 | [512, 2, "complex"]) |
---|
| 17 | |
---|
| 18 | def test_hfc(self): |
---|
| 19 | o = specdesc("hfc") |
---|
| 20 | c = cvec() |
---|
| 21 | assert_equal( 0., o(c)) |
---|
| 22 | a = arange(c.length, dtype='float32') |
---|
| 23 | c.norm = a |
---|
| 24 | assert_equal (a, c.norm[0]) |
---|
| 25 | assert_equal ( sum(a*(a+1)), o(c)) |
---|
| 26 | |
---|
| 27 | def test_complex(self): |
---|
| 28 | o = specdesc("complex") |
---|
| 29 | c = cvec() |
---|
| 30 | assert_equal( 0., o(c)) |
---|
| 31 | a = arange(c.length, dtype='float32') |
---|
| 32 | c.norm = a |
---|
| 33 | assert_equal (a, c.norm[0]) |
---|
| 34 | # the previous run was on zeros, so previous frames are still 0 |
---|
| 35 | # so we have sqrt ( abs ( r2 ^ 2) ) == r2 |
---|
| 36 | assert_equal ( sum(a), o(c)) |
---|
| 37 | # second time. c.norm = a, so, r1 = r2, and the euclidian distance is 0 |
---|
| 38 | assert_equal ( 0, o(c)) |
---|
| 39 | |
---|
| 40 | def test_phase(self): |
---|
| 41 | o = specdesc("phase") |
---|
| 42 | c = cvec() |
---|
| 43 | assert_equal( 0., o(c)) |
---|
| 44 | |
---|
| 45 | def test_kl(self): |
---|
| 46 | o = specdesc("kl") |
---|
| 47 | c = cvec() |
---|
| 48 | assert_equal( 0., o(c)) |
---|
| 49 | a = arange(c.length, dtype='float32') |
---|
| 50 | c.norm = a |
---|
| 51 | assert_almost_equal( sum(a * log(1.+ a/1.e-10 ) ) / o(c), 1., decimal=6) |
---|
| 52 | |
---|
| 53 | def test_mkl(self): |
---|
| 54 | o = specdesc("mkl") |
---|
| 55 | c = cvec() |
---|
| 56 | assert_equal( 0., o(c)) |
---|
| 57 | a = arange(c.length, dtype='float32') |
---|
| 58 | c.norm = a |
---|
| 59 | assert_almost_equal( sum(log(1.+ a/1.e-10 ) ) / o(c), 1, decimal=6) |
---|
| 60 | |
---|
| 61 | def test_specflux(self): |
---|
| 62 | o = specdesc("specflux") |
---|
| 63 | c = cvec() |
---|
| 64 | assert_equal( 0., o(c)) |
---|
| 65 | a = arange(c.length, dtype='float32') |
---|
| 66 | c.norm = a |
---|
| 67 | assert_equal( sum(a), o(c)) |
---|
| 68 | assert_equal( 0, o(c)) |
---|
| 69 | c.norm = zeros(c.length, dtype='float32') |
---|
| 70 | assert_equal( 0, o(c)) |
---|
| 71 | |
---|
| 72 | def test_centroid(self): |
---|
| 73 | o = specdesc("centroid") |
---|
| 74 | c = cvec() |
---|
| 75 | # make sure centroid of zeros is zero |
---|
| 76 | assert_equal( 0., o(c)) |
---|
| 77 | a = arange(c.length, dtype='float32') |
---|
| 78 | c.norm = a |
---|
| 79 | centroid = sum(a*a) / sum(a) |
---|
| 80 | assert_almost_equal (centroid, o(c), decimal = 2) |
---|
| 81 | |
---|
| 82 | c.norm = a * .5 |
---|
| 83 | assert_almost_equal (centroid, o(c), decimal = 2) |
---|
| 84 | |
---|
| 85 | def test_spread(self): |
---|
| 86 | o = specdesc("spread") |
---|
| 87 | c = cvec() |
---|
| 88 | assert_equal( 0., o(c)) |
---|
| 89 | a = arange(c.length, dtype='float32') |
---|
| 90 | c.norm = a |
---|
| 91 | centroid = sum(a*a) / sum(a) |
---|
| 92 | spread = sum( (a - centroid)**2 *a) / sum(a) |
---|
| 93 | assert_almost_equal (spread, o(c), decimal = 2) |
---|
| 94 | |
---|
| 95 | c.norm = a * 3 |
---|
| 96 | assert_almost_equal (spread, o(c), decimal = 2) |
---|
| 97 | |
---|
| 98 | def test_skewness(self): |
---|
| 99 | o = specdesc("skewness") |
---|
| 100 | c = cvec() |
---|
| 101 | assert_equal( 0., o(c)) |
---|
| 102 | a = arange(c.length, dtype='float32') |
---|
| 103 | c.norm = a |
---|
| 104 | centroid = sum(a*a) / sum(a) |
---|
| 105 | spread = sum( (a - centroid)**2 *a) / sum(a) |
---|
| 106 | skewness = sum( (a - centroid)**3 *a) / sum(a) / spread **1.5 |
---|
| 107 | assert_almost_equal (skewness, o(c), decimal = 2) |
---|
| 108 | |
---|
| 109 | c.norm = a * 3 |
---|
| 110 | assert_almost_equal (skewness, o(c), decimal = 2) |
---|
| 111 | |
---|
| 112 | def test_kurtosis(self): |
---|
| 113 | o = specdesc("kurtosis") |
---|
| 114 | c = cvec() |
---|
| 115 | assert_equal( 0., o(c)) |
---|
| 116 | a = arange(c.length, dtype='float32') |
---|
| 117 | c.norm = a |
---|
| 118 | centroid = sum(a*a) / sum(a) |
---|
| 119 | spread = sum( (a - centroid)**2 *a) / sum(a) |
---|
| 120 | kurtosis = sum( (a - centroid)**4 *a) / sum(a) / spread **2 |
---|
| 121 | assert_almost_equal (kurtosis, o(c), decimal = 2) |
---|
| 122 | |
---|
| 123 | def test_slope(self): |
---|
| 124 | o = specdesc("slope") |
---|
| 125 | c = cvec() |
---|
| 126 | assert_equal( 0., o(c)) |
---|
| 127 | a = arange(c.length * 2, 0, -2, dtype='float32') |
---|
| 128 | k = arange(c.length, dtype='float32') |
---|
| 129 | c.norm = a |
---|
| 130 | num = len(a) * sum(k*a) - sum(k)*sum(a) |
---|
| 131 | den = (len(a) * sum(k**2) - sum(k)**2) |
---|
| 132 | slope = num/den/sum(a) |
---|
| 133 | assert_almost_equal (slope, o(c), decimal = 5) |
---|
| 134 | |
---|
| 135 | a = arange(0, c.length * 2, +2, dtype='float32') |
---|
| 136 | c.norm = a |
---|
| 137 | num = len(a) * sum(k*a) - sum(k)*sum(a) |
---|
| 138 | den = (len(a) * sum(k**2) - sum(k)**2) |
---|
| 139 | slope = num/den/sum(a) |
---|
| 140 | assert_almost_equal (slope, o(c), decimal = 5) |
---|
| 141 | |
---|
| 142 | a = arange(0, c.length * 2, +2, dtype='float32') |
---|
| 143 | c.norm = a * 2 |
---|
| 144 | assert_almost_equal (slope, o(c), decimal = 5) |
---|
| 145 | |
---|
| 146 | def test_decrease(self): |
---|
| 147 | o = specdesc("decrease") |
---|
| 148 | c = cvec() |
---|
| 149 | assert_equal( 0., o(c)) |
---|
| 150 | a = arange(c.length * 2, 0, -2, dtype='float32') |
---|
| 151 | k = arange(c.length, dtype='float32') |
---|
| 152 | c.norm = a |
---|
| 153 | decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) |
---|
| 154 | assert_almost_equal (decrease, o(c), decimal = 5) |
---|
| 155 | |
---|
| 156 | a = arange(0, c.length * 2, +2, dtype='float32') |
---|
| 157 | c.norm = a |
---|
| 158 | decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) |
---|
| 159 | assert_almost_equal (decrease, o(c), decimal = 5) |
---|
| 160 | |
---|
| 161 | a = arange(0, c.length * 2, +2, dtype='float32') |
---|
| 162 | c.norm = a * 2 |
---|
| 163 | decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) |
---|
| 164 | assert_almost_equal (decrease, o(c), decimal = 5) |
---|
| 165 | |
---|
| 166 | def test_rolloff(self): |
---|
| 167 | o = specdesc("rolloff") |
---|
| 168 | c = cvec() |
---|
| 169 | assert_equal( 0., o(c)) |
---|
| 170 | a = arange(c.length * 2, 0, -2, dtype='float32') |
---|
| 171 | k = arange(c.length, dtype='float32') |
---|
| 172 | c.norm = a |
---|
| 173 | cumsum = .95*sum(a*a) |
---|
| 174 | i = 0; rollsum = 0 |
---|
| 175 | while rollsum < cumsum: |
---|
| 176 | rollsum += a[i]*a[i] |
---|
| 177 | i+=1 |
---|
| 178 | rolloff = i |
---|
| 179 | assert_equal (rolloff, o(c)) |
---|
| 180 | |
---|
| 181 | if __name__ == '__main__': |
---|
| 182 | from unittest import main |
---|
| 183 | main() |
---|