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

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

updated fft.py tests, added template for assertCloseEnough

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[398c1c5]1import math
2
[038852a]3from template import aubio_unit_template
4
[398c1c5]5from aubio.aubiowrapper import *
6
[038852a]7buf_size = 1024
[2b3280a]8channels = 4
9
[038852a]10class 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.
[2b3280a]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 """
[2b3280a]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)
[2b3280a]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
[038852a]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))
[2b3280a]90    # check unwrap2pi(phas) ~= pi everywhere but in first bin
91    for index in range(1,buf_size/2+1):
[038852a]92       self.assertCloseEnough(math.pi, aubio_unwrap2pi(cvec_read_phas(fftgrain, 0, index)))
93       self.assertCloseEnough(some_constant, cvec_read_norm(fftgrain, 0, index))
[2b3280a]94    del fftgrain
95    del input
96
[038852a]97  def test_do_impulse_multichannel(self):
98    " test aubio_fft_do on impulse two channels "
[398c1c5]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)
[038852a]104    aubio_fft_do(self.o, input, fftgrain)
[398c1c5]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
[038852a]120  def test_rdo_impulse(self):
121    """ test aubio_fft_rdo on impulse """
[398c1c5]122    fftgrain  = new_cvec(buf_size, channels)
[2b3280a]123    for channel in range(channels):
124      cvec_write_norm(fftgrain, 1., channel, 0)
[398c1c5]125    output    = new_fvec(buf_size, channels)
[038852a]126    aubio_fft_rdo(self.o, fftgrain, output)
[398c1c5]127    for index in range(buf_size/2+1):
128      for channel in range(channels):
[038852a]129        self.assertCloseEnough(fvec_read_sample(output, channel, index), 1./buf_size)
[398c1c5]130    del fftgrain
131    del output
132
[038852a]133  def test_do_back_and_forth(self):
134    """ test aubio_fft_rdo on a constant """
[2d8880d5]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)
[038852a]141    aubio_fft_do(self.o, input, fftgrain)
142    aubio_fft_rdo(self.o, fftgrain, output)
[2d8880d5]143    for index in range(buf_size/2+1):
144      for channel in range(channels):
[038852a]145        self.assertCloseEnough(0.67, fvec_read_sample(output, channel, index))
[2d8880d5]146    del fftgrain
147    del output
148
[398c1c5]149if __name__ == '__main__': unittest.main()
Note: See TracBrowser for help on using the repository browser.