From ef1f1f316f1d6e4f839292a032286e7c5724c34b Mon Sep 17 00:00:00 2001 From: nwolek Date: Wed, 30 Dec 2015 12:12:47 -0500 Subject: [PATCH] Interpolator: Cosine option is now overloaded and passing tests. issue #71 --- include/core/JamomaInterpolator.h | 16 ++++++++++++---- test/Interpolator/Interpolator.cpp | 22 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/include/core/JamomaInterpolator.h b/include/core/JamomaInterpolator.h index 5d6953d..3c7dad8 100644 --- a/include/core/JamomaInterpolator.h +++ b/include/core/JamomaInterpolator.h @@ -111,8 +111,10 @@ namespace Jamoma { /** Cosine interpolation - @param x0 Sample value at prior integer index - @param x1 Sample value at next integer index + @param x0 Unused sample value + @param x1 Sample value at prior integer index + @param x2 Sample value at next integer index + @param x3 Unused sample value @param delta Fractional location between x0 (delta=0) and x1 (delta=1) @return The interpolated value */ @@ -121,10 +123,16 @@ namespace Jamoma { public: static const int delay = 1; - constexpr T operator()(T x0, T x1, double delta) noexcept { + constexpr T operator()(T x1, T x2, double delta) noexcept { T a = 0.5 * (1.0 - cos(delta * kPi)); - return x0 + a * (x1-x0); + return x1 + a * (x2-x1); } + + constexpr T operator()(T x0, T x1, T x2, T x3, double delta) noexcept { + // NW: ideally we would call the operator above to remain DRY, but I could not get syntax right + T a = 0.5 * (1.0 - cos(delta * kPi)); + return x1 + a * (x2-x1); + } }; diff --git a/test/Interpolator/Interpolator.cpp b/test/Interpolator/Interpolator.cpp index 803f8f1..fec42b3 100644 --- a/test/Interpolator/Interpolator.cpp +++ b/test/Interpolator/Interpolator.cpp @@ -216,10 +216,10 @@ class InterpolatorTest { int badSampleCount = 0; Jamoma::Interpolator::Cosine my_interp; - //auto x0 = -1.0; + auto x0 = -1.0; auto x1 = 2.0; auto x2 = 1.0; - //auto x3 = 4.0; + auto x3 = 4.0; // The following output was generated using the Octave code // in InterpolatorTargetOutput.m by NW @@ -305,6 +305,24 @@ class InterpolatorTest { } mTest->TEST_ASSERT("testCosine produced correct interpolation output", badSampleCount == 0); + + // reset varaiables + badSampleCount = 0; + temp = 0.0; + tempExpected = 0.0; + delta = 0.0; + + for (int i = 0; i < expectedOutputCosine.size(); i++) { + delta = (i + 1.0) / 64.0; + temp = my_interp(x0,x1,x2,x3,delta); + tempExpected = expectedOutputCosine[i]; + if (! mTest->compare(temp, tempExpected, true, 8) ) { + badSampleCount++; + std::cout << "sample " << i << " had a difference of " << std::fabs(temp - tempExpected) << std::endl; + } + } + + mTest->TEST_ASSERT("testCosine overloaded operator produced consistent output", badSampleCount == 0); }