%matplotlib inline

"""
Explanation: Interpolation with scipy
TODO
* ...
End of explanation
"""


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

xmin, xmax = 0., 4*np.pi

x = np.linspace(xmin, xmax, 10)
y = np.sin(x)

x2 = np.linspace(xmin, xmax, 100)

"""
Explanation: Official documentation: https://docs.scipy.org/doc/scipy/reference/interpolate.html
Import modules and initialize data
End of explanation
"""


# Linear interpolation with extrapolation
f = scipy.interpolate.interp1d(x, y,
                               kind='linear',
                               fill_value="extrapolate")

y2 = f(x2)

plt.plot(x, y, ":b", label="original")
plt.plot(x2, y2, "-r", label="interpollated")
plt.legend();

"""
Explanation: Linear interpollation
End of explanation
"""


# https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.splrep.html#scipy.interpolate.splrep
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.splprep.html#scipy.interpolate.splprep

spl = scipy.interpolate.splrep(x, y)

y2 = scipy.interpolate.splev(x2, spl)

plt.plot(x, y, ":b", label="original")
plt.plot(x2, y2, "-r", label="interpollated")
plt.legend();

spl = scipy.interpolate.splrep(x, y,
                               xb=x[0], xe=x[-1],   # The interval to fit
                               #s=0.,               # A smoothing factor
                               k=1)                 # The degree fo the spline fit

y2 = scipy.interpolate.splev(x2, spl)

plt.plot(x, y, ":b", label="original")
plt.plot(x2, y2, "-r", label="interpollated")
plt.legend();

"""
Explanation: 1-D Splines
https://docs.scipy.org/doc/scipy/reference/interpolate.html#d-splines
End of explanation
"""


# Spline linear interpolation with extrapolation (should be the same than spline1...)
f = scipy.interpolate.interp1d(x, y,
                               kind='slinear',
                               fill_value="extrapolate")

y2 = f(x2)

plt.plot(x, y, ":b", label="original")
plt.plot(x2, y2, "-r", label="interpollated")
plt.legend();

"""
Explanation: Spline linear interpolation
End of explanation
"""


