source: tests/python/src/spectral/fft.py @ e6b2a0c

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

tests/python/src/spectral/fft.py: remove abs, test passes in double precision

  • Property mode set to 100644
File size: 5.7 KB
Line 
1from template import aubio_unit_template
2from localaubio import *
3import math
4
5buf_size = 1024
6channels = 4
7
8class fft_unit(aubio_unit_template):
9
10  def setUp(self):
11    self.o = new_aubio_fft(buf_size, channels)
12
13  def tearDown(self):
14    del_aubio_fft(self.o)
15
16  def test_create(self):
17    """ test creation and deletion of fft object """
18    pass
19
20  def test_do_zeroes(self):
21    """ test aubio_fft_do on zeroes """
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):
26        self.assertCloseEnough(0., fvec_read_sample(input, channel, index))
27    aubio_fft_do(self.o, input, fftgrain)
28    for index in range(buf_size/2+1):
29      for channel in range(channels):
30        self.assertCloseEnough(0., cvec_read_norm(fftgrain, channel, index))
31    for index in range(buf_size/2+1):
32      for channel in range(channels):
33        self.assertCloseEnough(0., cvec_read_phas(fftgrain, channel, index))
34    del fftgrain
35    del input
36
37  def test_rdo_zeroes(self):
38    """ test aubio_fft_rdo on zeroes """
39    fftgrain = new_cvec(buf_size, channels)
40    output    = new_fvec(buf_size, channels)
41    aubio_fft_rdo(self.o, fftgrain, output)
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
49  def test_do_impulse(self):
50    """ test aubio_fft_do with an impulse on one channel """
51    input    = new_fvec(buf_size, channels)
52    fftgrain = new_cvec(buf_size, channels)
53    # write impulse in channel 0, sample 0.
54    some_constant = 0.3412432456
55    fvec_write_sample(input, some_constant, 0, 0)
56    aubio_fft_do(self.o, input, fftgrain)
57    # check norm
58    for index in range(buf_size/2+1):
59      self.assertCloseEnough(some_constant, cvec_read_norm(fftgrain, 0, index))
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
70  def test_do_constant(self):
71    """ test aubio_fft_do with a constant on one channel """
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)
78    aubio_fft_do(self.o, input, fftgrain)
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))
83        self.assertEqual(0., cvec_read_phas(fftgrain, channel, index))
84
85    # check norm and phase == 0 in first first and last bin of first channel
86    # check unwrap2pi(phas) ~= pi everywhere but in first and last bin
87    self.assertCloseEnough(0., cvec_read_phas(fftgrain, 0, 0))
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))
91    self.assertCloseEnough(0., cvec_read_phas(fftgrain, 0, buf_size/2+1))
92
93    self.assertCloseEnough((buf_size-1)*some_constant, cvec_read_norm(fftgrain, 0, 0))
94    for index in range(1,buf_size/2+1):
95       self.assertCloseEnough(some_constant, cvec_read_norm(fftgrain, 0, index))
96    self.assertCloseEnough(0., cvec_read_norm(fftgrain, 0, buf_size/2+1))
97
98    del fftgrain
99    del input
100
101  def test_do_impulse_multichannel(self):
102    " test aubio_fft_do on impulse two channels "
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)
108    aubio_fft_do(self.o, input, fftgrain)
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
124  def test_rdo_impulse(self):
125    """ test aubio_fft_rdo on impulse """
126    fftgrain  = new_cvec(buf_size, channels)
127    for channel in range(channels):
128      cvec_write_norm(fftgrain, 1., channel, 0)
129    output    = new_fvec(buf_size, channels)
130    aubio_fft_rdo(self.o, fftgrain, output)
131    for index in range(buf_size/2+1):
132      for channel in range(channels):
133        self.assertCloseEnough(fvec_read_sample(output, channel, index), 1./buf_size)
134    del fftgrain
135    del output
136
137  def test_do_back_and_forth(self):
138    """ test aubio_fft_rdo on a constant """
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)
145    aubio_fft_do(self.o, input, fftgrain)
146    aubio_fft_rdo(self.o, fftgrain, output)
147    for index in range(buf_size/2+1):
148      for channel in range(channels):
149        self.assertCloseEnough(0.67, fvec_read_sample(output, channel, index))
150    del fftgrain
151    del output
152
153if __name__ == '__main__': unittest.main()
Note: See TracBrowser for help on using the repository browser.