Bilinear Interpolation in Python

Bilinear Interpolation in Python

What is Bilinear Interpolation?

Linear Interpolation in mathematics helps curve fitting by using linear polynomials that make new data points between a specific range of a discrete set of definite data points.

The term Bilinear Interpolation is an extension to linear interpolation that performs the interpolation of functions containing two variables (for example, x and y) on a rectilinear two-dimensional grid.

This tutorial will demonstrate how to perform such Bilinear Interpolation in Python.

Creating a function to perform bilinear interpolation in Python

We can implement the logic for Bilinear Interpolation in a function. This function works for a collection of 4 points. It is a very basic implementation of the mathematical formula for Bilinear Interpolation.

See the code below.

Output:

4

Using the scipy.interpolate.interp2d() function to perform bilinear interpolation in Python

The scipy library helps perform different mathematical and scientific calculations like linear algebra, integration, and many more.

The scipy.interpolate.interp2d() function performs the interpolation over a two-dimensional grid. This method can handle more complex problems. This method represents functions containing x, y, and z, array-like values that make functions like z = f(x, y). It will return the scalar value of z.

We can use it as shown below.

To use this function, we need to understand the three main parameters.

  • x, y (array_like) – This parameter defines the data points of the coordinates. If the data points lie on a regular grid, then x can represent the column coordinates and y can represent the row coordinates.
  • z (array_like) – This parameter defines the value of the function to interpolate at the given data points.
  • kind ('linear', 'cubic', 'quintic') – This parameter informs the kind of spline interpolation to use. The default value of this parameter is linear.

Now let us see how to perform bilinear interpolation using this method.

Output:

In the above code,

  • The numpy.arange() function returns evenly spaced values within a given interval. This function takes the values in the form of an array.
  • The numpy.meshgrid() function creates a rectangular grid with the help of one-dimensional arrays that represent cartesian indexing or matrix indexing.
  • Finally, the cos() function of the numpy library helps in finding the cosine value. We use this as the main function in the above code, i.e., z.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *