Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

bilinear.grid

Bilinear Interpolation for Data on a Rectangular grid


Description

This is an implementation of a bilinear interpolating function.

For a point (x0,y0) contained in a rectangle (x1,y1),(x2,y1), (x2,y2),(x1,y2) and x1<x2, y1<y2, the first step is to get z() at locations (x0,y1) and (x0,y2) as convex linear combinations z(x0,y*)=a*z(x1,y*)+(1-a)*z(x2,y*) where a=(x2-x1)/(x0-x1) for y*=y1,y2. In a second step z(x0,y0) is calculated as convex linear combination between z(x0,y1) and z(x0,y2) as z(x0,y1)=b*z(x0,y1)+(1-b)*z(x0,y2) where b=(y2-y1)/(y0-y1).

Finally, z(x0,y0) is a convex linear combination of the z values at the corners of the containing rectangle with weights according to the distance from (x0,y0) to these corners.

The grid lines can be unevenly spaced.

Usage

bilinear.grid(x,y,z,xlim=c(min(x),max(x)),ylim=c(min(y),max(y)),
                         nx=40,ny=40,dx=NULL,dy=NULL)

Arguments

x

a vector containing the x coordinates of the rectangular data grid.

y

a vector containing the y coordinates of the rectangular data grid.

z

a matrix containing the z[i,j] data values for the grid points (x[i],y[j]).

xlim

vector of length 2 giving lower and upper limit for range x coordinates used for output grid.

ylim

vector of length 2 giving lower and upper limit for range of y coordinates used for output grid.

nx

output grid dimension in x direction.

ny

output grid dimension in y direction.

dx

output grid spacing in x direction, not used by default, overrides nx if specified.

dy

output grid spacing in y direction, not used by default, overrides ny if specified..

Value

This function produces a grid of interpolated points, feasible to be used directly with image and contour:

x

vector of x coordinates of the output grid.

y

vector of y coordinates of the output grid.

z

matrix of interpolated data for the output grid.

Note

Use interp for the general case of irregular gridded data!

References

Pascal Getreuer, Linear Methods for Image Interpolation, Image Processing On Line, 2011, http://www.ipol.im/pub/art/2011/g_lmii/article.pdf

See Also

Examples

data(akima760)
# interpolate at a grid [0,8]x[0,10]
akima.bil <- bilinear.grid(akima760$x,akima760$y,akima760$z)
zmin <- min(akima.bil$z, na.rm=TRUE)
zmax <- max(akima.bil$z, na.rm=TRUE)
breaks <- pretty(c(zmin,zmax),10)
colors <- heat.colors(length(breaks)-1)
image(akima.bil, breaks=breaks, col=colors)
contour(akima.bil, levels=breaks, add=TRUE)

akima

Interpolation of Irregularly and Regularly Spaced Data

v0.6-2.1
ACM | file LICENSE
Authors
Hiroshi Akima [aut, cph] (Fortran code (TOMS 760, 761, 697 and 433)), Albrecht Gebhardt [aut, cre, cph] (R port (interp* functions), bicubic* functions), Thomas Petzold [ctb, cph] (aspline function), Martin Maechler [ctb, cph] (interp2xyz function + enhancements), YYYY Association for Computing Machinery, Inc. [cph] (covers code from TOMS 760, 761, 697 and 433)
Initial release
2016-12-16

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.