Skip to content

Commit 81ee887

Browse files
committed
Updated sample_system() function to check for proper version of SciPy. If cont2discrete is not avialable (requires scipy 0.10.0+), generate a warning.
1 parent 9642d11 commit 81ee887

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2013-07-14 Richard Murray <murray@altura-2.local>
2+
3+
* src/dtime.py (sample_system): added check to make sure
4+
cont2discrete is available in scipy.signal
5+
(sample_system): added docstring
6+
17
2013-06-24 Richard Murray <murray@altura-2.local>
28

39
* tests/minreal_test.py (TestMinreal.testMinrealBrute),

src/dtime.py

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
4848
"""
4949

50-
from scipy.signal import zpk2tf, tf2zpk, cont2discrete
50+
from scipy.signal import zpk2tf, tf2zpk
5151
import numpy as np
5252
from cmath import exp
5353
from warnings import warn
@@ -57,7 +57,38 @@
5757

5858
# Sample a continuous time system
5959
def sample_system(sysc, Ts, method='matched'):
60-
# TODO: add docstring
60+
"""Convert a continuous time system to discrete time
61+
62+
Creates a discrete time system from a continuous time system by
63+
sampling. Multiple methods of conversion are supported.
64+
65+
Parameters
66+
----------
67+
sysc : linsys
68+
Continuous time system to be converted
69+
Ts : real
70+
Sampling period
71+
method : string
72+
Method to use for conversion: 'matched' (default), 'tustin', 'zoh'
73+
74+
Returns
75+
-------
76+
sysd : linsys
77+
Discrete time system, with sampling rate Ts
78+
79+
Notes
80+
-----
81+
1. The conversion methods 'tustin' and 'zoh' require the
82+
cont2discrete() function, including in SciPy 0.10.0 and above.
83+
84+
2. Additional methods 'foh' and 'impulse' are planned for future
85+
implementation.
86+
87+
Examples
88+
--------
89+
>>> sysc = TransferFunction([1], [1, 2, 1])
90+
>>> sysd = sample_system(sysc, 1, method='matched')
91+
"""
6192

6293
# Make sure we have a continuous time system
6394
if not isctime(sysc):
@@ -77,14 +108,22 @@ def sample_system(sysc, Ts, method='matched'):
77108
sysd = _c2dmatched(sysc, Ts)
78109

79110
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-
111+
try:
112+
from scipy.signal import cont2discrete
113+
sys = [sysc.num[0][0], sysc.den[0][0]]
114+
scipySysD = cont2discrete(sys, Ts, method='bilinear')
115+
sysd = TransferFunction(scipySysD[0][0], scipySysD[1], dt)
116+
except ImportError:
117+
raise TypeError("cont2discrete not found in scipy.signal; upgrade to v0.10.0+")
118+
84119
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)
120+
try:
121+
from scipy.signal import cont2discrete
122+
sys = [sysc.num[0][0], sysc.den[0][0]]
123+
scipySysD = cont2discrete(sys, Ts, method='zoh')
124+
sysd = TransferFunction(scipySysD[0][0],scipySysD[1], dt)
125+
except ImportError:
126+
raise TypeError("cont2discrete not found in scipy.signal; upgrade to v0.10.0+")
88127

89128
elif method == 'foh' or method == 'impulse':
90129
raise ValueError("Method not developed yet")

0 commit comments

Comments
 (0)