Skip to content

Commit aa6e0df

Browse files
committed
replace np.pi with math.pi (avoids Mock() issues) + docstring update in margin
1 parent 314c4eb commit aa6e0df

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

control/ctrlutil.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,12 @@
4343
# Packages that we need access to
4444
from . import lti
4545
import numpy as np
46-
from numpy import pi
47-
48-
# Hack for sphinx.ext.autodoc: if numpy is a mock import, then numpy.pi
49-
# will be assigned to _Mock() and this generates a type error
50-
if not isinstance(pi, float):
51-
pi = 3.14
46+
import math
5247

5348
__all__ = ['unwrap', 'issys', 'db2mag', 'mag2db']
5449

5550
# Utility function to unwrap an angle measurement
56-
def unwrap(angle, period=2*pi):
51+
def unwrap(angle, period=2*math.pi):
5752
"""Unwrap a phase angle to give a continuous curve
5853
5954
Parameters

control/freqplot.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import matplotlib.pyplot as plt
4545
import scipy as sp
4646
import numpy as np
47+
import math
4748
from .ctrlutil import unwrap
4849
from .bdalg import feedback
4950

@@ -128,7 +129,7 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
128129
else:
129130
omega_limits = np.array(omega_limits)
130131
if Hz:
131-
omega_limits *= 2.*np.pi
132+
omega_limits *= 2.*math.pi
132133
if omega_num:
133134
omega = sp.logspace(np.log10(omega_limits[0]), np.log10(omega_limits[1]), num=omega_num, endpoint=True)
134135
else:
@@ -142,7 +143,7 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
142143
else:
143144
omega_sys = np.array(omega)
144145
if sys.isdtime(True):
145-
nyquistfrq = 2. * np.pi * 1. / sys.dt / 2.
146+
nyquistfrq = 2. * math.pi * 1. / sys.dt / 2.
146147
omega_sys = omega_sys[omega_sys < nyquistfrq]
147148
# TODO: What distance to the Nyquist frequency is appropriate?
148149
else:
@@ -154,9 +155,9 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
154155
phase = unwrap(phase)
155156
nyquistfrq_plot = None
156157
if Hz:
157-
omega_plot = omega_sys / (2. * np.pi)
158+
omega_plot = omega_sys / (2. * math.pi)
158159
if nyquistfrq:
159-
nyquistfrq_plot = nyquistfrq / (2. * np.pi)
160+
nyquistfrq_plot = nyquistfrq / (2. * math.pi)
160161
else:
161162
omega_plot = omega_sys
162163
if nyquistfrq:
@@ -187,7 +188,7 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
187188
# Phase plot
188189
ax_phase = plt.subplot(212, sharex=ax_mag);
189190
if deg:
190-
phase_plot = phase * 180. / np.pi
191+
phase_plot = phase * 180. / math.pi
191192
else:
192193
phase_plot = phase
193194
ax_phase.semilogx(omega_plot, phase_plot, *args, **kwargs)
@@ -208,8 +209,8 @@ def genZeroCenteredSeries(val_min, val_max, period):
208209
ax_phase.set_yticks(genZeroCenteredSeries(ylim[0], ylim[1], 15.), minor=True)
209210
else:
210211
ylim = ax_phase.get_ylim()
211-
ax_phase.set_yticks(genZeroCenteredSeries(ylim[0], ylim[1], np.pi / 4.))
212-
ax_phase.set_yticks(genZeroCenteredSeries(ylim[0], ylim[1], np.pi / 12.), minor=True)
212+
ax_phase.set_yticks(genZeroCenteredSeries(ylim[0], ylim[1], math.pi / 4.))
213+
ax_phase.set_yticks(genZeroCenteredSeries(ylim[0], ylim[1], math.pi / 12.), minor=True)
213214
ax_phase.grid(True, which='both')
214215
# ax_mag.grid(which='minor', alpha=0.3)
215216
# ax_mag.grid(which='major', alpha=0.9)
@@ -449,7 +450,7 @@ def default_frequency_range(syslist, Hz=None, number_of_samples=None, feature_pe
449450
features_ = features_[features_ != 0.0];
450451
features = np.concatenate((features, features_))
451452
elif sys.isdtime(strict=True):
452-
fn = np.pi * 1. / sys.dt
453+
fn = math.pi * 1. / sys.dt
453454
# TODO: What distance to the Nyquist frequency is appropriate?
454455
freq_interesting.append(fn * 0.9)
455456

@@ -475,12 +476,12 @@ def default_frequency_range(syslist, Hz=None, number_of_samples=None, feature_pe
475476
features = np.array([1.]);
476477

477478
if Hz:
478-
features /= 2.*np.pi
479+
features /= 2.*math.pi
479480
features = np.log10(features)
480481
lsp_min = np.floor(np.min(features) - feature_periphery_decade)
481482
lsp_max = np.ceil(np.max(features) + feature_periphery_decade)
482-
lsp_min += np.log10(2.*np.pi)
483-
lsp_max += np.log10(2.*np.pi)
483+
lsp_min += np.log10(2.*math.pi)
484+
lsp_max += np.log10(2.*math.pi)
484485
else:
485486
features = np.log10(features)
486487
lsp_min = np.floor(np.min(features) - feature_periphery_decade)

control/margins.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@
5050
$Id$
5151
"""
5252

