Skip to content

Commit ee0bd9c

Browse files
committed
working on matrix feedback with state-space and frd
1 parent 82ac4d2 commit ee0bd9c

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/frdata.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ def feedback(self, other, sign=-1):
389389
for j in range(self.outputs):
390390
fresp[i, j, k] = \
391391
self.fresp[i, j, k] / \
392-
(1.0-sign*inner(self.fresp[:, j, k], other.fresp[i, :, k]))
392+
(1.0-sign*inner(self.fresp[:, j, k],
393+
other.fresp[i, :, k]))
393394

394395
return FRD(fresp, other.omega)
395396

src/statesp.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
from numpy.linalg import inv, det, solve
8181
from numpy.linalg.linalg import LinAlgError
8282
from scipy.signal import lti
83+
from exceptions import Exception
8384
import warnings
8485
from lti import Lti
8586
import xferfcn
@@ -505,8 +506,18 @@ def _convertToStateSpace(sys, **kw):
505506
# return StateSpace([[]], [[]], [[]], eye(outputs, inputs))
506507
return StateSpace(0., zeros((1, inputs)), zeros((outputs, 1)),
507508
sys * ones((outputs, inputs)))
508-
else:
509-
raise TypeError("Can't convert given type to StateSpace system.")
509+
510+
# If this is a matrix, try to create a constant feedthrough
511+
try:
512+
D = matrix(sys)
513+
outputs, inputs = D.shape
514+
515+
return StateSpace(0., zeros((1, inputs)), zeros((outputs, 1)), D)
516+
except Exception, e:
517+
print "Failure to assume argument is matrix-like in" \
518+
" _convertToStateSpace, result %s" % e
519+
520+
raise TypeError("Can't convert given type to StateSpace system.")
510521

511522
def _rss_generate(states, inputs, outputs, type):
512523
"""Generate a random state space.

tests/frd_test.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,47 @@ def testAuto(self):
9898
f2 = _convertToFRD(np.matrix([[1, 0], [0.1, -1]]), omega)
9999
f2 = _convertToFRD([[1, 0], [0.1, -1]], omega)
100100

101-
def testBode(self):
101+
def testNyquist(self):
102102
h1 = TransferFunction([1], [1, 2, 2])
103103
omega = np.logspace(-1, 2, 40)
104104
f1 = FRD(h1, omega)
105105
control.freqplot.nyquist(f1, np.logspace(-1, 2, 100))
106+
plt.savefig('/dev/null', format='svg')
106107
plt.figure(2)
107108
control.freqplot.nyquist(f1, f1.omega)
108-
plt.show()
109+
plt.savefig('/dev/null', format='svg')
109110

111+
def testMIMO(self):
112+
sys = StateSpace([[-0.5, 0.0], [0.0, -1.0]],
113+
[[1.0, 0.0], [0.0, 1.0]],
114+
[[1.0, 0.0], [0.0, 1.0]],
115+
[[0.0, 0.0], [0.0, 0.0]])
116+
omega = np.logspace(-1, 2, 10)
117+
f1 = FRD(sys, omega)
118+
np.testing.assert_array_almost_equal(
119+
sys.freqresp([0.1, 1.0, 10])[0],
120+
f1.freqresp([0.1, 1.0, 10])[0])
121+
np.testing.assert_array_almost_equal(
122+
sys.freqresp([0.1, 1.0, 10])[1],
123+
f1.freqresp([0.1, 1.0, 10])[1])
124+
125+
def testMIMOfb(self):
126+
sys = StateSpace([[-0.5, 0.0], [0.0, -1.0]],
127+
[[1.0, 0.0], [0.0, 1.0]],
128+
[[1.0, 0.0], [0.0, 1.0]],
129+
[[0.0, 0.0], [0.0, 0.0]])
130+
omega = np.logspace(-1, 2, 10)
131+
f1 = FRD(sys, omega).feedback([[0.1, 0.3],[0.0, 1.0]])
132+
f2 = FRD(sys.feedback([[0.1, 0.3],[0.0, 1.0]]), omega)
133+
print f1 - f2
134+
print f1
135+
print f2
136+
#np.testing.assert_array_almost_equal(
137+
# sys.feedback([[0.1, 0.3],[0.0, 1.0]]).freqresp([0.1, 1.0, 10])[0],
138+
# f1.feedback([[0.1, 0.3],[0.0, 1.0]]).freqresp([0.1, 1.0, 10])[0])
139+
#np.testing.assert_array_almost_equal(
140+
# sys.feedback([[0.1, 0.3],[0.0, 1.0]]).freqresp([0.1, 1.0, 10])[1],
141+
# f1.feedback([[0.1, 0.3],[0.0, 1.0]]).freqresp([0.1, 1.0, 10])[1])
110142
if __name__ == "__main__":
111143
unittest.main()
112144
sys.exit(0)

0 commit comments

Comments
 (0)