Skip to content

Commit 35cebfb

Browse files
committed
Added the clean_ss() function
At the very bottom of the existing document I added the clean_ss() function, adjusting it slightly to confirm with the existing imports in the document. Re-wrote the documentation to have the same style as the rest of the document
1 parent d3142ff commit 35cebfb

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

control/statesp.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
from scipy.signal import lti, cont2discrete
6363
from warnings import warn
6464
from .lti import LTI, timebase, timebaseEqual, isdtime
65+
from .xferfcn import TransferFunction
6566
from . import config
6667
from copy import deepcopy
6768

@@ -1503,3 +1504,33 @@ def ssdata(sys):
15031504
"""
15041505
ss = _convertToStateSpace(sys)
15051506
return ss.A, ss.B, ss.C, ss.D
1507+
1508+
1509+
def clean_ss(sys, precision=10):
1510+
"""
1511+
Rounds the A, B, C and D matrices of a StateSpace function, removing small non-zero numbers that are, for instance,
1512+
a result of computational errors and that can otherwise lead to problems in future computations and analyses
1513+
1514+
Parameters
1515+
----------
1516+
sys : LTI (StateSpace, or TransferFunction)
1517+
LTI system whose data will be returned
1518+
precision: integer
1519+
Number of decimals to be left unchanged in rounding
1520+
1521+
Returns
1522+
-------
1523+
out: StateSpace
1524+
LTI StateSpace rounded to the desired precision
1525+
1526+
Raises
1527+
------
1528+
TypeError
1529+
if sys is not a StateSpace or TransferFunction object
1530+
"""
1531+
1532+
if (type(sys) is not TransferFunction) and (type(sys) is not StateSpace):
1533+
raise TypeError("Neither a StateSpace nor TransferFunction was passed to 'sys'")
1534+
sys = ss(sys)
1535+
return ss(np.round(sys.A, precision), np.round(sys.B, precision), np.round(sys.C, precision),
1536+
np.round(sys.D, precision))

0 commit comments

Comments
 (0)