python/tests/test_phasevoc.py: clean up, add 50% overlap test
authorPaul Brossier <piem@piem.org>
Fri, 29 Apr 2016 18:06:36 +0000 (20:06 +0200)
committerPaul Brossier <piem@piem.org>
Fri, 29 Apr 2016 18:06:36 +0000 (20:06 +0200)
python/tests/test_phasevoc.py

index 9e866d6..15a0aa3 100755 (executable)
@@ -1,12 +1,19 @@
 #! /usr/bin/env python
 
-from numpy.testing import TestCase, assert_equal, assert_almost_equal
+from numpy.testing import TestCase, assert_equal, assert_array_less
 from aubio import fvec, cvec, pvoc, float_type
 from numpy import array, shape
 from numpy.random import random
 import numpy as np
 
-precision = 4
+max_sq_error = 1.e-12
+
+def create_sine(hop_s, freq, samplerate):
+    t = np.arange(hop_s).astype(float_type)
+    return np.sin( 2. * np.pi * t * freq / samplerate)
+
+def create_noise(hop_s):
+    return np.random.rand(hop_s).astype(float_type) * 2. - 1.
 
 class aubio_pvoc_test_case(TestCase):
     """ pvoc object test case """
@@ -44,28 +51,45 @@ class aubio_pvoc_test_case(TestCase):
         hop_s = 1024
         ratio = 8
         freq = 445; samplerate = 22050
-        sigin = np.sin( 2. * np.pi * np.arange(hop_s).astype(float_type) * freq / samplerate)
-        r2 = self.reconstruction( sigin, hop_s, ratio)
-        error = ((r2 - sigin)**2).mean()
-        self.assertLessEqual(error , 1.e-3)
+        sigin = create_sine(hop_s, freq, samplerate)
+        self.reconstruction( sigin, hop_s, ratio)
 
     def test_resynth_8_steps(self):
         """ check the resynthesis of is correct with 87.5% overlap """
         hop_s = 1024
         ratio = 8
-        sigin = np.random.rand(hop_s).astype(float_type) * 2. - 1.
-        r2 = self.reconstruction( sigin, hop_s, ratio)
-        error = ((r2 - sigin)**2).mean()
-        self.assertLessEqual(error , 1.e-2)
+        sigin = create_noise(hop_s)
+        self.reconstruction(sigin, hop_s, ratio)
+
+    def test_resynth_4_steps_sine(self):
+        """ check the resynthesis of is correct with 87.5% overlap """
+        hop_s = 1024
+        ratio = 4
+        freq = 445; samplerate = 22050
+        sigin = create_sine(hop_s, freq, samplerate)
+        self.reconstruction(sigin, hop_s, ratio)
 
     def test_resynth_4_steps(self):
         """ check the resynthesis of is correct with 75% overlap """
         hop_s = 1024
         ratio = 4
-        sigin = np.random.rand(hop_s).astype(float_type) * 2. - 1.
-        r2 = self.reconstruction( sigin, hop_s, ratio)
-        error = ((r2 - sigin)**2).mean()
-        self.assertLessEqual(error , 1.e-2)
+        sigin = create_noise(hop_s)
+        self.reconstruction(sigin, hop_s, ratio)
+
+    def test_resynth_2_steps_sine(self):
+        """ check the resynthesis of is correct with 50% overlap """
+        hop_s = 1024
+        ratio = 2
+        freq = 445; samplerate = 22050
+        sigin = create_sine(hop_s, freq, samplerate)
+        self.reconstruction(sigin, hop_s, ratio)
+
+    def test_resynth_2_steps(self):
+        """ check the resynthesis of is correct with 50% overlap """
+        hop_s = 1024
+        ratio = 2
+        sigin = create_noise(hop_s)
+        self.reconstruction(sigin, hop_s, ratio)
 
     def reconstruction(self, sigin, hop_s, ratio):
         buf_s = hop_s * ratio
@@ -74,15 +98,10 @@ class aubio_pvoc_test_case(TestCase):
         r2 = f.rdo( f(sigin) )
         for i in range(1, ratio):
             r2 = f.rdo( f(zeros) )
-
-        # FIXME: if we don't return a copy here, test_phasevoc.py will crash
-        # once in a while
-        return np.copy(r2)
-
-    def plot_this( self, this ):
-        from pylab import semilogy, show
-        semilogy ( this )
-        show ()
+        # compute square errors
+        sq_error = (r2 - sigin)**2
+        # make sure all square errors are less than desired precision
+        assert_array_less(sq_error, max_sq_error)
 
 if __name__ == '__main__':
   from unittest import main