Skip to content

Solve #862 and #864: bode_plot phase wrapping incorrect for multiple systems #863

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 5 commits into from
Feb 18, 2023
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
28 changes: 17 additions & 11 deletions control/freqplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,21 @@ def bode_plot(syslist, omega=None,
if initial_phase is None:
# Start phase in the range 0 to -360 w/ initial phase = -180
# If wrap_phase is true, use 0 instead (phase \in (-pi, pi])
initial_phase = -math.pi if wrap_phase is not True else 0
initial_phase_value = -math.pi if wrap_phase is not True else 0
elif isinstance(initial_phase, (int, float)):
# Allow the user to override the default calculation
if deg:
initial_phase = initial_phase/180. * math.pi
initial_phase_value = initial_phase/180. * math.pi
else:
initial_phase_value = initial_phase

else:
raise ValueError("initial_phase must be a number.")

# Shift the phase if needed
if abs(phase[0] - initial_phase) > math.pi:
if abs(phase[0] - initial_phase_value) > math.pi:
phase -= 2*math.pi * \
round((phase[0] - initial_phase) / (2*math.pi))
round((phase[0] - initial_phase_value) / (2*math.pi))

# Phase wrapping
if wrap_phase is False:
Expand Down Expand Up @@ -1021,9 +1024,11 @@ def _parse_linestyle(style_name, allow_false=False):
# Plot the scaled sections of the curve (changing linestyle)
x_scl = np.ma.masked_where(scale_mask, resp.real)
y_scl = np.ma.masked_where(scale_mask, resp.imag)
plt.plot(
x_scl * (1 + curve_offset), y_scl * (1 + curve_offset),
primary_style[1], color=c, **kwargs)
if x_scl.count() >= 1 and y_scl.count() >= 1:
plt.plot(
x_scl * (1 + curve_offset),
y_scl * (1 + curve_offset),
primary_style[1], color=c, **kwargs)

# Plot the primary curve (invisible) for setting arrows
x, y = resp.real.copy(), resp.imag.copy()
Expand All @@ -1041,10 +1046,11 @@ def _parse_linestyle(style_name, allow_false=False):
# Plot the regular and scaled segments
plt.plot(
x_reg, -y_reg, mirror_style[0], color=c, **kwargs)
plt.plot(
x_scl * (1 - curve_offset),
-y_scl * (1 - curve_offset),
mirror_style[1], color=c, **kwargs)
if x_scl.count() >= 1 and y_scl.count() >= 1:
plt.plot(
x_scl * (1 - curve_offset),
-y_scl * (1 - curve_offset),
mirror_style[1], color=c, **kwargs)

# Add the arrows (on top of an invisible contour)
x, y = resp.real.copy(), resp.imag.copy()
Expand Down
12 changes: 12 additions & 0 deletions control/tests/freqresp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,18 @@ def test_phase_wrap(TF, wrap_phase, min_phase, max_phase):
assert(max(phase) <= max_phase)


def test_phase_wrap_multiple_systems():
sys_unstable = ctrl.zpk([],[1,1], gain=1)

mag, phase, omega = ctrl.bode(sys_unstable, plot=False)
assert(np.min(phase) >= -2*np.pi)
assert(np.max(phase) <= -1*np.pi)

mag, phase, omega = ctrl.bode((sys_unstable, sys_unstable), plot=False)
assert(np.min(phase) >= -2*np.pi)
assert(np.max(phase) <= -1*np.pi)


def test_freqresp_warn_infinite():
"""Test evaluation warnings for transfer functions w/ pole at the origin"""
sys_finite = ctrl.tf([1], [1, 0.01])
Expand Down