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