Interpolation

Interpolation is a method of creating new data points within the range of known data points.

In the graph below, the dots show original data and the curves show functions plotting interpolated data points, See below for the Python code example that generated the graph.

Types of interpolation include:

Python Example

To download the code below, click here.

"""
interpolation.py
creates and displays interpolation functions
"""
from scipy.interpolate import interp1d
import numpy as np
import matplotlib.pyplot as plotlib

# Define parameters.
x_sample_start = 0
x_sample_end = 15
x_sample_number = 16
x_sample_endpoint = True
x_new_sample_number = 60
y_exponent = 2
y_divisor = 15.0

# Create an array of x sample data points.
x = np.linspace(
    x_sample_start,
    x_sample_end,
    num=x_sample_number,
    endpoint=x_sample_endpoint)
print("x array values:")
print(x)

# Create an array of y sample data points as a sine function of x.
y = np.sin(-x**y_exponent/y_divisor)
print("y array values:")
print(y)

# Create interpolation functions to generate new y data points.
linear_interpolation_function = interp1d(x, y, kind='linear')
cubic_interpolation_function = interp1d(x, y, kind='cubic')
quadratic_interpolation_function = interp1d(x, y, kind='quadratic')

# Define an array of new x data points.
x_new = np.linspace(
    x_sample_start,
    x_sample_end,
    num=x_new_sample_number,
    endpoint=x_sample_endpoint)

# Plot the functions.
plotlib.plot(
    x, y, 'o',
    x_new, linear_interpolation_function(x_new), '-',
    x_new, cubic_interpolation_function(x_new), '--',
    x_new, quadratic_interpolation_function(x_new), ':'
)

# Plot the legend.
plotlib.legend(['data', 'linear', 'cubic', 'quadratic'], loc='best')

# Display the plot.
plotlib.show()
The output is shown below:
x array values:
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
y array values:
[-0.         -0.06661729 -0.26351739 -0.56464247 -0.87559522 -0.99540796
 -0.67546318  0.12474817  0.9022995   0.77276449 -0.37415123 -0.97746767
  0.17432678  0.96347895 -0.47968533 -0.65028784]