rgpycrumbs.interpolation

Functions

spline_interp(x, y[, num, knots])

Interpolate using B-splines.

Module Contents

rgpycrumbs.interpolation.spline_interp(x, y, num=None, knots=3)[source]

Interpolate using B-splines.

Fits a B-spline representation to the data (x, y) and evaluates it on a uniformly spaced grid.

Parameters:
  • x – Independent variable values (1D array-like, strictly increasing).

  • y – Dependent variable values (same length as x).

  • num – Number of evaluation points on the output grid. It defaults to (len(x) - 1) * knots + 1. The grid step is then the mean sample spacing divided by knots, so the grid never has fewer points than the input. For evenly spaced x the grid passes through every sample. With an odd degree, such as the cubic default, each interval between samples then gets the knots + 1 grid points that fix the spline polynomial on it.

  • knots – Degree of the spline (passed as k to scipy.interpolate.splrep). The default of 3 is the cubic spline that SciPy recommends.

Returns:

(x_fine, y_fine) where x_fine is a uniform grid of length num spanning [x.min(), x.max()] and y_fine is the spline evaluated on that grid.

Return type:

tuple

Raises:

ValueError – If x and y are not one-dimensional and of equal length, if x is not strictly increasing, if there are no more samples than knots, or if num is below one.

Added in version 1.0.0.

Changed in version 1.14.0: Inputs are validated before SciPy sees them, and lists are accepted. A repeated x used to return all-NaN values. num defaults to a grid derived from the samples instead of 100 points, which dropped detail from inputs with more samples than that.