Skip to content

Commit d3aaafb

Browse files
committed
Added clean_tf() function
Added the clean_tf() function at the bottom of xferfcn.py, adjusting it slightly to be conform with the existing imports in the file and adding one import from another file of the control library. Also adjusted the documentation for the function to match the existing style. Additionally fixed a formatting error in the documentation for clean_ss()
1 parent 35cebfb commit d3aaafb

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

control/statesp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,12 +1515,12 @@ def clean_ss(sys, precision=10):
15151515
----------
15161516
sys : LTI (StateSpace, or TransferFunction)
15171517
LTI system whose data will be returned
1518-
precision: integer
1518+
precision : integer
15191519
Number of decimals to be left unchanged in rounding
15201520
15211521
Returns
15221522
-------
1523-
out: StateSpace
1523+
out : StateSpace
15241524
LTI StateSpace rounded to the desired precision
15251525
15261526
Raises

control/xferfcn.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from itertools import chain
6464
from re import sub
6565
from .lti import LTI, timebaseEqual, timebase, isdtime
66+
from .statesp import StateSpace
6667
from . import config
6768

6869
__all__ = ['TransferFunction', 'tf', 'ss2tf', 'tfdata']
@@ -1565,6 +1566,56 @@ def _clean_part(data):
15651566
return data
15661567

15671568

1569+
def clean_tf(sys, input=None, output=None, precision=10):
1570+
"""
1571+
Rounds the elements of the desired transfer function. This removes small non-zero numbers that, for instance, are
1572+
a result of computational errors and can otherwise lead to problems. The function also removes cancelling poles
1573+
and zeros, similar to the control.minreal() function
1574+
1575+
Parameters
1576+
----------
1577+
sys : LTI (StateSpace, or TransferFunction)
1578+
LTI system whose data will be returned
1579+
input : None
1580+
optionally an integer referring to the requested input (starting from 1)
1581+
output : None
1582+
optionally an integer referring to the requested output (starting from 1)
1583+
precision : integer
1584+
Number of decimals to be left unchanged in rounding
1585+
1586+
Returns
1587+
-------
1588+
out : TransferFunctino
1589+
LTI TransferFunction rounded to the desired precision
1590+
1591+
Raises
1592+
------
1593+
TypeError
1594+
if sys is not a StateSpace or TransferFunction object
1595+
"""
1596+
if (type(sys) is not TransferFunction) and (type(sys) is not StateSpace):
1597+
raise Exception("Neither a StateSpace nor TransferFunction was passed to 'sys'")
1598+
1599+
sys = tf(sys).minreal()
1600+
if input is not None and output is not None:
1601+
sys = sys[output - 1, input - 1]
1602+
elif output is not None:
1603+
sys = sys[output - 1, :]
1604+
elif input is not None:
1605+
sys = sys[:, input - 1]
1606+
num = np.round(sys.num, precision)
1607+
den = np.round(sys.den, precision)
1608+
for i in range(sys.outputs):
1609+
for j in range(sys.inputs):
1610+
test = True
1611+
while test:
1612+
if num[i][j][-1] == den[i][j][-1] == 0:
1613+
num[i][j] = np.insert(num[i][j][:-1],0,0)
1614+
den[i][j] = np.insert(den[i][j][:-1],0,0)
1615+
else: test = False
1616+
return tf(num,den)
1617+
1618+
15681619
# Define constants to represent differentiation, unit delay
15691620
TransferFunction.s = TransferFunction([1, 0], [1], 0)
15701621
TransferFunction.z = TransferFunction([1, 0], [1], True)

0 commit comments

Comments
 (0)