interp1#
Piecewise interpolation with various methods (linear, nearest, next, previous, spline).
Added in version 0.9.1.
-
template<typename OpX, typename OpV, typename OpXQ>
auto matx::interp1(const OpX &x, const OpV &v, const OpXQ &xq, InterpMethod method = InterpMethod::LINEAR)# 1D interpolation of samples at query points.
Interpolation is performed along the last dimension. All other dimensions must be of compatible size.
- Template Parameters:
OpX – Type of sample points
OpV – Type of sample values
OpXQ – Type of query points
- Parameters:
x – Sample points. Last dimension must be sorted in ascending order.
v – Sample values. Must have compatible dimensions with x.
xq – Query points where to interpolate. All dimensions except the last must be of compatible size with x and v (e.g. x and v can be vectors, and xq can be a matrix).
method – Interpolation method (LINEAR, NEAREST, NEXT, PREV, SPLINE)
- Returns:
Operator that interpolates values at query points, with the same dimensions as xq.
-
template<typename OpX, typename OpV, typename OpXQ>
auto matx::interp1(const OpX &x, const OpV &v, const OpXQ &xq, const int (&axis)[1], InterpMethod method = InterpMethod::LINEAR)# 1D interpolation of samples at query points.
Interpolation is performed along the specified dimension. All other dimensions must be of compatible size.
- Template Parameters:
OpX – Type of sample points
OpV – Type of sample values
OpXQ – Type of query points
- Parameters:
x – Sample points. Last dimension must be sorted in ascending order.
v – Sample values. Must have compatible dimensions with x.
xq – Query points where to interpolate. All dimensions except the specified dimension must be of compatible size with x and v (e.g. x and v can be vectors, and xq can be a matrix).
axis – Dimension (of xq) along which to interpolate.
method – Interpolation method (LINEAR, NEAREST, NEXT, PREV, SPLINE)
- Returns:
Operator that interpolates values at query points, with the same dimensions as xq.
Interpolation Methods#
-
enum class matx::InterpMethod#
Interpolation method enumeration.
Specifies the algorithm to use when performing interpolation between sample points.
Values:
-
enumerator LINEAR#
Linear interpolation between adjacent points.
-
enumerator NEAREST#
Uses the value at the nearest sample point.
-
enumerator NEXT#
Uses the value at the next sample point.
-
enumerator PREV#
Uses the value at the previous sample point.
-
enumerator SPLINE#
Cubic spline interpolation, using not-a-knot boundary conditions.
-
enumerator LINEAR#
Examples#
Linear Interpolation (default):
auto x = make_tensor<TestType>({5});
x.SetVals({static_cast<TestType>(0.0), static_cast<TestType>(1.0), static_cast<TestType>(3.0), static_cast<TestType>(3.5), static_cast<TestType>(4.0)});
auto v = make_tensor<TestType>(x.Shape());
v.SetVals({static_cast<TestType>(0.0), static_cast<TestType>(2.0), static_cast<TestType>(1.0), static_cast<TestType>(3.0), static_cast<TestType>(4.0)});
auto xq = make_tensor<TestType>({6});
xq.SetVals({static_cast<TestType>(-1.0), static_cast<TestType>(0.0), static_cast<TestType>(0.25), static_cast<TestType>(1.0), static_cast<TestType>(1.5), static_cast<TestType>(5.0)});
auto vq_linear = make_tensor<TestType>({xq.Size(0)});
vq_linear.SetVals({static_cast<TestType>(0.0), static_cast<TestType>(0.0), static_cast<TestType>(0.5), static_cast<TestType>(2.0), static_cast<TestType>(1.75), static_cast<TestType>(4.0)});
auto vq_nearest = make_tensor<TestType>({xq.Size(0)});
vq_nearest.SetVals({static_cast<TestType>(0.0), static_cast<TestType>(0.0), static_cast<TestType>(0.0), static_cast<TestType>(2.0), static_cast<TestType>(2.0), static_cast<TestType>(4.0)});
auto vq_next = make_tensor<TestType>({xq.Size(0)});
vq_next.SetVals({static_cast<TestType>(0.0), static_cast<TestType>(0.0), static_cast<TestType>(2.0), static_cast<TestType>(2.0), static_cast<TestType>(1.0), static_cast<TestType>(4.0)});
auto vq_prev = make_tensor<TestType>({xq.Size(0)});
vq_prev.SetVals({static_cast<TestType>(0.0), static_cast<TestType>(0.0), static_cast<TestType>(0.0), static_cast<TestType>(2.0), static_cast<TestType>(2.0), static_cast<TestType>(4.0)});
auto vq_spline = make_tensor<TestType>({xq.Size(0)});
vq_spline.SetVals({static_cast<TestType>(-10.7391), static_cast<TestType>(0.0), static_cast<TestType>(1.1121), static_cast<TestType>(2.0), static_cast<TestType>(1.3804), static_cast<TestType>(-8.1739)});
auto out_linear = make_tensor<TestType>({xq.Size(0)});
(out_linear = interp1(x, v, xq, InterpMethod::LINEAR)).run(exec);
Nearest Neighbor Interpolation:
auto out_nearest = make_tensor<TestType>({xq.Size(0)});
(out_nearest = interp1(x, v, xq, InterpMethod::NEAREST)).run(exec);