Interpolation: Fix "trend" extrapolation for value outside of range on high end.

This commit is contained in:
Kristian Bendiksen
2024-04-05 11:28:02 +02:00
parent bce4dcfe54
commit bebf59c309
3 changed files with 31 additions and 1 deletions

View File

@@ -103,7 +103,22 @@ double RiaInterpolationTools::linear( const std::vector<double>& x, const std::v
//--------------------------------------------------------------------------------------------------
double RiaInterpolationTools::extrapolate( const std::vector<double>& x, const std::vector<double>& y, double value )
{
return y[0] + ( value - x[0] ) / ( x[1] - x[0] ) * ( y[1] - y[0] );
if ( value <= x[0] )
return extrapolate( x[0], y[0], x[1], y[1], value );
else
{
size_t lastIndex = x.size() - 1;
size_t secondLastIndex = x.size() - 2;
return extrapolate( x[secondLastIndex], y[secondLastIndex], x[lastIndex], y[lastIndex], value );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaInterpolationTools::extrapolate( double x0, double y0, double x1, double y1, double value )
{
return y0 + ( value - x0 ) / ( x1 - x0 ) * ( y1 - y0 );
}
//--------------------------------------------------------------------------------------------------

View File

@@ -47,5 +47,7 @@ private:
static int findNextDataPoint( const std::vector<double>& values, int index );
static int findPreviousDataPoint( const std::vector<double>& values, int index );
static double extrapolate( const std::vector<double>& x, const std::vector<double>& y, double value );
static double extrapolate( double x0, double y0, double x1, double y1, double value );
static double extrapolateClosestValue( const std::vector<double>& x, const std::vector<double>& y, double value );
};

View File

@@ -117,6 +117,19 @@ TEST( RiaInterpolationToolsTest, ValidIntervalValueTooLowExtrapolationTrend )
EXPECT_DOUBLE_EQ( -2.0, res );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RiaInterpolationToolsTest, ValidIntervalValueTooHighExtrapolationTrend )
{
std::vector<double> x = { 0.0, 1.0, 2.0, 3.0 };
std::vector<double> y = { 0.0, 1.0, 10.0, 20.0 };
// Outside interval on low side
double res = RiaInterpolationTools::linear( x, y, 4.0, RiaInterpolationTools::ExtrapolationMode::TREND );
EXPECT_DOUBLE_EQ( 30.0, res );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------