|
| 1 | +"""dtime.py |
| 2 | +
|
| 3 | +Functions for manipulating discrete time systems. |
| 4 | +
|
| 5 | +Routines in this module: |
| 6 | +
|
| 7 | +sample_system() |
| 8 | +_c2dmatched() |
| 9 | +""" |
| 10 | + |
| 11 | +"""Copyright (c) 2012 by California Institute of Technology |
| 12 | +All rights reserved. |
| 13 | +
|
| 14 | +Redistribution and use in source and binary forms, with or without |
| 15 | +modification, are permitted provided that the following conditions |
| 16 | +are met: |
| 17 | +
|
| 18 | +1. Redistributions of source code must retain the above copyright |
| 19 | + notice, this list of conditions and the following disclaimer. |
| 20 | +
|
| 21 | +2. Redistributions in binary form must reproduce the above copyright |
| 22 | + notice, this list of conditions and the following disclaimer in the |
| 23 | + documentation and/or other materials provided with the distribution. |
| 24 | +
|
| 25 | +3. Neither the name of the California Institute of Technology nor |
| 26 | + the names of its contributors may be used to endorse or promote |
| 27 | + products derived from this software without specific prior |
| 28 | + written permission. |
| 29 | +
|
| 30 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 31 | +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 32 | +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 33 | +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CALTECH |
| 34 | +OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 35 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 36 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 37 | +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 38 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 39 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 40 | +OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 41 | +SUCH DAMAGE. |
| 42 | +
|
| 43 | +Author: Richard M. Murray |
| 44 | +Date: 6 October 2012 |
| 45 | +
|
| 46 | +$Id: dtime.py 185 2012-08-30 05:44:32Z murrayrm $ |
| 47 | +
|
| 48 | +""" |
| 49 | + |
| 50 | +from scipy.signal import zpk2tf, tf2zpk, cont2discrete |
| 51 | +import numpy as np |
| 52 | +from cmath import exp |
| 53 | +from warnings import warn |
| 54 | +from lti import isctime |
| 55 | +from statesp import StateSpace, _convertToStateSpace |
| 56 | +from xferfcn import TransferFunction, _convertToTransferFunction |
| 57 | + |
| 58 | +# Sample a continuous time system |
| 59 | +def sample_system(sysc, Ts, method='matched'): |
| 60 | + # TODO: add docstring |
| 61 | + |
| 62 | + # Make sure we have a continuous time system |
| 63 | + if not isctime(sysc): |
| 64 | + raise ValueError("First argument must be continuous time system") |
| 65 | + |
| 66 | + # TODO: impelement MIMO version |
| 67 | + if (sysc.inputs != 1 or sysc.outputs != 1): |
| 68 | + raise NotImplementedError("MIMO implementation not available") |
| 69 | + |
| 70 | + # If we are passed a state space system, convert to transfer function first |
| 71 | + if isinstance(sysc, StateSpace): |
| 72 | + warn("sample_system: converting to transfer function") |
| 73 | + sysc = _convertToTransferFunction(sysc) |
| 74 | + |
| 75 | + # Decide what to do based on the methods available |
| 76 | + if method == 'matched': |
| 77 | + sysd = _c2dmatched(sysc, Ts) |
| 78 | + |
| 79 | + elif method == 'tustin': |
| 80 | + sys = [sysc.num[0][0], sysc.den[0][0]] |
| 81 | + scipySysD = cont2discrete(sys, Ts, method='bilinear') |
| 82 | + sysd = TransferFunction(scipySysD[0][0], scipySysD[1], dt) |
| 83 | + |
| 84 | + elif method == 'zoh': |
| 85 | + sys = [sysc.num[0][0], sysc.den[0][0]] |
| 86 | + scipySysD = cont2discrete(sys, Ts, method='zoh') |
| 87 | + sysd = TransferFunction(scipySysD[0][0],scipySysD[1], dt) |
| 88 | + |
| 89 | + elif method == 'foh' or method == 'impulse': |
| 90 | + raise ValueError("Method not developed yet") |
| 91 | + |
| 92 | + else: |
| 93 | + raise ValueError("Invalid discretization method: %s" % method) |
| 94 | + |
| 95 | + # TODO: Convert back into the input form |
| 96 | + # Set sampling time |
| 97 | + return sysd |
| 98 | + |
| 99 | +# c2d function contributed by Benjamin White, Oct 2012 |
| 100 | +def _c2dmatched(sysC, Ts): |
| 101 | + # Pole-zero match method of continuous to discrete time conversion |
| 102 | + szeros, spoles, sgain = tf2zpk(sysC.num[0][0], sysC.den[0][0]) |
| 103 | + zzeros = [0] * len(szeros) |
| 104 | + zpoles = [0] * len(spoles) |
| 105 | + pregainnum = [0] * len(szeros) |
| 106 | + pregainden = [0] * len(spoles) |
| 107 | + for idx, s in enumerate(szeros): |
| 108 | + sTs = s*Ts |
| 109 | + z = exp(sTs) |
| 110 | + zzeros[idx] = z |
| 111 | + pregainnum[idx] = 1-z |
| 112 | + for idx, s in enumerate(spoles): |
| 113 | + sTs = s*Ts |
| 114 | + z = exp(sTs) |
| 115 | + zpoles[idx] = z |
| 116 | + pregainden[idx] = 1-z |
| 117 | + zgain = np.multiply.reduce(pregainnum)/np.multiply.reduce(pregainden) |
| 118 | + gain = sgain/zgain |
| 119 | + sysDnum, sysDden = zpk2tf(zzeros, zpoles, gain) |
| 120 | + return TransferFunction(sysDnum, sysDden, Ts) |
0 commit comments