source: tests/python/fft.py @ 038852a

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since 038852a was 038852a, checked in by Paul Brossier <piem@piem.org>, 13 years ago

updated fft.py tests, added template for assertCloseEnough

  • Property mode set to 100644
File size: 5.5 KB
Line 
1import math
2
3from template import aubio_unit_template
4
5from aubio.aubiowrapper import *
6
7buf_size = 1024
8channels = 4
9
10class fft_unit(aubio_unit_template):
11
12  def setUp(self):
13    self.o = new_aubio_fft(buf_size, channels)
14
15  def tearDown(self):
16    del_aubio_fft(self.o)
17
18  def test_create(self):
19    """ test creation and deletion of fft object """
20    pass
21
22  def test_do_zeroes(self):
23    """ test aubio_fft_do on zeroes """
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):
28        self.assertCloseEnough(0., fvec_read_sample(input, channel, index))
29    aubio_fft_do(self.o, input, fftgrain)
30    for index in range(buf_size/2+1):
31      for channel in range(channels):
32        self.assertCloseEnough(0., cvec_read_norm(fftgrain, channel, index))
33    for index in range(buf_size/2+1):
34      for channel in range(channels):
35        self.assertCloseEnough(0., cvec_read_phas(fftgrain, channel, index))
36    del fftgrain
37    del input
38
39  def test_rdo_zeroes(self):
40    """ test aubio_fft_rdo on zeroes """
41    fftgrain = new_cvec(buf_size, channels)
42    output    = new_fvec(buf_size, channels)
43    aubio_fft_rdo(self.o, fftgrain, output)
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
51  def test_do_impulse(self):
52    """ test aubio_fft_do with an impulse on one channel """
53    input    = new_fvec(buf_size, channels)
54    fftgrain = new_cvec(buf_size, channels)
55    # write impulse in channel 0, sample 0.
56    some_constant = 0.3412432456
57    fvec_write_sample(input, some_constant, 0, 0)
58    aubio_fft_do(self.o, input, fftgrain)
59    # check norm
60    for index in range(buf_size/2+1):
61      self.assertCloseEnough(some_constant, cvec_read_norm(fftgrain, 0, index))
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
72  def test_do_constant(self):
73    """ test aubio_fft_do with a constant on one channel """
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)
80    aubio_fft_do(self.o, input, fftgrain)
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))
85    # check norm and phase == 0 in first first and last bin of first channel
86    self.assertCloseEnough((buf_size-1)*some_constant, cvec_read_norm(fftgrain, 0, 0))
87    self.assertCloseEnough(0., cvec_read_phas(fftgrain, 0, 0))
88    self.assertCloseEnough(0., cvec_read_norm(fftgrain, 0, buf_size/2+1))
89    self.assertCloseEnough(0., cvec_read_phas(fftgrain, 0, buf_size/2+1))
90    # check unwrap2pi(phas) ~= pi everywhere but in first bin
91    for index in range(1,buf_size/2+1):
92       self.assertCloseEnough(math.pi, aubio_unwrap2pi(cvec_read_phas(fftgrain, 0, index)))
93       self.assertCloseEnough(some_constant, cvec_read_norm(fftgrain, 0, index))
94    del fftgrain
95    del input
96
97  def test_do_impulse_multichannel(self):
98    " test aubio_fft_do on impulse two channels "
99    input    = new_fvec(buf_size, channels)
100    fftgrain = new_cvec(buf_size, channels)
101    # put an impulse in first an last channel, at first and last index
102    fvec_write_sample(input, 1., 0, 0)
103    fvec_write_sample(input, 1., channels-1, 0)
104    aubio_fft_do(self.o, input, fftgrain)
105    # check the norm
106    for index in range(buf_size/2+1):
107      self.assertEqual(1., cvec_read_norm(fftgrain, 0, index))
108    for index in range(buf_size/2+1):
109      for channel in range(1, channels-1):
110        self.assertEqual(0., cvec_read_norm(fftgrain, channel, index))
111    for index in range(buf_size/2+1):
112      self.assertEqual(1., cvec_read_norm(fftgrain, channels-1, index))
113    # check the phase
114    for index in range(buf_size/2+1):
115      for channel in range(channels):
116        self.assertEqual(0., cvec_read_phas(fftgrain, channel, index))
117    del fftgrain
118    del input
119
120  def test_rdo_impulse(self):
121    """ test aubio_fft_rdo on impulse """
122    fftgrain  = new_cvec(buf_size, channels)
123    for channel in range(channels):
124      cvec_write_norm(fftgrain, 1., channel, 0)
125    output    = new_fvec(buf_size, channels)
126    aubio_fft_rdo(self.o, fftgrain, output)
127    for index in range(buf_size/2+1):
128      for channel in range(channels):
129        self.assertCloseEnough(fvec_read_sample(output, channel, index), 1./buf_size)
130    del fftgrain
131    del output
132
133  def test_do_back_and_forth(self):
134    """ test aubio_fft_rdo on a constant """
135    input    = new_fvec(buf_size, channels)
136    output   = new_fvec(buf_size, channels)
137    fftgrain = new_cvec(buf_size, channels)
138    for index in range(buf_size/2+1):
139      for channel in range(channels):
140        fvec_write_sample(input, 0.67, channel, index)
141    aubio_fft_do(self.o, input, fftgrain)
142    aubio_fft_rdo(self.o, fftgrain, output)
143    for index in range(buf_size/2+1):
144      for channel in range(channels):
145        self.assertCloseEnough(0.67, fvec_read_sample(output, channel, index))
146    del fftgrain
147    del output
148
149if __name__ == '__main__': unittest.main()
Note: See TracBrowser for help on using the repository browser.