Aaron ChenAaron ChenAaron Chen SciPy-5 B-Spline Curves

Interpolate bank, which is in the SciPy, provides lots of ways to do the interpolation operation.

Here is an example of interpolation with directed line and B-Spline to fix the point on a sine wave.

In this program, we use the interp1d function to get a new linear interpolation function. Splrep function is used to calculate the parameters in B-Spline Curves, then transfer them to splev function to realize the interpolation operation.

Here is the code and the result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# -*- coding: utf-8 -*-
import numpy as np
from scipy import interpolate
import pylab as pl

x = np.linspace(0, 2*np.pi+np.pi/6, 10)
y = np.sin(x)

x_new = np.linspace(0, 2*np.pi+np.pi/6, 100)
f_linear = interpolate.interp1d(x, y)
tck = interpolate.splrep(x, y)
y_bspline = interpolate.splev(x_new, tck)

pl.plot(x, y, "o", label = 'Original Data')
pl.plot(x_new, f_linear(x_new), label = 'Linear Interpolation')
pl.plot(x_new, y_bspline, label = 'B-Spline')
pl.legend()
pl.show()

Definition of Interpolate.

  • To Be Continue