Skip to content

Commit e3b087f

Browse files
author
Kevin Chen
committed
Bug fixes in convertToStateSpace; new TestConvert.py
Kevin K. Chen <kkchen@princeton.edu>
1 parent b728861 commit e3b087f

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

src/TestConvert.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
3+
import numpy as np
4+
import matlab
5+
import unittest
6+
7+
class TestConvert(unittest.TestCase):
8+
"""Test state space and transfer function conversions."""
9+
10+
def setUp(self):
11+
"""Set up testing parameters."""
12+
13+
# Number of times to run each of the randomized tests.
14+
self.numTests = 10
15+
# Maximum number of states to test + 1
16+
self.maxStates = 5
17+
# Maximum number of inputs and outputs to test + 1
18+
self.maxIO = 5
19+
20+
def testConvert(self):
21+
"""Test state space to transfer function conversion."""
22+
23+
for states in range(1, self.maxStates):
24+
for inputs in range(1, self.maxIO):
25+
for outputs in range(1, self.maxIO):
26+
sys = matlab.rss(states, inputs, outputs)
27+
print "sys1:\n", sys
28+
sys2 = matlab.tf(sys)
29+
print "sys2:\n", sys2
30+
sys3 = matlab.ss(sys2)
31+
print "sys3:\n", sys3
32+
sys4 = matlab.tf(sys3)
33+
print "sys4:\n", sys4
34+
35+
if __name__ == "__main__":
36+
unittest.main()

src/TestStateSp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env python
22

33
import numpy as np
4+
import unittest
45
import matlab
56
from statesp import StateSpace
6-
import unittest
77

88
class TestStateSpace(unittest.TestCase):
99
"""Tests for the StateSpace class."""

src/statesp.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,13 +433,18 @@ def convertToStateSpace(sys, **kw):
433433
# function matrix has a common denominator.
434434
num, den = sys._common_den()
435435
# Make a list of the orders of the denominator polynomials.
436-
index = [len(den) for i in range(sys.outputs)]
436+
index = [len(den) - 1 for i in range(sys.outputs)]
437437
# Repeat the common denominator along the rows.
438438
den = array([den for i in range(sys.outputs)])
439439

440+
print "outputs = %g\n" % sys.outputs
440441
ssout = td04ad(sys.inputs, sys.outputs, index, den, num)
441442

442-
return StateSpace(ssout[1], ssout[2], ssout[3], ssout[4])
443+
states = ssout[0]
444+
return StateSpace(ssout[1][:states, :states],
445+
ssout[2][:states, :sys.inputs],
446+
ssout[3][:sys.outputs, :states],
447+
ssout[4])
443448
elif isinstance(sys, (int, long, float, complex)):
444449
if "inputs" in kw:
445450
inputs = kw["inputs"]

0 commit comments

Comments
 (0)