source: tests/python/src/spectral/fft.py @ 66a150a2

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

move around files in tests/python according to src/ changes

  • Property mode set to 100644
File size: 5.7 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        self.assertEqual(0., cvec_read_phas(fftgrain, channel, index))
86
87    # check norm and phase == 0 in first first and last bin of first channel
88    # check unwrap2pi(phas) ~= pi everywhere but in first and last bin
89    self.assertCloseEnough(0., cvec_read_phas(fftgrain, 0, 0))
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))
93    self.assertCloseEnough(0., cvec_read_phas(fftgrain, 0, buf_size/2+1))
94
95    self.assertCloseEnough((buf_size-1)*some_constant, cvec_read_norm(fftgrain, 0, 0))
96    for index in range(1,buf_size/2+1):
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
100    del fftgrain
101    del input
102
103  def test_do_impulse_multichannel(self):
104    " test aubio_fft_do on impulse two channels "
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)
110    aubio_fft_do(self.o, input, fftgrain)
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
126  def test_rdo_impulse(self):
127    """ test aubio_fft_rdo on impulse """
128    fftgrain  = new_cvec(buf_size, channels)
129    for channel in range(channels):
130      cvec_write_norm(fftgrain, 1., channel, 0)
131    output    = new_fvec(buf_size, channels)
132    aubio_fft_rdo(self.o, fftgrain, output)
133    for index in range(buf_size/2+1):
134      for channel in range(channels):
135        self.assertCloseEnough(fvec_read_sample(output, channel, index), 1./buf_size)
136    del fftgrain
137    del output
138
139  def test_do_back_and_forth(self):
140    """ test aubio_fft_rdo on a constant """
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)
147    aubio_fft_do(self.o, input, fftgrain)
148    aubio_fft_rdo(self.o, fftgrain, output)
149    for index in range(buf_size/2+1):
150      for channel in range(channels):
151        self.assertCloseEnough(0.67, fvec_read_sample(output, channel, index))
152    del fftgrain
153    del output
154
155if __name__ == '__main__': unittest.main()
Note: See TracBrowser for help on using the repository browser.