- Notifications
You must be signed in to change notification settings - Fork441
Add unit test illustrating issue #935 + add method keyword for tf2ss#937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -60,10 +60,10 @@ | ||
from scipy.signal import StateSpace as signalStateSpace | ||
from warnings import warn | ||
from .exception import ControlSlycot, slycot_check, ControlMIMONotImplemented | ||
from .frdata import FrequencyResponseData | ||
from .lti import LTI, _process_frequency_response | ||
from .iosys import InputOutputSystem, common_timebase, isdtime,issiso,\ | ||
_process_iosys_keywords, _process_dt_keyword, _process_signal_list | ||
from .nlsys import NonlinearIOSystem, InterconnectedSystem | ||
from . import config | ||
@@ -1583,6 +1583,13 @@ def ss(*args, **kwargs): | ||
-------- | ||
tf, ss2tf, tf2ss | ||
Notes | ||
----- | ||
If a transfer function is passed as the sole positional argument, the | ||
system will be converted to state space form in the same way as calling | ||
:func:`~control.tf2ss`. The `method` keyword can be used to select the | ||
method for conversion. | ||
Examples | ||
-------- | ||
Create a Linear I/O system object from matrices. | ||
@@ -1615,10 +1622,13 @@ def ss(*args, **kwargs): | ||
warn("state labels specified for " | ||
"non-unique state space realization") | ||
# Allow method to be specified (eg, tf2ss) | ||
method = kwargs.pop('method', None) | ||
# Create a state space system from an LTI system | ||
sys = StateSpace( | ||
_convert_to_statespace( | ||
sys, method=method, | ||
use_prefix_suffix=not sys._generic_name_check()), | ||
**kwargs) | ||
@@ -1765,6 +1775,10 @@ def tf2ss(*args, **kwargs): | ||
name : string, optional | ||
System name. If unspecified, a generic name <sys[id]> is generated | ||
with a unique integer id. | ||
method : str, optional | ||
Set the method used for computing the result. Current methods are | ||
'slycot' and 'scipy'. If set to None (default), try 'slycot' first | ||
and then 'scipy' (SISO only). | ||
Raises | ||
------ | ||
@@ -1781,6 +1795,13 @@ def tf2ss(*args, **kwargs): | ||
tf | ||
ss2tf | ||
Notes | ||
----- | ||
The ``slycot`` routine used to convert a transfer function into state | ||
space form appears to have a bug and in some (rare) instances may not | ||
return a system with the same poles as the input transfer function. | ||
For SISO systems, setting ``method=scipy`` can be used as an alternative. | ||
Examples | ||
-------- | ||
>>> num = [[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]]] | ||
@@ -2189,7 +2210,7 @@ def _f2s(f): | ||
return s | ||
def _convert_to_statespace(sys, use_prefix_suffix=False, method=None): | ||
"""Convert a system to state space form (if needed). | ||
If sys is already a state space, then it is returned. If sys is a | ||
@@ -2213,13 +2234,17 @@ def _convert_to_statespace(sys, use_prefix_suffix=False): | ||
raise ValueError("transfer function is non-proper; can't " | ||
"convert to StateSpace system") | ||
if method is None and slycot_check() or method == 'slycot': | ||
if not slycot_check(): | ||
raise ValueError("method='slycot' requires slycot") | ||
from slycot import td04ad | ||
# Change the numerator and denominator arrays so that the transfer | ||
# function matrix has a common denominator. | ||
# matrices are also sized/padded to fit td04ad | ||
num, den, denorder = sys.minreal()._common_den() | ||
num, den, denorder = sys._common_den() | ||
# transfer function to state space conversion now should work! | ||
ssout = td04ad('C', sys.ninputs, sys.noutputs, | ||
@@ -2230,9 +2255,8 @@ def _convert_to_statespace(sys, use_prefix_suffix=False): | ||
ssout[1][:states, :states], ssout[2][:states, :sys.ninputs], | ||
ssout[3][:sys.noutputs, :states], ssout[4], sys.dt) | ||
elif method in [None, 'scipy']: | ||
# Scipy tf->ss can't handle MIMO, but SISO is OK | ||
maxn = max(max(len(n) for n in nrow) | ||
for nrow in sys.num) | ||
maxd = max(max(len(d) for d in drow) | ||
@@ -2244,12 +2268,15 @@ def _convert_to_statespace(sys, use_prefix_suffix=False): | ||
D[i, j] = sys.num[i][j][0] / sys.den[i][j][0] | ||
newsys = StateSpace([], [], [], D, sys.dt) | ||
else: | ||
if not issiso(sys): | ||
raise ControlMIMONotImplemented( | ||
"MIMO system conversion not supported without Slycot") | ||
A, B, C, D = \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change for the two lines above:
| ||
sp.signal.tf2ss(squeeze(sys.num), squeeze(sys.den)) | ||
newsys = StateSpace(A, B, C, D, sys.dt) | ||
else: | ||
raise ValueError(f"unknown {method=}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. First instantiation of self-documenting F-strings in python-control? : ) | ||
# Copy over the signal (and system) names | ||
newsys._copy_names( | ||