- All Implemented Interfaces:
IPlottable
Suitable for linear interpolation of time-series data points.
Interpolation algorithm behavior depends on the method set via setInterpolationMethod(Method). Defaults to linear interpolation. Handles NaN datasource values gracefully.
Different interpolation methods available: - INTERPOLATE_LEFT - Left interpolation - INTERPOLATE_RIGHT - Right interpolation - INTERPOLATE_LINEAR - Standard linear interpolation
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumA enumeration of interpolation methodsField Summary
Modifier and TypeFieldDescription(package private) doubleIntercept of the best-fit line, or NaN when regression has not been computed.(package private) doubleSlope of the best-fit line, or NaN when regression has not been computed.static final intConstant used to specify LEFT interpolation.static final intConstant used to specify LINEAR interpolation (default interpolation method).static final intConstant used to specify LINEAR REGRESSION as interpolation method.static final intConstant used to specify RIGHT interpolation.private LinearInterpolator.MethodInterpolation methodprivate intLast index usedprivate final long[]Array of timestamps for the data seriesprivate final double[]Y coordinates of the points being interpolated.Constructor Summary
ConstructorsConstructorDescriptionLinearInterpolator(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
Modifier and TypeMethodDescriptionprivate voidCalculate best fit linedoublegetValue(long timestamp) Retrieves datapoint value based on a given timestamp.voidsetInterpolationMethod(LinearInterpolator.Method interpolationMethod) /** Sets interpolation method to be used.private voidvalidate()Validate the current processing state
Field Details
INTERPOLATE_LEFT
public static final int INTERPOLATE_LEFTConstant used to specify LEFT interpolation. SeesetInterpolationMethod()for explanation.- See Also:
INTERPOLATE_RIGHT
public static final int INTERPOLATE_RIGHTConstant used to specify RIGHT interpolation. SeesetInterpolationMethod()for explanation.- See Also:
INTERPOLATE_LINEAR
public static final int INTERPOLATE_LINEARConstant used to specify LINEAR interpolation (default interpolation method). SeesetInterpolationMethod()for explanation.- See Also:
INTERPOLATE_REGRESSION
public static final int INTERPOLATE_REGRESSIONConstant used to specify LINEAR REGRESSION as interpolation method. SeesetInterpolationMethod()for explanation.- See Also:
lastIndexUsed
private int lastIndexUsedLast index usedinterpolationMethod
Interpolation methodtimestamps
private final long[] timestampsArray of timestamps for the data seriesvalues
private final double[] valuesY coordinates of the points being interpolated.b0
double b0Intercept of the best-fit line, or NaN when regression has not been computed. Used only if INTERPOLATE_BESTFIT is specified.b1
double b1Slope of the best-fit line, or NaN when regression has not been computed.
Constructor Details
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
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
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 Details
validate
private void validate()Validate the current processing statesetInterpolationMethod
/** 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: 100RIGHT: 300LINEAR: 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.
calculateBestFitLine
private void calculateBestFitLine()Calculate best fit linegetValue
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:
getValuein interfaceIPlottable- Specified by:
getValuein classorg.rrd4j.data.Plottable- Parameters:
timestamp- Timestamp in seconds for the datapoint.- Returns:
- Double value of the datapoint.