Class LinearInterpolator
- java.lang.Object
-
- org.rrd4j.data.Plottable
-
- org.rrd4j.data.LinearInterpolator
-
- All Implemented Interfaces:
IPlottable
public class LinearInterpolator extends Plottable
Class used to interpolate datasource values from the collection of (timestamp, values) points. This class is suitable for linear interpolation only.
Interpolation algorithm returns different values based on the value passed to
setInterpolationMethod()
. If not set, interpolation method defaults to standard linear interpolation (LinearInterpolator.Method.LINEAR
). Interpolation method handles NaN datasource values gracefully.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
LinearInterpolator.Method
A enumeration of interpolation methods
-
Field Summary
Fields Modifier and Type Field Description (package private) double
b0
(package private) double
b1
static int
INTERPOLATE_LEFT
constant used to specify LEFT interpolation.static int
INTERPOLATE_LINEAR
constant used to specify LINEAR interpolation (default interpolation method).static int
INTERPOLATE_REGRESSION
constant used to specify LINEAR REGRESSION as interpolation method.static int
INTERPOLATE_RIGHT
constant used to specify RIGHT interpolation.
-
Constructor Summary
Constructors Constructor Description LinearInterpolator(long[] timestamps, double[] values)
Creates LinearInterpolator from arrays of timestamps and corresponding datasource values.LinearInterpolator(Calendar[] dates, double[] values)
Creates LinearInterpolator from arrays of timestamps and corresponding datasource values.LinearInterpolator(Date[] dates, double[] values)
Creates LinearInterpolator from arrays of timestamps and corresponding datasource values.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description double
getValue(long timestamp)
Retrieves datapoint value based on a given timestamp.void
setInterpolationMethod(int interpolationMethod)
Deprecated.usessetInterpolationMethod(Method)
instead.void
setInterpolationMethod(LinearInterpolator.Method interpolationMethod)
Sets interpolation method to be used.
-
-
-
Field Detail
-
INTERPOLATE_LEFT
public static final int INTERPOLATE_LEFT
constant used to specify LEFT interpolation. SeesetInterpolationMethod()
for explanation.- See Also:
- Constant Field Values
-
INTERPOLATE_RIGHT
public static final int INTERPOLATE_RIGHT
constant used to specify RIGHT interpolation. SeesetInterpolationMethod()
for explanation.- See Also:
- Constant Field Values
-
INTERPOLATE_LINEAR
public static final int INTERPOLATE_LINEAR
constant used to specify LINEAR interpolation (default interpolation method). SeesetInterpolationMethod()
for explanation.- See Also:
- Constant Field Values
-
INTERPOLATE_REGRESSION
public static final int INTERPOLATE_REGRESSION
constant used to specify LINEAR REGRESSION as interpolation method. SeesetInterpolationMethod()
for explanation.- See Also:
- Constant Field Values
-
b0
double b0
-
b1
double b1
-
-
Constructor Detail
-
LinearInterpolator
public LinearInterpolator(long[] timestamps, double[] values)
Creates LinearInterpolator from arrays of timestamps and corresponding datasource values.- Parameters:
timestamps
- timestamps in secondsvalues
- corresponding datasource values- Throws:
IllegalArgumentException
- Thrown if supplied arrays do not contain at least two values, or if timestamps are not ordered, or array lengths are not equal.
-
LinearInterpolator
public LinearInterpolator(Date[] dates, double[] values)
Creates LinearInterpolator from arrays of timestamps and corresponding datasource values.- Parameters:
dates
- Array of Date objectsvalues
- corresponding datasource values- Throws:
IllegalArgumentException
- Thrown if supplied arrays do not contain at least two values, or if timestamps are not ordered, or array lengths are not equal.
-
LinearInterpolator
public LinearInterpolator(Calendar[] dates, double[] values)
Creates LinearInterpolator from arrays of timestamps and corresponding datasource values.- Parameters:
dates
- array of GregorianCalendar objectsvalues
- corresponding datasource values- Throws:
IllegalArgumentException
- Thrown if supplied arrays do not contain at least two values, or if timestamps are not ordered, or array lengths are not equal.
-
-
Method Detail
-
setInterpolationMethod
@Deprecated public void setInterpolationMethod(int interpolationMethod)
Deprecated.usessetInterpolationMethod(Method)
instead.Sets interpolation method to be used. Suppose that we have two timestamp/value pairs:
(t, 100)
and(t + 100, 300)
. Here are the results interpolator returns for t + 50 seconds, for variousinterpolationMethods
:INTERPOLATE_LEFT: 100
INTERPOLATE_RIGHT: 300
INTERPOLATE_LINEAR: 200
If not set, interpolation method defaults to
INTERPOLATE_LINEAR
.The fourth available interpolation method is INTERPOLATE_REGRESSION. This method uses simple linear regression to interpolate supplied data with a simple straight line which does not necessarily pass through all data points. The slope of the best-fit line will be chosen so that the total square distance of real data points from from the best-fit line is at minimum.
This method is deprecated, one should use
setInterpolationMethod()
instead.The full explanation of this interpolation method can be found here.
- Parameters:
interpolationMethod
- Should beINTERPOLATE_LEFT
,INTERPOLATE_RIGHT
,INTERPOLATE_LINEAR
orINTERPOLATE_REGRESSION
. Any other value will be interpreted as INTERPOLATE_LINEAR (default).
-
setInterpolationMethod
public void setInterpolationMethod(LinearInterpolator.Method interpolationMethod)
Sets interpolation method to be used. Suppose that we have two timestamp/value pairs:
(t, 100)
and(t + 100, 300)
. Here are the results interpolator returns for t + 50 seconds, for variousinterpolationMethods
:LEFT: 100
RIGHT: 300
LINEAR: 200
If not set, interpolation method defaults to
INTERPOLATE_LINEAR
.The fourth available interpolation method is REGRESSION. This method uses simple linear regression to interpolate supplied data with a simple straight line which does not necessarily pass through all data points. The slope of the best-fit line will be chosen so that the total square distance of real data points from from the best-fit line is at minimum.
The full explanation of this interpolation method can be found here.
- Parameters:
interpolationMethod
- a method fromLinearInterpolator.Method
.
-
getValue
public double getValue(long timestamp)
Retrieves datapoint value based on a given timestamp. Use this method if you only have one series of data in this class. Method overridden from the base class. This method will be called by the framework. Call this method only if you need interpolated values in your code.- Specified by:
getValue
in interfaceIPlottable
- Specified by:
getValue
in classPlottable
- Parameters:
timestamp
- Timestamp in seconds for the datapoint.- Returns:
- Double value of the datapoint.
-
-