{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "*This notebook contains course material from [CBE30338](https://jckantor.github.io/CBE30338)\n", "by Jeffrey Kantor (jeff at nd.edu); the content is available [on Github](https://github.com/jckantor/CBE30338.git).\n", "The text is released under the [CC-BY-NC-ND-4.0 license](https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode),\n", "and code is released under the [MIT license](https://opensource.org/licenses/MIT).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [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) >
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Zero-Order Hold and Interpolation\n",
"\n",
"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. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Interpolation Function"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"def interp0(x, xp, yp):\n",
" \"\"\"Zeroth order hold interpolation w/ same\n",
" (base) signature as numpy.interp.\"\"\"\n",
"\n",
" def func(x0):\n",
" if x0 <= xp[0]:\n",
" return yp[0]\n",
" if x0 >= xp[-1]:\n",
" return yp[-1]\n",
" k = 0\n",
" while x0 > xp[k]:\n",
" k += 1\n",
" return yp[k-1]\n",
" \n",
" if isinstance(x,float):\n",
" return func(x)\n",
" elif isinstance(x, list):\n",
" return [func(x) for x in x]\n",
" elif isinstance(x, np.ndarray):\n",
" return np.asarray([func(x) for x in x])\n",
" else:\n",
" raise TypeError('argument must be float, list, or ndarray')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Demonstration"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
" "
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}