From 215b33c94cfa35a27be7a11f692bee1b473e2efd Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Fri, 29 Apr 2016 20:06:36 +0200 Subject: [PATCH] python/tests/test_phasevoc.py: clean up, add 50% overlap test --- python/tests/test_phasevoc.py | 65 ++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/python/tests/test_phasevoc.py b/python/tests/test_phasevoc.py index 9e866d6f..15a0aa35 100755 --- a/python/tests/test_phasevoc.py +++ b/python/tests/test_phasevoc.py @@ -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 -- 2.11.0