source: tests/python/fft.py @ 2b3280a

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 2b3280a was 2b3280a, checked in by Paul Brossier <piem@piem.org>, 16 years ago

fft.py: some more tests

  • Property mode set to 100644
File size: 5.7 KB
Line 
1import unittest
2import math
3
4from aubio.aubiowrapper import *
5
6buf_size = 8092 
7channels = 4
8
9precision = 6
10
11class aubio_mfft_test_case(unittest.TestCase):
12
13  def setUp(self):
14    self.o = new_aubio_mfft(buf_size, channels)
15
16  def tearDown(self):
17    del_aubio_mfft(self.o)
18
19  def test_create(self):
20    """ test creation and deletion of fft object """
21    pass
22
23  def test_aubio_mfft_do_zeroes(self):
24    """ test aubio_mfft_do on zeroes """
25    input    = new_fvec(buf_size, channels)
26    fftgrain = new_cvec(buf_size, channels)
27    for index in range(buf_size):
28      for channel in range(channels):
29        self.assertEqual(0., fvec_read_sample(input, channel, index))
30    aubio_mfft_do(self.o, input, fftgrain)
31    for index in range(buf_size/2+1):
32      for channel in range(channels):
33        self.assertEqual(0., cvec_read_norm(fftgrain, channel, index))
34    for index in range(buf_size/2+1):
35      for channel in range(channels):
36        self.assertEqual(0., cvec_read_phas(fftgrain, channel, index))
37    del fftgrain
38    del input
39
40  def test_aubio_mfft_rdo_zeroes(self):
41    """ test aubio_mfft_rdo on zeroes """
42    fftgrain = new_cvec(buf_size, channels)
43    output    = new_fvec(buf_size, channels)
44    aubio_mfft_rdo(self.o, fftgrain, output)
45    # check output
46    for index in range(buf_size):
47      for channel in range(channels):
48        self.assertEqual(0., fvec_read_sample(output, channel, index))
49    del fftgrain
50    del output
51
52  def test_aubio_mfft_do_impulse(self):
53    """ test aubio_mfft_do with an impulse on one channel """
54    input    = new_fvec(buf_size, channels)
55    fftgrain = new_cvec(buf_size, channels)
56    # write impulse in channel 0, sample 0.
57    some_constant = 0.3412432456
58    fvec_write_sample(input, some_constant, 0, 0)
59    aubio_mfft_do(self.o, input, fftgrain)
60    # check norm
61    for index in range(buf_size/2+1):
62      self.assertAlmostEqual(some_constant, cvec_read_norm(fftgrain, 0, index), precision)
63    for index in range(buf_size/2+1):
64      for channel in range(1, channels):
65        self.assertEqual(0., cvec_read_norm(fftgrain, channel, index))
66    # check phas
67    for index in range(buf_size/2+1):
68      for channel in range(channels):
69        self.assertEqual(0., cvec_read_phas(fftgrain, channel, index))
70    del fftgrain
71    del input
72
73  def test_aubio_mfft_do_constant(self):
74    """ test aubio_mfft_do with a constant on one channel """
75    input    = new_fvec(buf_size, channels)
76    fftgrain = new_cvec(buf_size, channels)
77    # write impulse in channel 0, sample 0.
78    some_constant = 0.003412432456
79    for index in range(1,buf_size):
80      fvec_write_sample(input, some_constant, 0, index)
81    aubio_mfft_do(self.o, input, fftgrain)
82    # check norm and phase == 0 in all other channels
83    for index in range(buf_size/2+1):
84      for channel in range(1, channels):
85        self.assertEqual(0., cvec_read_norm(fftgrain, channel, index))
86    # check norm and phase == 0 in first first and last bin of first channel
87    self.assertAlmostEqual((buf_size-1)*some_constant, cvec_read_norm(fftgrain, 0, 0), precision)
88    self.assertEqual(0., cvec_read_phas(fftgrain, 0, 0))
89    self.assertEqual(0., cvec_read_norm(fftgrain, 0, buf_size/2+1))
90    self.assertEqual(0., cvec_read_phas(fftgrain, 0, buf_size/2+1))
91    # check unwrap2pi(phas) ~= pi everywhere but in first bin
92    for index in range(1,buf_size/2+1):
93       self.assertAlmostEqual ( math.pi, aubio_unwrap2pi(cvec_read_phas(fftgrain, 0, index)), precision)
94       self.assertAlmostEqual(some_constant, cvec_read_norm(fftgrain, 0, index), precision)
95    del fftgrain
96    del input
97
98  def test_aubio_mfft_do_impulse_multichannel(self):
99    " test aubio_mfft_do on impulse two channels "
100    input    = new_fvec(buf_size, channels)
101    fftgrain = new_cvec(buf_size, channels)
102    # put an impulse in first an last channel, at first and last index
103    fvec_write_sample(input, 1., 0, 0)
104    fvec_write_sample(input, 1., channels-1, 0)
105    aubio_mfft_do(self.o, input, fftgrain)
106    # check the norm
107    for index in range(buf_size/2+1):
108      self.assertEqual(1., cvec_read_norm(fftgrain, 0, index))
109    for index in range(buf_size/2+1):
110      for channel in range(1, channels-1):
111        self.assertEqual(0., cvec_read_norm(fftgrain, channel, index))
112    for index in range(buf_size/2+1):
113      self.assertEqual(1., cvec_read_norm(fftgrain, channels-1, index))
114    # check the phase
115    for index in range(buf_size/2+1):
116      for channel in range(channels):
117        self.assertEqual(0., cvec_read_phas(fftgrain, channel, index))
118    del fftgrain
119    del input
120
121  def test_aubio_mfft_rdo_impulse(self):
122    """ test aubio_mfft_rdo on impulse """
123    fftgrain  = new_cvec(buf_size, channels)
124    for channel in range(channels):
125      cvec_write_norm(fftgrain, 1., channel, 0)
126    output    = new_fvec(buf_size, channels)
127    aubio_mfft_rdo(self.o, fftgrain, output)
128    for index in range(buf_size/2+1):
129      for channel in range(channels):
130        self.assertAlmostEqual(fvec_read_sample(output, channel, index), 1./buf_size, precision)
131    del fftgrain
132    del output
133
134  def test_aubio_mfft_do_back_and_forth(self):
135    """ test aubio_mfft_rdo on a constant """
136    input    = new_fvec(buf_size, channels)
137    output   = new_fvec(buf_size, channels)
138    fftgrain = new_cvec(buf_size, channels)
139    for index in range(buf_size/2+1):
140      for channel in range(channels):
141        fvec_write_sample(input, 0.67, channel, index)
142    aubio_mfft_do(self.o, input, fftgrain)
143    aubio_mfft_rdo(self.o, fftgrain, output)
144    for index in range(buf_size/2+1):
145      for channel in range(channels):
146        self.assertAlmostEqual(fvec_read_sample(output, channel, index), 0.67, precision)
147    del fftgrain
148    del output
149
150if __name__ == '__main__': unittest.main()
Note: See TracBrowser for help on using the repository browser.