python/tests/test_phasevoc.py: add more tests
[aubio.git] / python / tests / test_phasevoc.py
1 #! /usr/bin/env python
2
3 from numpy.testing import TestCase, assert_equal, assert_almost_equal
4 from aubio import fvec, cvec, pvoc, float_type
5 from numpy import array, shape
6 from numpy.random import random
7 import numpy as np
8
9 precision = 4
10
11 class aubio_pvoc_test_case(TestCase):
12     """ pvoc object test case """
13
14     def test_members_automatic_sizes_default(self):
15         """ check object creation with default parameters """
16         f = pvoc()
17         assert_equal ([f.win_s, f.hop_s], [1024, 512])
18
19     def test_members_unnamed_params(self):
20         """ check object creation with unnamed parameters """
21         f = pvoc(2048, 128)
22         assert_equal ([f.win_s, f.hop_s], [2048, 128])
23
24     def test_members_named_params(self):
25         """ check object creation with named parameters """
26         f = pvoc(hop_s = 128, win_s = 2048)
27         assert_equal ([f.win_s, f.hop_s], [2048, 128])
28
29     def test_zeros(self):
30         """ check the resynthesis of zeros gives zeros """
31         win_s, hop_s = 1024, 256
32         f = pvoc (win_s, hop_s)
33         t = fvec (hop_s)
34         for time in range( int ( 4 * win_s / hop_s ) ):
35             s = f(t)
36             r = f.rdo(s)
37             assert_equal ( array(t), 0)
38             assert_equal ( s.norm, 0)
39             assert_equal ( s.phas, 0)
40             assert_equal ( r, 0)
41
42     def test_resynth_8_steps_sine(self):
43         """ check the resynthesis of is correct with 87.5% overlap """
44         hop_s = 1024
45         ratio = 8
46         freq = 445; samplerate = 22050
47         sigin = np.sin( 2. * np.pi * np.arange(hop_s).astype(float_type) * freq / samplerate)
48         r2 = self.reconstruction( sigin, hop_s, ratio)
49         error = ((r2 - sigin)**2).mean()
50         self.assertLessEqual(error , 1.e-3)
51
52     def test_resynth_8_steps(self):
53         """ check the resynthesis of is correct with 87.5% overlap """
54         hop_s = 1024
55         ratio = 8
56         sigin = np.random.rand(hop_s).astype(float_type) * 2. - 1.
57         r2 = self.reconstruction( sigin, hop_s, ratio)
58         error = ((r2 - sigin)**2).mean()
59         self.assertLessEqual(error , 1.e-2)
60
61     def test_resynth_4_steps(self):
62         """ check the resynthesis of is correct with 75% overlap """
63         hop_s = 1024
64         ratio = 4
65         sigin = np.random.rand(hop_s).astype(float_type) * 2. - 1.
66         r2 = self.reconstruction( sigin, hop_s, ratio)
67         error = ((r2 - sigin)**2).mean()
68         self.assertLessEqual(error , 1.e-2)
69
70     def reconstruction(self, sigin, hop_s, ratio):
71         buf_s = hop_s * ratio
72         f = pvoc(buf_s, hop_s)
73         zeros = fvec(hop_s)
74         r2 = f.rdo( f(sigin) )
75         for i in range(1, ratio):
76             r2 = f.rdo( f(zeros) )
77         return r2
78
79     def plot_this( self, this ):
80         from pylab import semilogy, show
81         semilogy ( this )
82         show ()
83
84 if __name__ == '__main__':
85   from unittest import main
86   main()
87