Spline Interpolation Example in Python

     Interpolation is a technique for estimating unknown data points within a given data range, resulting in a smoother curve by discovering new values between existing data points. Spline interpolation is a type of piecewise polynomial interpolation and it can implemented using various functions provided by the SciPy API.

    In this tutorial, we'll explore the application of spline interpolation for a given data using the SciPy functions in Python. The tutorial will cover:

  1. Preparing test data
  2. Understanding the concept of spline interpolation
  3. Performing direct spline interpolation
  4. Applying spline interpolation with InterpolatedUnivariateSpline
  5. Source code listing

    We'll start by loading the required libraries.

 
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np 
 
 

 
Preparing test data
 
   To illustrate the spline interpolation method, let's create a simple data. Our target data consists of x and y components representing a curve. You can apply the same method to your specific dataset.
 
 
y = [1, 3, 4, 3, 5, 7, 5, 6, 8, 7, 8, 9, 8, 7]
n = len(y)
x = range(0, n)
 
 
    Now, let's visualize this data on a graph and proceed to apply linear interpolation. Linear interpolation involves connecting data points with straight lines on the graph.
 
 
plt.plot(x,y,'ro')
plt.plot(x,y, 'b')
plt.title("Data set and linear interpolation")
plt.show() 

 

Understanding the concept of spline interpolation

    Spline interpolation is a mathematical technique used to construct a smooth curve (spline) that passes through a set of given data points. Unlike linear interpolation, which connects data points with straight line segments, spline interpolation uses piecewise-defined polynomials to create a smooth and continuous curve. This method divides intervals between data points into smaller subintervals and fits polynomials that connect smoothly at these points, ensuring continuity.

    The applications of this method include computer graphics, signal processing, image processing, and scientific computing, where accurate and smooth curves are essential. In short, spline interpolation is a powerful tool for approximating functions based on a set of discrete data points.

 

Direct Spline Interpolation

    To represent spline interpolation, smoothing coefficients can be obtained parametrically or directly. The 'splrep' function helps to define the curve using the direct method, providing a tuple (t, c, k) containing the vector of knots, the B-spline coefficients, and the degree of the spline. Once the coefficients are obtained, we use the 'splev' function to evaluate the spline.

The 'splev' function calculates the value of the smoothing polynomial based on the given coefficients. Finally, we visualize the curve on a graph.

 
tck = interpolate.splrep(x, y, s=0)
xfit = np.arange(0, n-1, np.pi/50)
yfit = interpolate.splev(xfit, tck, der=0)

plt.plot(x, y, 'ro')
plt.plot(xfit, yfit,'b')
plt.plot(xfit, yfit)
plt.title("Direct spline interpolantion")
plt.show() 

 

 

Spline interpolation with InterpolatedUnivariateSpline
 
    We can achieve spline smoothing using the InterpolatedUnivariateSpline function too. Derived from the UnivariateSpline class, InterpolatedUnivariateSpline fits the curve based on the provided x and y data. Below, we apply this method to our target test data and visualize the result on a graph.

 
s = interpolate.InterpolatedUnivariateSpline(x, y)
xfit = np.arange(0, n-1, np.pi/50)
yfit = s(xfit)

plt.plot(x, y, 'ro')
plt.plot(xfit, yfit,'green')
plt.title("InterpolatedUnivariateSpline interpolation")
plt.show()
  
   

 
    In this tutorial, we've briefly learned how to implement spline interpolation by using SciPy API's interpolation functions in Python. The full source code is listed below. 
 
 
Source code listing

 
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np

y = [1,3,4,3,5,7,5,6,8,7,8,9,8,7]
n = len(y)
x = range(0, n)
 
plt.plot(x,y,'ro')
plt.plot(x,y, 'b')
plt.title("Data set and linear interpolation")
plt.show()

tck = interpolate.splrep(x, y, s=0)
xfit = np.arange(0, n-1, np.pi/50)
yfit = interpolate.splev(xfit, tck, der=0)

plt.plot(x, y, 'ro')
plt.plot(xfit, yfit,'b')
plt.plot(xfit, yfit)
plt.title("Direct spline interpolantion")
plt.show()

s = interpolate.InterpolatedUnivariateSpline(x, y)
xfit = np.arange(0, n-1, np.pi/50)
yfit = s(xfit)

plt.plot(x, y, 'ro')
plt.plot(xfit, yfit,'green')
plt.title("InterpolatedUnivariateSpline interpolation")
plt.show() 
  
 
References:

No comments:

Post a Comment