Skip to content

Commit

Permalink
Interpolator: Cosine option is now overloaded and passing tests. issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nwolek committed Dec 30, 2015
1 parent 2d6fa09 commit ef1f1f3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
16 changes: 12 additions & 4 deletions include/core/JamomaInterpolator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
}
};


Expand Down
22 changes: 20 additions & 2 deletions test/Interpolator/Interpolator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ class InterpolatorTest {
int badSampleCount = 0;
Jamoma::Interpolator::Cosine<Jamoma::Sample> 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
Expand Down Expand Up @@ -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);
}


Expand Down

0 comments on commit ef1f1f3

Please sign in to comment.