#!/usr/bin/env python # coding: utf-8 # # *This notebook contains course material from [CBE30338](https://jckantor.github.io/CBE30338) # by Jeffrey Kantor (jeff at nd.edu); the content is available [on Github](https://github.com/jckantor/CBE30338.git). # The text is released under the [CC-BY-NC-ND-4.0 license](https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode), # and code is released under the [MIT license](https://opensource.org/licenses/MIT).* # # < [Predictive Control](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/08.00-Predictive-Control.ipynb) | [Contents](toc.ipynb) | [Discrete Events](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/09.00-Discrete-Events.ipynb) >

Open in Colab

Download # # Zero-Order Hold and Interpolation # # 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. # ## Interpolation Function # In[1]: 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') # ## Demonstration # In[2]: get_ipython().run_line_magic('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']) # In[ ]: # # < [Predictive Control](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/08.00-Predictive-Control.ipynb) | [Contents](toc.ipynb) | [Discrete Events](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/09.00-Discrete-Events.ipynb) >

Open in Colab

Download