Interpolate EPU Buffer for smooth results - Emoshape Inc.

C++ Sample code

  • An interpolation function can be useful in order to smooth your results.
    Polling data by TCPIP from your application to the SDK can generate gaps depending on your polling rate.
    We encourage you to smooth the data received by reconstructing the MDMA (Emo-Matric) dynamically in your application
		  
#include 
#include 
#include 
using namespace std;

//======================================================================

//  Returns interpolated value at x from parallel arrays ( xData, yData )
//  Assumes that xData has at least two elements, is sorted and is strictly monotonic increasing
//  boolean argument extrapolate determines behaviour beyond ends of array (if needed)

double interpolate( vector &xData, vector &yData, double x, bool extrapolate )
{
   int size = xData.size();

   int i = 0;                                                                  // find left end of interval for interpolation
   if ( x >= xData[size - 2] )                                                 // special case: beyond right end
   {
      i = size - 2;
   }
   else
   {
      while ( x > xData[i+1] ) i++;
   }
   double xL = xData[i], yL = yData[i], xR = xData[i+1], yR = yData[i+1];      // points on either side (unless beyond ends)
   if ( !extrapolate )                                                         // if beyond ends of array and not extrapolating
   {
      if ( x < xL ) yR = yL;
      if ( x > xR ) yL = yR;
   }

   double dydx = ( yR - yL ) / ( xR - xL );                                    // gradient

   return yL + dydx * ( x - xL );                                              // linear interpolation
}

//======================================================================

int main()
{
   // Original data
   vector xData = { 1, 5, 10, 15, 20 };
   vector yData = { 0.3, 0.5, 0.8, 0.1, 0.14 };

   // Set up some points for interpolation in xVals
   const int NPTS = 20;
   vector xVals, yVals;
   for ( int i = 1; i <= NPTS; i++ ) xVals.push_back( (double)i );

   // Interpolate
   for ( double x : xVals )
   {
      double y = interpolate( xData, yData, x, true );
      yVals.push_back( y );
   }

   // Output
   #define SP << fixed << setw( 15 ) << setprecision( 6 ) <<
   #define NL << '\n'
   cout << "Original data:\n";
   for ( int i = 0; i < xData.size(); i++ ) cout SP xData[i] SP yData[i] NL;
   cout << "\nInterpolated data:\n";
   for ( int i = 0; i < xVals.size(); i++ ) cout SP xVals[i] SP yVals[i] NL;
}

Original data:
	Values		Time
       1.000000       0.300000
       5.000000       0.500000
      10.000000       0.800000
      15.000000       0.100000
      20.000000       0.140000

Interpolated data:
	Values		Time
       1.000000       0.300000
       2.000000       0.350000
       3.000000       0.400000
       4.000000       0.450000
       5.000000       0.500000
       6.000000       0.560000
       7.000000       0.620000
       8.000000       0.680000
       9.000000       0.740000
      10.000000       0.800000
      11.000000       0.660000
      12.000000       0.520000
      13.000000       0.380000
      14.000000       0.240000
      15.000000       0.100000
      16.000000       0.108000
      17.000000       0.116000
      18.000000       0.124000
      19.000000       0.132000
      20.000000       0.140000