approx2: Resampling in 2D with Interpolation

View source: R/signal2.R

approx2R Documentation

Resampling in 2D with Interpolation

Description

Resample the (potentially scattered) 2D data to a rectilinear grid at the specified coordinates. Interpolation can be performed within a tolerance using several interpolation methods.

Usage

approx2(x, y, z, xout, yout,
	interp = "linear", nx = length(x), ny = length(y),
	tol = NA_real_, tol.ref = "abs", extrap = NA_real_)

Arguments

x, y, z

The data to be interpolated. Alternatively, x can be a matrix, in which case the matrix elements are used for z and x and y are generated from the matrix's dimensions.

xout, yout

The coordinate (grid lines) where the resampling should take place. These are expanded into a rectilinear grid using expand.grid().

interp

Interpolation method. One of 'none', 'sum', 'mean', 'max', 'min', 'area', 'linear', 'cubic', 'gaussian', or 'lanczos'.

nx, ny

If xout is not given, then interpolation is performed at nx * ny equally spaced data points along the range of x and y.

tol

The tolerance for the data points used for interpolation. Must be nonnegative. May be length-2 to have different tolerances for x and y. If NA, then the tolerance is estimated from the maximum differences in x and y.

tol.ref

If 'abs', then comparison is done by taking the absolute difference. If 'x', then relative differences are used.

extrap

The value to be returned when performing extrapolation, i.e., in the case when there is no data within tol.

Details

See approx1 for details of the 1D implementation. The 2D implementation is mostly the same, except it uses a kd-tree to quickly find neighboring points.

Note that interp = "linear" and interp = "cubic" use a kernel-based approximation. Traditionally, bilinear and bicubic interpolation use 4 and 16 neighboring points, respectively. However, to support scattered data, approx2 will use as many points as are found within the given tolerance, and scale the kernels accordingly. If the input data falls on a regular grid already, then the tolerance should be specified accordingly. Set tol equal to the sampling rate for interp = "linear" and twice the sampling rate for interp = "cubic".

Value

A vector of the same length as xout, giving the resampled data.

Author(s)

Kylie A. Bemis

See Also

expand.grid, asearch, approx, approx1

Examples

x <- matrix(1:25, nrow=5, ncol=5)

approx2(x, nx=10, ny=10, interp="cubic") # upsampling

kuwisdelu/matter documentation built on Oct. 19, 2024, 10:31 a.m.