[398c1c5] | 1 | import math |
---|
| 2 | |
---|
[038852a] | 3 | from template import aubio_unit_template |
---|
| 4 | |
---|
[398c1c5] | 5 | from aubio.aubiowrapper import * |
---|
| 6 | |
---|
[038852a] | 7 | buf_size = 1024 |
---|
[c721874] | 8 | channels = 4 |
---|
| 9 | |
---|
[038852a] | 10 | class fft_unit(aubio_unit_template): |
---|
[398c1c5] | 11 | |
---|
| 12 | def setUp(self): |
---|
[038852a] | 13 | self.o = new_aubio_fft(buf_size, channels) |
---|
[398c1c5] | 14 | |
---|
| 15 | def tearDown(self): |
---|
[038852a] | 16 | del_aubio_fft(self.o) |
---|
[398c1c5] | 17 | |
---|
[2d8880d5] | 18 | def test_create(self): |
---|
| 19 | """ test creation and deletion of fft object """ |
---|
[398c1c5] | 20 | pass |
---|
| 21 | |
---|
[038852a] | 22 | def test_do_zeroes(self): |
---|
| 23 | """ test aubio_fft_do on zeroes """ |
---|
[398c1c5] | 24 | input = new_fvec(buf_size, channels) |
---|
| 25 | fftgrain = new_cvec(buf_size, channels) |
---|
| 26 | for index in range(buf_size): |
---|
| 27 | for channel in range(channels): |
---|
[038852a] | 28 | self.assertCloseEnough(0., fvec_read_sample(input, channel, index)) |
---|
| 29 | aubio_fft_do(self.o, input, fftgrain) |
---|
[398c1c5] | 30 | for index in range(buf_size/2+1): |
---|
| 31 | for channel in range(channels): |
---|
[038852a] | 32 | self.assertCloseEnough(0., cvec_read_norm(fftgrain, channel, index)) |
---|
[398c1c5] | 33 | for index in range(buf_size/2+1): |
---|
| 34 | for channel in range(channels): |
---|
[038852a] | 35 | self.assertCloseEnough(0., cvec_read_phas(fftgrain, channel, index)) |
---|
[398c1c5] | 36 | del fftgrain |
---|
| 37 | del input |
---|
| 38 | |
---|
[038852a] | 39 | def test_rdo_zeroes(self): |
---|
| 40 | """ test aubio_fft_rdo on zeroes """ |
---|
[398c1c5] | 41 | fftgrain = new_cvec(buf_size, channels) |
---|
| 42 | output = new_fvec(buf_size, channels) |
---|
[038852a] | 43 | aubio_fft_rdo(self.o, fftgrain, output) |
---|
[398c1c5] | 44 | # check output |
---|
| 45 | for index in range(buf_size): |
---|
| 46 | for channel in range(channels): |
---|
| 47 | self.assertEqual(0., fvec_read_sample(output, channel, index)) |
---|
| 48 | del fftgrain |
---|
| 49 | del output |
---|
| 50 | |
---|
[038852a] | 51 | def test_do_impulse(self): |
---|
| 52 | """ test aubio_fft_do with an impulse on one channel """ |
---|
[398c1c5] | 53 | input = new_fvec(buf_size, channels) |
---|
| 54 | fftgrain = new_cvec(buf_size, channels) |
---|
| 55 | # write impulse in channel 0, sample 0. |
---|
[c721874] | 56 | some_constant = 0.3412432456 |
---|
| 57 | fvec_write_sample(input, some_constant, 0, 0) |
---|
[038852a] | 58 | aubio_fft_do(self.o, input, fftgrain) |
---|
[398c1c5] | 59 | # check norm |
---|
| 60 | for index in range(buf_size/2+1): |
---|
[038852a] | 61 | self.assertCloseEnough(some_constant, cvec_read_norm(fftgrain, 0, index)) |
---|
[398c1c5] | 62 | for index in range(buf_size/2+1): |
---|
| 63 | for channel in range(1, channels): |
---|
| 64 | self.assertEqual(0., cvec_read_norm(fftgrain, channel, index)) |
---|
| 65 | # check phas |
---|
| 66 | for index in range(buf_size/2+1): |
---|
| 67 | for channel in range(channels): |
---|
| 68 | self.assertEqual(0., cvec_read_phas(fftgrain, channel, index)) |
---|
| 69 | del fftgrain |
---|
| 70 | del input |
---|
| 71 | |
---|
[038852a] | 72 | def test_do_constant(self): |
---|
| 73 | """ test aubio_fft_do with a constant on one channel """ |
---|
[c721874] | 74 | input = new_fvec(buf_size, channels) |
---|
| 75 | fftgrain = new_cvec(buf_size, channels) |
---|
| 76 | # write impulse in channel 0, sample 0. |
---|
| 77 | some_constant = 0.003412432456 |
---|
| 78 | for index in range(1,buf_size): |
---|
| 79 | fvec_write_sample(input, some_constant, 0, index) |
---|
[038852a] | 80 | aubio_fft_do(self.o, input, fftgrain) |
---|
[c721874] | 81 | # check norm and phase == 0 in all other channels |
---|
| 82 | for index in range(buf_size/2+1): |
---|
| 83 | for channel in range(1, channels): |
---|
| 84 | self.assertEqual(0., cvec_read_norm(fftgrain, channel, index)) |
---|
[d57d1de] | 85 | self.assertEqual(0., cvec_read_phas(fftgrain, channel, index)) |
---|
| 86 | |
---|
[c721874] | 87 | # check norm and phase == 0 in first first and last bin of first channel |
---|
[d57d1de] | 88 | # check unwrap2pi(phas) ~= pi everywhere but in first and last bin |
---|
[038852a] | 89 | self.assertCloseEnough(0., cvec_read_phas(fftgrain, 0, 0)) |
---|
[d57d1de] | 90 | for index in range(1,buf_size/2): |
---|
| 91 | self.assertCloseEnough(math.pi, aubio_unwrap2pi(cvec_read_phas(fftgrain, 0, index))) |
---|
| 92 | self.assertCloseEnough(0., cvec_read_phas(fftgrain, 0, buf_size/2)) |
---|
[038852a] | 93 | self.assertCloseEnough(0., cvec_read_phas(fftgrain, 0, buf_size/2+1)) |
---|
[d57d1de] | 94 | |
---|
| 95 | self.assertCloseEnough((buf_size-1)*some_constant, cvec_read_norm(fftgrain, 0, 0)) |
---|
[c721874] | 96 | for index in range(1,buf_size/2+1): |
---|
[d57d1de] | 97 | self.assertCloseEnough(some_constant, abs(cvec_read_norm(fftgrain, 0, index))) |
---|
| 98 | self.assertCloseEnough(0., cvec_read_norm(fftgrain, 0, buf_size/2+1)) |
---|
| 99 | |
---|
[c721874] | 100 | del fftgrain |
---|
| 101 | del input |
---|
| 102 | |
---|
[038852a] | 103 | def test_do_impulse_multichannel(self): |
---|
| 104 | " test aubio_fft_do on impulse two channels " |
---|
[398c1c5] | 105 | input = new_fvec(buf_size, channels) |
---|
| 106 | fftgrain = new_cvec(buf_size, channels) |
---|
| 107 | # put an impulse in first an last channel, at first and last index |
---|
| 108 | fvec_write_sample(input, 1., 0, 0) |
---|
| 109 | fvec_write_sample(input, 1., channels-1, 0) |
---|
[038852a] | 110 | aubio_fft_do(self.o, input, fftgrain) |
---|
[398c1c5] | 111 | # check the norm |
---|
| 112 | for index in range(buf_size/2+1): |
---|
| 113 | self.assertEqual(1., cvec_read_norm(fftgrain, 0, index)) |
---|
| 114 | for index in range(buf_size/2+1): |
---|
| 115 | for channel in range(1, channels-1): |
---|
| 116 | self.assertEqual(0., cvec_read_norm(fftgrain, channel, index)) |
---|
| 117 | for index in range(buf_size/2+1): |
---|
| 118 | self.assertEqual(1., cvec_read_norm(fftgrain, channels-1, index)) |
---|
| 119 | # check the phase |
---|
| 120 | for index in range(buf_size/2+1): |
---|
| 121 | for channel in range(channels): |
---|
| 122 | self.assertEqual(0., cvec_read_phas(fftgrain, channel, index)) |
---|
| 123 | del fftgrain |
---|
| 124 | del input |
---|
| 125 | |
---|
[038852a] | 126 | def test_rdo_impulse(self): |
---|
| 127 | """ test aubio_fft_rdo on impulse """ |
---|
[398c1c5] | 128 | fftgrain = new_cvec(buf_size, channels) |
---|
[c721874] | 129 | for channel in range(channels): |
---|
| 130 | cvec_write_norm(fftgrain, 1., channel, 0) |
---|
[398c1c5] | 131 | output = new_fvec(buf_size, channels) |
---|
[038852a] | 132 | aubio_fft_rdo(self.o, fftgrain, output) |
---|
[398c1c5] | 133 | for index in range(buf_size/2+1): |
---|
| 134 | for channel in range(channels): |
---|
[038852a] | 135 | self.assertCloseEnough(fvec_read_sample(output, channel, index), 1./buf_size) |
---|
[398c1c5] | 136 | del fftgrain |
---|
| 137 | del output |
---|
| 138 | |
---|
[038852a] | 139 | def test_do_back_and_forth(self): |
---|
| 140 | """ test aubio_fft_rdo on a constant """ |
---|
[2d8880d5] | 141 | input = new_fvec(buf_size, channels) |
---|
| 142 | output = new_fvec(buf_size, channels) |
---|
| 143 | fftgrain = new_cvec(buf_size, channels) |
---|
| 144 | for index in range(buf_size/2+1): |
---|
| 145 | for channel in range(channels): |
---|
| 146 | fvec_write_sample(input, 0.67, channel, index) |
---|
[038852a] | 147 | aubio_fft_do(self.o, input, fftgrain) |
---|
| 148 | aubio_fft_rdo(self.o, fftgrain, output) |
---|
[2d8880d5] | 149 | for index in range(buf_size/2+1): |
---|
| 150 | for channel in range(channels): |
---|
[038852a] | 151 | self.assertCloseEnough(0.67, fvec_read_sample(output, channel, index)) |
---|
[2d8880d5] | 152 | del fftgrain |
---|
| 153 | del output |
---|
| 154 | |
---|
[398c1c5] | 155 | if __name__ == '__main__': unittest.main() |
---|