53
53
54
54
"""
55
55
56
- import scipy as sp
57
56
import numpy as np
58
57
from . import xferfcn as tf
59
58
from . import statesp as ss
60
59
from . import frdata as frd
61
60
62
61
__all__ = ['series' , 'parallel' , 'negate' , 'feedback' , 'append' , 'connect' ]
63
62
63
+
64
64
def series (sys1 , * sysn ):
65
65
"""Return the series connection (... \* sys3 \*) sys2 \* sys1
66
66
67
67
Parameters
68
68
----------
69
- sys1: scalar, StateSpace, TransferFunction, or FRD
70
- sysn: other scalars, StateSpaces, TransferFunctions, or FRDs
69
+ sys1 : scalar, StateSpace, TransferFunction, or FRD
70
+ sysn : other scalars, StateSpaces, TransferFunctions, or FRDs
71
71
72
72
Returns
73
73
-------
74
- out: scalar, StateSpace, or TransferFunction
74
+ out : scalar, StateSpace, or TransferFunction
75
75
76
76
Raises
77
77
------
@@ -105,18 +105,19 @@ def series(sys1, *sysn):
105
105
from functools import reduce
106
106
return reduce (lambda x , y :y * x , sysn , sys1 )
107
107
108
+
108
109
def parallel (sys1 , * sysn ):
109
110
"""
110
111
Return the parallel connection sys1 + sys2 (+ sys3 + ...)
111
112
112
113
Parameters
113
114
----------
114
- sys1: scalar, StateSpace, TransferFunction, or FRD
115
- *sysn: other scalars, StateSpaces, TransferFunctions, or FRDs
115
+ sys1 : scalar, StateSpace, TransferFunction, or FRD
116
+ *sysn : other scalars, StateSpaces, TransferFunctions, or FRDs
116
117
117
118
Returns
118
119
-------
119
- out: scalar, StateSpace, or TransferFunction
120
+ out : scalar, StateSpace, or TransferFunction
120
121
121
122
Raises
122
123
------
@@ -150,17 +151,18 @@ def parallel(sys1, *sysn):
150
151
from functools import reduce
151
152
return reduce (lambda x , y :x + y , sysn , sys1 )
152
153
154
+
153
155
def negate (sys ):
154
156
"""
155
157
Return the negative of a system.
156
158
157
159
Parameters
158
160
----------
159
- sys: StateSpace, TransferFunction or FRD
161
+ sys : StateSpace, TransferFunction or FRD
160
162
161
163
Returns
162
164
-------
163
- out: StateSpace or TransferFunction
165
+ out : StateSpace or TransferFunction
164
166
165
167
Notes
166
168
-----
@@ -177,7 +179,6 @@ def negate(sys):
177
179
>>> sys2 = negate(sys1) # Same as sys2 = -sys1.
178
180
179
181
"""
180
-
181
182
return - sys ;
182
183
183
184
#! TODO: expand to allow sys2 default to work in MIMO case?
@@ -187,18 +188,18 @@ def feedback(sys1, sys2=1, sign=-1):
187
188
188
189
Parameters
189
190
----------
190
- sys1: scalar, StateSpace, TransferFunction, FRD
191
- The primary plant .
192
- sys2: scalar, StateSpace, TransferFunction, FRD
193
- The feedback plant (often a feedback controller).
191
+ sys1 : scalar, StateSpace, TransferFunction, FRD
192
+ The primary process .
193
+ sys2 : scalar, StateSpace, TransferFunction, FRD
194
+ The feedback process (often a feedback controller).
194
195
sign: scalar
195
196
The sign of feedback. `sign` = -1 indicates negative feedback, and
196
197
`sign` = 1 indicates positive feedback. `sign` is an optional
197
198
argument; it assumes a value of -1 if not specified.
198
199
199
200
Returns
200
201
-------
201
- out: StateSpace or TransferFunction
202
+ out : StateSpace or TransferFunction
202
203
203
204
Raises
204
205
------
@@ -256,7 +257,7 @@ def feedback(sys1, sys2=1, sign=-1):
256
257
return sys1 .feedback (sys2 , sign )
257
258
258
259
def append (* sys ):
259
- ''' append(sys1, sys2, ..., sysn)
260
+ """ append(sys1, sys2, ..., sysn)
260
261
261
262
Group models by appending their inputs and outputs
262
263
@@ -279,42 +280,40 @@ def append(*sys):
279
280
280
281
Examples
281
282
--------
282
- >>> sys1 = ss("1. -2; 3. -4", "5.; 7 ", "6. 8", "9." )
283
- >>> sys2 = ss(" -1.", "1.", "1.", "0." )
283
+ >>> sys1 = ss([[1., -2], [3., -4]], [[5.], [7]] ", [[6., 8]], [[9.]] )
284
+ >>> sys2 = ss([[ -1.]], [[1.]], [[1.]], [[0.]] )
284
285
>>> sys = append(sys1, sys2)
285
286
286
- .. todo::
287
- also implement for transfer function, zpk, etc.
288
- '''
287
+ """
289
288
s1 = sys [0 ]
290
289
for s in sys [1 :]:
291
290
s1 = s1 .append (s )
292
291
return s1
293
292
294
293
def connect (sys , Q , inputv , outputv ):
295
- '''
296
- Index-base interconnection of system
294
+ """Index-based interconnection of an LTI system.
297
295
298
- The system sys is a system typically constructed with append, with
299
- multiple inputs and outputs. The inputs and outputs are connected
300
- according to the interconnection matrix Q , and then the final
301
- inputs and outputs are trimmed according to the inputs and outputs
302
- listed in inputv and outputv.
296
+ The system ` sys` is a system typically constructed with ` append` , with
297
+ multiple inputs and outputs. The inputs and outputs are connected
298
+ according to the interconnection matrix `Q` , and then the final inputs and
299
+ outputs are trimmed according to the inputs and outputs listed in `inputv`
300
+ and ` outputv` .
303
301
304
- Note: to have this work, inputs and outputs start counting at 1!!!!
302
+ NOTE: Inputs and outputs are indexed starting at 1 and negative values
303
+ correspond to a negative feedback interconnection.
305
304
306
305
Parameters
307
306
----------
308
- sys: StateSpace Transferfunction
307
+ sys : StateSpace Transferfunction
309
308
System to be connected
310
- Q: 2d array
309
+ Q : 2D array
311
310
Interconnection matrix. First column gives the input to be connected
312
- second column gives the output to be fed into this input. Negative
311
+ second column gives the output to be fed into this input. Negative
313
312
values for the second column mean the feedback is negative, 0 means
314
- no connection is made
315
- inputv: 1d array
313
+ no connection is made. Inputs and outputs are indexed starting at 1.
314
+ inputv : 1D array
316
315
list of final external inputs
317
- outputv: 1d array
316
+ outputv : 1D array
318
317
list of final external outputs
319
318
320
319
Returns
@@ -324,28 +323,30 @@ def connect(sys, Q, inputv, outputv):
324
323
325
324
Examples
326
325
--------
327
- >>> sys1 = ss("1. -2; 3. -4", "5.; 7", " 6, 8", "9." )
328
- >>> sys2 = ss(" -1.", "1.", "1.", "0." )
326
+ >>> sys1 = ss([[1., -2], [3., -4]], [[5.], [7]], [[ 6, 8]], [[9.]] )
327
+ >>> sys2 = ss([[ -1.]], [[1.]], [[1.]], [[0.]] )
329
328
>>> sys = append(sys1, sys2)
330
- >>> Q = sp.mat([ [ 1, 2], [2, -1] ]) # basically feedback, output 2 in 1
329
+ >>> Q = [[ 1, 2], [2, -1]]) # negative feedback interconnection
331
330
>>> sysc = connect(sys, Q, [2], [1, 2])
332
- '''
331
+
332
+ """
333
333
# first connect
334
- K = sp .zeros ( (sys .inputs , sys .outputs ) )
335
- for r in sp .array (Q ).astype (int ):
334
+ K = np .zeros ((sys .inputs , sys .outputs ))
335
+ for r in np .array (Q ).astype (int ):
336
336
inp = r [0 ]- 1
337
337
for outp in r [1 :]:
338
338
if outp > 0 and outp <= sys .outputs :
339
339
K [inp ,outp - 1 ] = 1.
340
340
elif outp < 0 and - outp >= - sys .outputs :
341
341
K [inp ,- outp - 1 ] = - 1.
342
- sys = sys .feedback (sp . matrix (K ), sign = 1 )
342
+ sys = sys .feedback (np . array (K ), sign = 1 )
343
343
344
344
# now trim
345
- Ytrim = sp .zeros ( (len (outputv ), sys .outputs ) )
346
- Utrim = sp .zeros ( (sys .inputs , len (inputv )) )
345
+ Ytrim = np .zeros ((len (outputv ), sys .outputs ))
346
+ Utrim = np .zeros ((sys .inputs , len (inputv )))
347
347
for i ,u in enumerate (inputv ):
348
348
Utrim [u - 1 ,i ] = 1.
349
349
for i ,y in enumerate (outputv ):
350
350
Ytrim [i ,y - 1 ] = 1.
351
- return sp .matrix (Ytrim )* sys * sp .matrix (Utrim )
351
+
352
+ return Ytrim * sys * Utrim
0 commit comments