Example 1: Interpolation

example1

#Example 1: Interpolation
import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
import os
os.chdir('C:\\Anaconda\\mycodes\\aerospace')
os.getcwd()
#create the data points and add noise as follows
x = np.linspace(-18,18,36)
noise = 0.1 * np.random.random(len(x))
signal = np.sinc(x) + noise
#create a linear interpolation function, and then apply it to an input array
#with five times as many data points
interpolated = interpolate.interp1d(x,signal)
x2 = np.linspace(-18,18,180)
y = interpolated(x2)
#cubic interpolation
cubic = interpolate.interp1d(x,signal,kind="cubic")
y2 = cubic(x2)
#plot the results
plt.plot(x,signal,'o',label="data")
plt.plot(x2,y,'-',label='linear')
plt.plot(x2,y,'--',label='cubic')
plt.legend()
plt.savefig("example1.png", dpi=300)
plt.show()
plt.close()