This notebook contains course material from CBE30338 by Jeffrey Kantor (jeff at nd.edu); the content is available on Github. The text is released under the CC-BY-NC-ND-4.0 license, and code is released under the MIT license.
Implementation of control systems generally requires an interface between the sampled data computations of process control and the continuous time dynamics of the real world. A zero-order hold is the most common model for the reconstruction of a continuous time signal from sampled data.
import numpy as np
def interp0(x, xp, yp):
"""Zeroth order hold interpolation w/ same
(base) signature as numpy.interp."""
def func(x0):
if x0 <= xp[0]:
return yp[0]
if x0 >= xp[-1]:
return yp[-1]
k = 0
while x0 > xp[k]:
k += 1
return yp[k-1]
if isinstance(x,float):
return func(x)
elif isinstance(x, list):
return [func(x) for x in x]
elif isinstance(x, np.ndarray):
return np.asarray([func(x) for x in x])
else:
raise TypeError('argument must be float, list, or ndarray')
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
# choose a function
f = np.sin
# sampled signal
xp = np.linspace(0,10,20)
yp = f(xp)
# interpolation grid with 'true' function
x = np.linspace(0,12,1000)
plt.plot(x,f(x),'--')
# plot
plt.hold(True)
plt.scatter(xp,yp)
plt.plot(x,interp0(x,xp,yp),'r')
plt.xlim([x.min(),x.max()])
plt.title('Zero Order Hold/Interpolation')
plt.xlabel('Time')
plt.ylabel('Value')
plt.legend(['Signal','ZOH'])
<matplotlib.legend.Legend at 0x10f61a080>