interpolate: Interpolation

View source: R/interpolate.R

interpolateR Documentation

Interpolation

Description

Interpolates unevenly spaced points into a relatively smooth curve. If the points are evenly spaced but there are missing values and/or aliasing should be avoided, use resample instead.

Usage

interpolate(
  x,
  y,
  xout,
  method = c("splineFC", "spline", "constant", "linear", "approxLowPass", "sgolay",
    "pchip", "cosine", "cardinal", "hermite", "loess"),
  plot = FALSE,
  ...
)

interpol_approxLowPass(bandwidth = 0.1)

interpol_sgolay(p = 3, n = 15)

interpol_cardinal(tension = 0.5)

interpol_loess(span = 0.5)

Arguments

x, y

numeric vectors giving the coordinates of the points to be interpolated (no NAs)

xout

numeric vector of target x‑coordinates where interpolation is to take place

method

interpolation method to use. Accepts either a character string naming an inbuilt method (see "Interpolation methods" below) or a constructor function such as interpol_loess(span = 0.3) or a custom function (see examples). The default is "splineFC". When calling interpolate() directly, a string plus method‑specific arguments in ... is the simplest option. When passing method through a higher‑level function (e.g. resample()), use a constructor because it packages all method‑specific arguments into a single object

plot

logical; if TRUE, a quick diagnostic plot is drawn

...

extra arguments specific to the chosen interpolation method, e.g. span = 0.7 for "loess" (see "Interpolation methods" for per‑method arguments). Used only when method is a character string; when method is a constructor function, parameters are passed to the constructor directly. Do not pass graphical arguments here

bandwidth

(interpol_approxLowPass) the amount of smoothing, a number between 0 and 1: close to 0 = more smoothing, close to 1 = less smoothing. Defaults to len_x / len_out when called via the string dispatch of interpolate().

p

(interpol_sgolay) polynomial order for Savitzky‑Golay smoothing (positive integer, defaults to 3).

n

(interpol_sgolay) filter length for Savitzky‑Golay smoothing (odd positive integer, defaults to floor(len_out / len_x / 2) * 2 + 1 when called via the string dispatch of interpolate()).

tension

(interpol_cardinal) a number between 0 and 1 controlling the tightness of the cardinal spline: tension = 0 gives Catmull‑Rom (smooth but may overshoot), tension = 0.5 is a good balance, and tension = 1 makes the curve pass through midpoints between the input points. Defaults to 0.5.

span

(interpol_loess) the amount of LOESS smoothing, a number between 0 and 1: a larger span gives more smoothing, a smaller span captures finer detail but may overfit. Defaults to 0.5.

Value

A numeric vector of interpolated y‑values at the requested xout locations.

Interpolation methods

constant

Constant interpolation via approx. Fast, but no smoothing.

linear aka approx

Linear interpolation via approx. Fast, but not smooth.

spline

Cubic spline interpolation (FMM method) via spline. Fast, but overshoots.

splineFC

Monotone cubic interpolation using the Fritsch‑Carlson method (see splinefun). Moderately fast, less overshooting than the FMM spline.

approxLowPass

Linear interpolation followed by low‑pass filtering. Fast, smooth, but reduced range compared to original y. Constructor: interpol_approxLowPass(bandwidth).

sgolay

Linear interpolation followed by Savitzky‑Golay smoothing (see sgolayfilt). Fairly similar to approxLowPass, but much slower. Constructor: interpol_sgolay(p, n).

pchip

Piecewise Cubic Hermite Interpolating Polynomial (preserves monotonicity). Calls interp1 with method = "pchip".

cosine

Cosine‑eased (smoothstep) interpolation. Eases between anchor points using a cosine curve. Fast, passes exactly through input points.

cardinal

Cardinal spline interpolation (a generalization of Catmull‑Rom). Fast, can be forced to pass exactly through input points. Constructor: interpol_cardinal(tension).

hermite

Hermite spline interpolation with forced zero slope at local extrema to prevent overshoot. Fast, passes exactly through input points.

loess

Locally estimated scatterplot smoothing (LOESS) via loess. Smooth, but slow; may overshoot. Constructor: interpol_loess(span).

See Also

interpolateNA resample

Examples

x  = c(0, .15, .2, .3, .7, 1)
y  = c(360, 116, 550, 350, 700, 610)
xout = seq(0, 1, length.out = 100)

# Compare inbuilt interpolation methods
ms = c('constant', 'linear', 'spline', 'splineFC', 'approxLowPass',
        'sgolay', 'cosine', 'cardinal', 'hermite', 'loess')
op = par(c('mfrow', 'mar')); par(mfrow = c(4, 3), mar = c(2, 2, 3, 1))
for (m in ms) {interpolate(x, y, xout, method = m, plot = TRUE); title(m)}
par(op)

# Passing method‑specific parameters via ...
interpolate(x, y, xout, method = 'cardinal', tension = 0, plot = TRUE)
interpolate(x, y, xout, method = 'loess', span = 0.2, plot = TRUE)
interpolate(x, y, xout, method = 'loess', span = 0.9, plot = TRUE)

# Equivalent: passing a constructor (useful when forwarding through
# higher‑level functions like resample())
interpolate(x, y, xout, method = interpol_cardinal(tension = 0), plot = TRUE)
interpolate(x, y, xout, method = interpol_loess(span = 0.2), plot = TRUE)

# Passing a fully custom function
interpolate(x, y, xout, plot = TRUE,
  method = function(...) spline(x, y, xout = xout, method = 'natural')$y)

soundgen documentation built on Sept. 20, 2026, 5:07 p.m.