adapted from http://www.scipy-lectures.org/ by Marco Della Vedova - marco.dellavedova@unicatt.it
Last update: June 2018
Random generate 11 values as
$m(t) = \sin(2 \pi t) + N$ for $t \in \left\{ 0, 0.1, 0.2, ..., 1 \right\}$
where $N$ is a random Gaussian noise with zero mean and 0.01 standard deviation. Plot the linear and the cubic interpolation of these values.
import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
# np.random.seed(1984)
n_values = 11
sigma = 0.01 # standard deviation
measured_time = np.linspace(0, 1, n_values)
noise = sigma * np.random.randn(n_values)
measures = np.sin(2 * np.pi * measured_time) + noise
# Defining the points in which the interpolation will be computed
interpolation_time = np.linspace(0, 1, 50)
# Linear interploation
linear_interp = interpolate.interp1d(measured_time, measures)
linear_results = linear_interp(interpolation_time)
# Cubic interpolation
cubic_interp = interpolate.interp1d(measured_time, measures, kind='cubic')
cubic_results = cubic_interp(interpolation_time)
plt.plot(measured_time, measures, 'o', ms=6, label='measures')
plt.plot(interpolation_time, linear_results, label='linear interp')
plt.plot(interpolation_time, cubic_results, label='cubic interp')
plt.xlabel("$t$")
plt.legend(loc="upper right")
plt.show()
Now try changing sigma and rerun...