Skip to content

discard zero imaginary part for sys.dcgain() #579

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions control/lti.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ def dcgain(self):
raise NotImplementedError("dcgain not implemented for %s objects" %
str(self.__class__))

def _dcgain(self, warn_infinite):
zeroresp = self(0 if self.isctime() else 1,
warn_infinite=warn_infinite)
if np.all(np.logical_or(np.isreal(zeroresp), np.isnan(zeroresp.imag))):
return zeroresp.real
else:
return zeroresp

# Test to see if a system is SISO
def issiso(sys, strict=False):
Expand Down
25 changes: 18 additions & 7 deletions control/statesp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,16 +1216,27 @@ def dcgain(self, warn_infinite=False):

.. math: G(1) = C (I - A)^{-1} B + D

Parameters
----------
warn_infinite : bool, optional
By default, don't issue a warning message if the zero-frequency
gain is infinite. Setting `warn_infinite` to generate the warning
message.

Returns
-------
gain : ndarray
An array of shape (outputs,inputs); the array will either be the
zero-frequency (or DC) gain, or, if the frequency response is
singular, the array will be filled with (inf + nanj).

gain : (outputs, inputs) ndarray or scalar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gain : (noutputs, ninputs) ndarray or scalar

Array or scalar value for SISO systems, depending on
config.defaults['control.squeeze_frequency_response'].
The value of the array elements or the scalar is either the
zero-frequency (or DC) gain, or `inf`, if the frequency response
is singular.

For real valued systems, the empty imaginary part of the
complex zero-frequency response is discarded and a real array or
scalar is returned.
"""
return self(0, warn_infinite=warn_infinite) if self.isctime() \
else self(1, warn_infinite=warn_infinite)
return self._dcgain(warn_infinite)

def _isstatic(self):
"""True if and only if the system has no dynamics, that is,
Expand Down
10 changes: 5 additions & 5 deletions control/tests/freqresp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,9 @@ def test_dcgain_consistency():
np.testing.assert_equal(
sys_ss(0j, warn_infinite=False), complex(np.inf, np.nan))
np.testing.assert_equal(
sys_tf.dcgain(warn_infinite=False), complex(np.inf, np.nan))
sys_tf.dcgain(), np.inf)
np.testing.assert_equal(
sys_ss.dcgain(warn_infinite=False), complex(np.inf, np.nan))
sys_ss.dcgain(), np.inf)

# Set up transfer function with pole, zero at the origin
sys_tf = ctrl.tf([1, 0], [1, 0])
Expand All @@ -448,7 +448,7 @@ def test_dcgain_consistency():
np.testing.assert_equal(
sys_tf(0j, warn_infinite=False), complex(np.nan, np.nan))
np.testing.assert_equal(
sys_tf.dcgain(warn_infinite=False), complex(np.nan, np.nan))
sys_tf.dcgain(), np.nan)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a test to show that dcgain() on a system with negative gain at zero frequency returns a negative number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already exists:

# Make sure that we get the *signed* DC gain
sys_tf = -1 / (s + 1)
np.testing.assert_almost_equal(sys_tf.dcgain(), -1)

# Set up state space version
sys_ss = ctrl.tf2ss(ctrl.tf([1, 0], [1, 1])) * \
Expand All @@ -462,15 +462,15 @@ def test_dcgain_consistency():
np.testing.assert_equal(
sys_ss(0j, warn_infinite=False), complex(np.nan, np.nan))
np.testing.assert_equal(
sys_ss.dcgain(warn_infinite=False), complex(np.nan, np.nan))
sys_ss.dcgain(), np.nan)
elif 0 in sys_ss.pole():
# Pole at the origin, but zero elsewhere => should get (inf + nanj)
np.testing.assert_equal(
sys_ss(0, warn_infinite=False), complex(np.inf, np.nan))
np.testing.assert_equal(
sys_ss(0j, warn_infinite=False), complex(np.inf, np.nan))
np.testing.assert_equal(
sys_ss.dcgain(warn_infinite=False), complex(np.inf, np.nan))
sys_ss.dcgain(), np.inf)
else:
# Near pole/zero cancellation => nothing sensible to check
pass
Expand Down
7 changes: 3 additions & 4 deletions control/tests/statesp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def test_dc_gain_cont(self):
np.testing.assert_allclose(sys2.dcgain(), expected)