53+
import math
5354
import numpy as np
55+
import scipy as sp
5456
from . import xferfcn
5557
from .lti import issiso
5658
from . import frdata
57-
import scipy as sp
5859

5960
__all__ = ['stability_margins', 'phase_crossover_frequencies', 'margin']
6061

@@ -140,7 +141,7 @@ def stability_margins(sysdata, returnall=False, epsw=0.0):
140141
sys = sysdata
141142
elif getattr(sysdata, '__iter__', False) and len(sysdata) == 3:
142143
mag, phase, omega = sysdata
143-
sys = frdata.FRD(mag * np.exp(1j * phase * np.pi/180),
144+
sys = frdata.FRD(mag * np.exp(1j * phase * math.pi/180),
144145
omega, smooth=True)
145146
else:
146147
sys = xferfcn._convertToTransferFunction(sysdata)
@@ -336,13 +337,13 @@ def phase_crossover_frequencies(sys):
336337

337338

338339
def margin(*args):
339-
"""margin(sys)
340+
"""margin(sysdata)
340341
341342
Calculate gain and phase margins and associated crossover frequencies
342343
343344
Parameters
344345
----------
345-
sysdata: LTI system or (mag, phase, omega) sequence
346+
sysdata : LTI system or (mag, phase, omega) sequence
346347
sys : StateSpace or TransferFunction
347348
Linear SISO system
348349
mag, phase, omega : sequence of array_like

control/statesp.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@
5151
$Id$
5252
"""
5353

54+
import math
5455
import numpy as np
5556
from numpy import all, angle, any, array, asarray, concatenate, cos, delete, \
56-
dot, empty, exp, eye, matrix, ones, pi, poly, poly1d, roots, shape, sin, \
57+
dot, empty, exp, eye, matrix, ones, poly, poly1d, roots, shape, sin, \
5758
zeros, squeeze
5859
from numpy.random import rand, randn
5960
from numpy.linalg import solve, eigvals, matrix_rank
@@ -367,7 +368,7 @@ def evalfr(self, omega):
367368
if isdtime(self, strict=True):
368369
dt = timebase(self)
369370
s = exp(1.j * omega * dt)
370-
if (omega * dt > pi):
371+
if (omega * dt > math.pi):
371372
warnings.warn("evalfr: frequency evaluation above Nyquist frequency")
372373
else:
373374
s = omega * 1.j
@@ -798,7 +799,7 @@ def _rss_generate(states, inputs, outputs, type):
798799
poles[i] = complex(-exp(randn()), 3. * exp(randn()))
799800
elif type == 'd':
800801
mag = rand()
801-
phase = 2. * pi * rand()
802+
phase = 2. * math.pi * rand()
802803
poles[i] = complex(mag * cos(phase),
803804
mag * sin(phase))
804805
poles[i+1] = complex(poles[i].real, -poles[i].imag)

0 commit comments

Comments
 (0)