sys3 = StateSpace(0., 1., 1., 0.)
np.testing.assert_equal(sys3.dcgain(), complex(np.inf, np.nan))
np.testing.assert_equal(sys3.dcgain(), np.inf)

def test_dc_gain_discr(self):
"""Test DC gain for discrete-time state-space systems."""
Expand All @@ -516,7 +516,7 @@ def test_dc_gain_discr(self):

# summer
sys = StateSpace(1, 1, 1, 0, True)
np.testing.assert_equal(sys.dcgain(), complex(np.inf, np.nan))
np.testing.assert_equal(sys.dcgain(), np.inf)

@pytest.mark.parametrize("outputs", range(1, 6))
@pytest.mark.parametrize("inputs", range(1, 6))
Expand All @@ -539,7 +539,7 @@ def test_dc_gain_integrator(self, outputs, inputs, dt):
c = np.eye(max(outputs, states))[:outputs, :states]
d = np.zeros((outputs, inputs))
sys = StateSpace(a, b, c, d, dt)
dc = np.full_like(d, complex(np.inf, np.nan), dtype=complex)
dc = np.full_like(d, np.inf, dtype=float)
if sys.issiso():
dc = dc.squeeze()

Expand Down Expand Up @@ -953,4 +953,3 @@ def test_xferfcn_ndarray_precedence(op, tf, arr):
ss = ct.tf2ss(tf)
result = op(arr, ss)
assert isinstance(result, ct.StateSpace)

6 changes: 3 additions & 3 deletions control/tests/xferfcn_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ def test_dcgain_cont(self):
np.testing.assert_equal(sys2.dcgain(), 2)

sys3 = TransferFunction(6, [1, 0])
np.testing.assert_equal(sys3.dcgain(), complex(np.inf, np.nan))
np.testing.assert_equal(sys3.dcgain(), np.inf)

num = [[[15], [21], [33]], [[10], [14], [22]]]
den = [[[1, 3], [2, 3], [3, 3]], [[1, 5], [2, 7], [3, 11]]]
Expand All @@ -827,13 +827,13 @@ def test_dcgain_discr(self):

# differencer
sys = TransferFunction(1, [1, -1], True)
np.testing.assert_equal(sys.dcgain(), complex(np.inf, np.nan))
np.testing.assert_equal(sys.dcgain(), np.inf)

# differencer, with warning
sys = TransferFunction(1, [1, -1], True)
with pytest.warns(RuntimeWarning, match="divide by zero"):
np.testing.assert_equal(
sys.dcgain(warn_infinite=True), complex(np.inf, np.nan))
sys.dcgain(warn_infinite=True), np.inf)

# summer
sys = TransferFunction([1, -1], [1], True)
Expand Down
15 changes: 11 additions & 4 deletions control/xferfcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,12 +1070,19 @@ def dcgain(self, warn_infinite=False):

Returns
-------
gain : ndarray
The zero-frequency gain
gain : (outputs, inputs) ndarray or scalar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gain : (noutputs, ninputs) ndarray or scalar

Array or scalar value for SISO systems, depending on
config.defaults['control.squeeze_frequency_response'].
The value of the array elements or the scalar is either the
zero-frequency (or DC) gain, or `inf`, if the frequency response
is singular.

For real valued systems, the empty imaginary part of the
complex zero-frequency response is discarded and a real array or
scalar is returned.

"""
return self(0, warn_infinite=warn_infinite) if self.isctime() \
else self(1, warn_infinite=warn_infinite)
return self._dcgain(warn_infinite)

def _isstatic(self):
"""returns True if and only if all of the numerator and denominator
Expand Down