-
Notifications
You must be signed in to change notification settings - Fork 414
/
xferfcn.py
1490 lines (1204 loc) · 53.1 KB
/
xferfcn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""xferfcn.py
Transfer function representation and functions.
This file contains the TransferFunction class and also functions
that operate on transfer functions. This is the primary representation
for the python-control library.
"""
# Python 3 compatibility (needs to go here)
from __future__ import print_function
from __future__ import division
"""Copyright (c) 2010 by California Institute of Technology
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the California Institute of Technology nor
the names of its contributors may be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CALTECH
OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
Author: Richard M. Murray
Date: 24 May 09
Revised: Kevin K. Chen, Dec 10
$Id$
"""
# External function declarations
import numpy as np
from numpy import angle, array, empty, finfo, ndarray, ones, \
polyadd, polymul, polyval, roots, sqrt, zeros, squeeze, exp, pi, \
where, delete, real, poly, nonzero
import scipy as sp
from numpy.polynomial.polynomial import polyfromroots
from scipy.signal import lti, tf2zpk, zpk2tf, cont2discrete
from copy import deepcopy
from warnings import warn
from itertools import chain
from .lti import LTI, timebaseEqual, timebase, isdtime
__all__ = ['TransferFunction', 'tf', 'ss2tf', 'tfdata']
class TransferFunction(LTI):
"""TransferFunction(num, den[, dt])
A class for representing transfer functions
The TransferFunction class is used to represent systems in transfer
function form.
The main data members are 'num' and 'den', which are 2-D lists of arrays
containing MIMO numerator and denominator coefficients. For example,
>>> num[2][5] = numpy.array([1., 4., 8.])
means that the numerator of the transfer function from the 6th input to the
3rd output is set to s^2 + 4s + 8.
Discrete-time transfer functions are implemented by using the 'dt'
instance variable and setting it to something other than 'None'. If 'dt'
has a non-zero value, then it must match whenever two transfer functions
are combined. If 'dt' is set to True, the system will be treated as a
discrete time system with unspecified sampling time.
The TransferFunction class defines two constants ``s`` and ``z`` that
represent the differentiation and delay operators in continuous and
discrete time. These can be used to create variables that allow algebraic
creation of transfer functions. For example,
>>> s = TransferFunction.s
>>> G = (s + 1)/(s**2 + 2*s + 1)
"""
def __init__(self, *args):
"""TransferFunction(num, den[, dt])
Construct a transfer function.
The default constructor is TransferFunction(num, den), where num and
den are lists of lists of arrays containing polynomial coefficients.
To create a discrete time transfer funtion, use TransferFunction(num,
den, dt) where 'dt' is the sampling time (or True for unspecified
sampling time). To call the copy constructor, call
TransferFunction(sys), where sys is a TransferFunction object
(continuous or discrete).
"""
args = deepcopy(args)
if len(args) == 2:
# The user provided a numerator and a denominator.
(num, den) = args
dt = None
elif len(args) == 3:
# Discrete time transfer function
(num, den, dt) = args
elif len(args) == 1:
# Use the copy constructor.
if not isinstance(args[0], TransferFunction):
raise TypeError("The one-argument constructor can only take \
in a TransferFunction object. Received %s."
% type(args[0]))
num = args[0].num
den = args[0].den
# TODO: not sure this can ever happen since dt is always present
try:
dt = args[0].dt
except NameError: # pragma: no coverage
dt = None
else:
raise ValueError("Needs 1, 2 or 3 arguments; received %i."
% len(args))
num = _clean_part(num)
den = _clean_part(den)
inputs = len(num[0])
outputs = len(num)
# Make sure numerator and denominator matrices have consistent sizes
if inputs != len(den[0]):
raise ValueError(
"The numerator has %i input(s), but the denominator has "
"%i input(s)." % (inputs, len(den[0])))
if outputs != len(den):
raise ValueError(
"The numerator has %i output(s), but the denominator has "
"%i output(s)." % (outputs, len(den)))
# Additional checks/updates on structure of the transfer function
for i in range(outputs):
# Make sure that each row has the same number of columns
if len(num[i]) != inputs:
raise ValueError(
"Row 0 of the numerator matrix has %i elements, but row "
"%i has %i." % (inputs, i, len(num[i])))
if len(den[i]) != inputs:
raise ValueError(
"Row 0 of the denominator matrix has %i elements, but row "
"%i has %i." % (inputs, i, len(den[i])))
# Check for zeros in numerator or denominator
# TODO: Right now these checks are only done during construction.
# It might be worthwhile to think of a way to perform checks if the
# user modifies the transfer function after construction.
for j in range(inputs):
# Check that we don't have any zero denominators.
zeroden = True
for k in den[i][j]:
if k:
zeroden = False
break
if zeroden:
raise ValueError(
"Input %i, output %i has a zero denominator."
% (j + 1, i + 1))
# If we have zero numerators, set the denominator to 1.
zeronum = True
for k in num[i][j]:
if k:
zeronum = False
break
if zeronum:
den[i][j] = ones(1)
LTI.__init__(self, inputs, outputs, dt)
self.num = num
self.den = den
self._truncatecoeff()
def __call__(self, s):
"""Evaluate the system's transfer function for a complex variable
For a SISO transfer function, returns the value of the
transfer function. For a MIMO transfer fuction, returns a
matrix of values evaluated at complex variable s."""
if self.issiso():
# return a scalar
return self.horner(s)[0][0]
else:
# return a matrix
return self.horner(s)
def _truncatecoeff(self):
"""Remove extraneous zero coefficients from num and den.
Check every element of the numerator and denominator matrices, and
truncate leading zeros. For instance, running self._truncatecoeff()
will reduce self.num = [[[0, 0, 1, 2]]] to [[[1, 2]]].
"""
# Beware: this is a shallow copy. This should be okay.
data = [self.num, self.den]
for p in range(len(data)):
for i in range(self.outputs):
for j in range(self.inputs):
# Find the first nontrivial coefficient.
nonzero = None
for k in range(data[p][i][j].size):
if data[p][i][j][k]:
nonzero = k
break
if nonzero is None:
# The array is all zeros.
data[p][i][j] = zeros(1)
else:
# Truncate the trivial coefficients.
data[p][i][j] = data[p][i][j][nonzero:]
[self.num, self.den] = data
def __str__(self, var=None):
"""String representation of the transfer function."""
mimo = self.inputs > 1 or self.outputs > 1
if var is None:
# TODO: replace with standard calls to lti functions
var = 's' if self.dt is None or self.dt == 0 else 'z'
outstr = ""
for i in range(self.inputs):
for j in range(self.outputs):
if mimo:
outstr += "\nInput %i to output %i:" % (i + 1, j + 1)
# Convert the numerator and denominator polynomials to strings.
numstr = _tf_polynomial_to_string(self.num[j][i], var=var)
denstr = _tf_polynomial_to_string(self.den[j][i], var=var)
# Figure out the length of the separating line
dashcount = max(len(numstr), len(denstr))
dashes = '-' * dashcount
# Center the numerator or denominator
if len(numstr) < dashcount:
numstr = (' ' * int(round((dashcount - len(numstr)) / 2)) +
numstr)
if len(denstr) < dashcount:
denstr = (' ' * int(round((dashcount - len(denstr)) / 2)) +
denstr)
outstr += "\n" + numstr + "\n" + dashes + "\n" + denstr + "\n"
# See if this is a discrete time system with specific sampling time
if not (self.dt is None) and type(self.dt) != bool and self.dt > 0:
# TODO: replace with standard calls to lti functions
outstr += "\ndt = " + self.dt.__str__() + "\n"
return outstr
# represent as string, makes display work for IPython
__repr__ = __str__
def _repr_latex_(self, var=None):
"""LaTeX representation of transfer function, for Jupyter notebook"""
mimo = self.inputs > 1 or self.outputs > 1
if var is None:
# ! TODO: replace with standard calls to lti functions
var = 's' if self.dt is None or self.dt == 0 else 'z'
out = ['$$']
if mimo:
out.append(r"\begin{bmatrix}")
for i in range(self.outputs):
for j in range(self.inputs):
# Convert the numerator and denominator polynomials to strings.
numstr = _tf_polynomial_to_string(self.num[i][j], var=var)
denstr = _tf_polynomial_to_string(self.den[i][j], var=var)
out += [r"\frac{", numstr, "}{", denstr, "}"]
if mimo and j < self.outputs - 1:
out.append("&")
if mimo:
out.append(r"\\")
if mimo:
out.append(r" \end{bmatrix}")
# See if this is a discrete time system with specific sampling time
if not (self.dt is None) and type(self.dt) != bool and self.dt > 0:
out += ["\quad dt = ", str(self.dt)]
out.append("$$")
return ''.join(out)
def __neg__(self):
"""Negate a transfer function."""
num = deepcopy(self.num)
for i in range(self.outputs):
for j in range(self.inputs):
num[i][j] *= -1
return TransferFunction(num, self.den, self.dt)
def __add__(self, other):
"""Add two LTI objects (parallel connection)."""
from .statesp import StateSpace
# Convert the second argument to a transfer function.
if isinstance(other, StateSpace):
other = _convert_to_transfer_function(other)
elif not isinstance(other, TransferFunction):
other = _convert_to_transfer_function(other, inputs=self.inputs,
outputs=self.outputs)
# Check that the input-output sizes are consistent.
if self.inputs != other.inputs:
raise ValueError(
"The first summand has %i input(s), but the second has %i."
% (self.inputs, other.inputs))
if self.outputs != other.outputs:
raise ValueError(
"The first summand has %i output(s), but the second has %i."
% (self.outputs, other.outputs))
# Figure out the sampling time to use
if self.dt is None and other.dt is not None:
dt = other.dt # use dt from second argument
elif (other.dt is None and self.dt is not None) or \
(timebaseEqual(self, other)):
dt = self.dt # use dt from first argument
else:
raise ValueError("Systems have different sampling times")
# Preallocate the numerator and denominator of the sum.
num = [[[] for j in range(self.inputs)] for i in range(self.outputs)]
den = [[[] for j in range(self.inputs)] for i in range(self.outputs)]
for i in range(self.outputs):
for j in range(self.inputs):
num[i][j], den[i][j] = _add_siso(
self.num[i][j], self.den[i][j],
other.num[i][j], other.den[i][j])
return TransferFunction(num, den, dt)
def __radd__(self, other):
"""Right add two LTI objects (parallel connection)."""
return self + other
def __sub__(self, other):
"""Subtract two LTI objects."""
return self + (-other)
def __rsub__(self, other):
"""Right subtract two LTI objects."""
return other + (-self)
def __mul__(self, other):
"""Multiply two LTI objects (serial connection)."""
# Convert the second argument to a transfer function.
if isinstance(other, (int, float, complex, np.number)):
other = _convert_to_transfer_function(other, inputs=self.inputs,
outputs=self.inputs)
else:
other = _convert_to_transfer_function(other)
# Check that the input-output sizes are consistent.
if self.inputs != other.outputs:
raise ValueError(
"C = A * B: A has %i column(s) (input(s)), but B has %i "
"row(s)\n(output(s))." % (self.inputs, other.outputs))
inputs = other.inputs
outputs = self.outputs
# Figure out the sampling time to use
if self.dt is None and other.dt is not None:
dt = other.dt # use dt from second argument
elif (other.dt is None and self.dt is not None) or \
(self.dt == other.dt):
dt = self.dt # use dt from first argument
else:
raise ValueError("Systems have different sampling times")
# Preallocate the numerator and denominator of the sum.
num = [[[0] for j in range(inputs)] for i in range(outputs)]
den = [[[1] for j in range(inputs)] for i in range(outputs)]
# Temporary storage for the summands needed to find the (i, j)th
# element of the product.
num_summand = [[] for k in range(self.inputs)]
den_summand = [[] for k in range(self.inputs)]
# Multiply & add.
for row in range(outputs):
for col in range(inputs):
for k in range(self.inputs):
num_summand[k] = polymul(
self.num[row][k], other.num[k][col])
den_summand[k] = polymul(
self.den[row][k], other.den[k][col])
num[row][col], den[row][col] = _add_siso(
num[row][col], den[row][col],
num_summand[k], den_summand[k])
return TransferFunction(num, den, dt)
def __rmul__(self, other):
"""Right multiply two LTI objects (serial connection)."""
# Convert the second argument to a transfer function.
if isinstance(other, (int, float, complex, np.number)):
other = _convert_to_transfer_function(other, inputs=self.inputs,
outputs=self.inputs)
else:
other = _convert_to_transfer_function(other)
# Check that the input-output sizes are consistent.
if other.inputs != self.outputs:
raise ValueError(
"C = A * B: A has %i column(s) (input(s)), but B has %i "
"row(s)\n(output(s))." % (other.inputs, self.outputs))
inputs = self.inputs
outputs = other.outputs
# Figure out the sampling time to use
if self.dt is None and other.dt is not None:
dt = other.dt # use dt from second argument
elif (other.dt is None and self.dt is not None) \
or (self.dt == other.dt):
dt = self.dt # use dt from first argument
else:
raise ValueError("Systems have different sampling times")
# Preallocate the numerator and denominator of the sum.
num = [[[0] for j in range(inputs)] for i in range(outputs)]
den = [[[1] for j in range(inputs)] for i in range(outputs)]
# Temporary storage for the summands needed to find the
# (i, j)th element
# of the product.
num_summand = [[] for k in range(other.inputs)]
den_summand = [[] for k in range(other.inputs)]
for i in range(outputs): # Iterate through rows of product.
for j in range(inputs): # Iterate through columns of product.
for k in range(other.inputs): # Multiply & add.
num_summand[k] = polymul(other.num[i][k], self.num[k][j])
den_summand[k] = polymul(other.den[i][k], self.den[k][j])
num[i][j], den[i][j] = _add_siso(
num[i][j], den[i][j],
num_summand[k], den_summand[k])
return TransferFunction(num, den, dt)
# TODO: Division of MIMO transfer function objects is not written yet.
def __truediv__(self, other):
"""Divide two LTI objects."""
if isinstance(other, (int, float, complex, np.number)):
other = _convert_to_transfer_function(
other, inputs=self.inputs,
outputs=self.inputs)
else:
other = _convert_to_transfer_function(other)
if (self.inputs > 1 or self.outputs > 1 or
other.inputs > 1 or other.outputs > 1):
raise NotImplementedError(
"TransferFunction.__truediv__ is currently \
implemented only for SISO systems.")
# Figure out the sampling time to use
if self.dt is None and other.dt is not None:
dt = other.dt # use dt from second argument
elif (other.dt is None and self.dt is not None) or \
(self.dt == other.dt):
dt = self.dt # use dt from first argument
else:
raise ValueError("Systems have different sampling times")
num = polymul(self.num[0][0], other.den[0][0])
den = polymul(self.den[0][0], other.num[0][0])
return TransferFunction(num, den, dt)
# TODO: Remove when transition to python3 complete
def __div__(self, other):
return TransferFunction.__truediv__(self, other)
# TODO: Division of MIMO transfer function objects is not written yet.
def __rtruediv__(self, other):
"""Right divide two LTI objects."""
if isinstance(other, (int, float, complex, np.number)):
other = _convert_to_transfer_function(
other, inputs=self.inputs,
outputs=self.inputs)
else:
other = _convert_to_transfer_function(other)
if (self.inputs > 1 or self.outputs > 1 or
other.inputs > 1 or other.outputs > 1):
raise NotImplementedError(
"TransferFunction.__rtruediv__ is currently implemented only "
"for SISO systems.")
return other / self
# TODO: Remove when transition to python3 complete
def __rdiv__(self, other):
return TransferFunction.__rtruediv__(self, other)
def __pow__(self, other):
if not type(other) == int:
raise ValueError("Exponent must be an integer")
if other == 0:
return TransferFunction([1], [1]) # unity
if other > 0:
return self * (self**(other - 1))
if other < 0:
return (TransferFunction([1], [1]) / self) * (self**(other + 1))
def __getitem__(self, key):
key1, key2 = key
# pre-process
if isinstance(key1, int):
key1 = slice(key1, key1 + 1, 1)
if isinstance(key2, int):
key2 = slice(key2, key2 + 1, 1)
# dim1
start1, stop1, step1 = key1.start, key1.stop, key1.step
if step1 is None:
step1 = 1
if start1 is None:
start1 = 0
if stop1 is None:
stop1 = len(self.num)
# dim1
start2, stop2, step2 = key2.start, key2.stop, key2.step
if step2 is None:
step2 = 1
if start2 is None:
start2 = 0
if stop2 is None:
stop2 = len(self.num[0])
num = []
den = []
for i in range(start1, stop1, step1):
num_i = []
den_i = []
for j in range(start2, stop2, step2):
num_i.append(self.num[i][j])
den_i.append(self.den[i][j])
num.append(num_i)
den.append(den_i)
if self.isctime():
return TransferFunction(num, den)
else:
return TransferFunction(num, den, self.dt)
def evalfr(self, omega):
"""Evaluate a transfer function at a single angular frequency.
self._evalfr(omega) returns the value of the transfer function
matrix with input value s = i * omega.
"""
warn("TransferFunction.evalfr(omega) will be deprecated in a "
"future release of python-control; use evalfr(sys, omega*1j) "
"instead", PendingDeprecationWarning)
return self._evalfr(omega)
def _evalfr(self, omega):
"""Evaluate a transfer function at a single angular frequency."""
# TODO: implement for discrete time systems
if isdtime(self, strict=True):
# Convert the frequency to discrete time
dt = timebase(self)
s = exp(1.j * omega * dt)
if np.any(omega * dt > pi):
warn("_evalfr: frequency evaluation above Nyquist frequency")
else:
s = 1.j * omega
return self.horner(s)
def horner(self, s):
"""Evaluate the systems's transfer function for a complex variable
Returns a matrix of values evaluated at complex variable s.
"""
# Preallocate the output.
if getattr(s, '__iter__', False):
out = empty((self.outputs, self.inputs, len(s)), dtype=complex)
else:
out = empty((self.outputs, self.inputs), dtype=complex)
for i in range(self.outputs):
for j in range(self.inputs):
out[i][j] = (polyval(self.num[i][j], s) /
polyval(self.den[i][j], s))
return out
# Method for generating the frequency response of the system
def freqresp(self, omega):
"""Evaluate a transfer function at a list of angular frequencies.
mag, phase, omega = self.freqresp(omega)
reports the value of the magnitude, phase, and angular frequency of
the transfer function matrix evaluated at s = i * omega, where omega
is a list of angular frequencies, and is a sorted version of the input
omega.
"""
# Preallocate outputs.
numfreq = len(omega)
mag = empty((self.outputs, self.inputs, numfreq))
phase = empty((self.outputs, self.inputs, numfreq))
# Figure out the frequencies
omega.sort()
if isdtime(self, strict=True):
dt = timebase(self)
slist = np.array([exp(1.j * w * dt) for w in omega])
if max(omega) * dt > pi:
warn("freqresp: frequency evaluation above Nyquist frequency")
else:
slist = np.array([1j * w for w in omega])
# Compute frequency response for each input/output pair
for i in range(self.outputs):
for j in range(self.inputs):
fresp = (polyval(self.num[i][j], slist) /
polyval(self.den[i][j], slist))
mag[i, j, :] = abs(fresp)
phase[i, j, :] = angle(fresp)
return mag, phase, omega
def pole(self):
"""Compute the poles of a transfer function."""
num, den, denorder = self._common_den()
rts = []
for d, o in zip(den, denorder):
rts.extend(roots(d[:o + 1]))
return np.array(rts)
def zero(self):
"""Compute the zeros of a transfer function."""
if self.inputs > 1 or self.outputs > 1:
raise NotImplementedError(
"TransferFunction.zero is currently only implemented "
"for SISO systems.")
else:
# for now, just give zeros of a SISO tf
return roots(self.num[0][0])
def feedback(self, other=1, sign=-1):
"""Feedback interconnection between two LTI objects."""
other = _convert_to_transfer_function(other)
if (self.inputs > 1 or self.outputs > 1 or
other.inputs > 1 or other.outputs > 1):
# TODO: MIMO feedback
raise NotImplementedError(
"TransferFunction.feedback is currently only implemented "
"for SISO functions.")
# Figure out the sampling time to use
if self.dt is None and other.dt is not None:
dt = other.dt # use dt from second argument
elif (other.dt is None and self.dt is not None) or \
(self.dt == other.dt):
dt = self.dt # use dt from first argument
else:
raise ValueError("Systems have different sampling times")
num1 = self.num[0][0]
den1 = self.den[0][0]
num2 = other.num[0][0]
den2 = other.den[0][0]
num = polymul(num1, den2)
den = polyadd(polymul(den2, den1), -sign * polymul(num2, num1))
return TransferFunction(num, den, dt)
# For MIMO or SISO systems, the analytic expression is
# self / (1 - sign * other * self)
# But this does not work correctly because the state size will be too
# large.
def minreal(self, tol=None):
"""Remove cancelling pole/zero pairs from a transfer function"""
# based on octave minreal
# default accuracy
from sys import float_info
sqrt_eps = sqrt(float_info.epsilon)
# pre-allocate arrays
num = [[[] for j in range(self.inputs)] for i in range(self.outputs)]
den = [[[] for j in range(self.inputs)] for i in range(self.outputs)]
for i in range(self.outputs):
for j in range(self.inputs):
# split up in zeros, poles and gain
newzeros = []
zeros = roots(self.num[i][j])
poles = roots(self.den[i][j])
gain = self.num[i][j][0] / self.den[i][j][0]
# check all zeros
for z in zeros:
t = tol or \
1000 * max(float_info.epsilon, abs(z) * sqrt_eps)
idx = where(abs(z - poles) < t)[0]
if len(idx):
# cancel this zero against one of the poles
poles = delete(poles, idx[0])
else:
# keep this zero
newzeros.append(z)
# poly([]) returns a scalar, but we always want a 1d array
num[i][j] = np.atleast_1d(gain * real(poly(newzeros)))
den[i][j] = np.atleast_1d(real(poly(poles)))
# end result
return TransferFunction(num, den, self.dt)
def returnScipySignalLTI(self):
"""Return a list of a list of scipy.signal.lti objects.
For instance,
>>> out = tfobject.returnScipySignalLTI()
>>> out[3][5]
is a signal.scipy.lti object corresponding to the
transfer function from the 6th input to the 4th output.
"""
# TODO: implement for discrete time systems
if self.dt != 0 and self.dt is not None:
raise NotImplementedError("Function not \
implemented in discrete time")
# Preallocate the output.
out = [[[] for j in range(self.inputs)] for i in range(self.outputs)]
for i in range(self.outputs):
for j in range(self.inputs):
out[i][j] = lti(self.num[i][j], self.den[i][j])
return out
def _common_den(self, imag_tol=None):
"""
Compute MIMO common denominators; return them and adjusted numerators.
This function computes the denominators per input containing all
the poles of sys.den, and reports it as the array den. The
output numerator array num is modified to use the common
denominator for this input/column; the coefficient arrays are also
padded with zeros to be the same size for all num/den.
num is an sys.outputs by sys.inputs
by len(d) array.
Parameters
----------
imag_tol: float
Threshold for the imaginary part of a root to use in detecting
complex poles
Returns
-------
num: array
Multi-dimensional array of numerator coefficients. num[i][j]
gives the numerator coefficient array for the ith input and jth
output, also prepared for use in td04ad; matches the denorder
order; highest coefficient starts on the left.
den: array
Multi-dimensional array of coefficients for common denominator
polynomial, one row per input. The array is prepared for use in
slycot td04ad, the first element is the highest-order polynomial
coefficiend of s, matching the order in denorder, if denorder <
number of columns in den, the den is padded with zeros
denorder: array of int, orders of den, one per input
Examples
--------
>>> num, den, denorder = sys._common_den()
"""
# Machine precision for floats.
eps = finfo(float).eps
# Decide on the tolerance to use in deciding of a pole is complex
if (imag_tol is None):
imag_tol = 1e-8 # TODO: figure out the right number to use
# A list to keep track of cumulative poles found as we scan
# self.den[..][..]
poles = [[] for j in range(self.inputs)]
# RvP, new implementation 180526, issue #194
# pre-calculate the poles for all num, den
# has zeros, poles, gain, list for pole indices not in den,
# number of poles known at the time analyzed
# do not calculate minreal. Rory's hint .minreal()
poleset = []
for i in range(self.outputs):
poleset.append([])
for j in range(self.inputs):
if abs(self.num[i][j]).max() <= eps:
poleset[-1].append([array([], dtype=float),
roots(self.den[i][j]), 0.0, [], 0])
else:
z, p, k = tf2zpk(self.num[i][j], self.den[i][j])
poleset[-1].append([z, p, k, [], 0])
# collect all individual poles
epsnm = eps * self.inputs * self.outputs
for j in range(self.inputs):
for i in range(self.outputs):
currentpoles = poleset[i][j][1]
nothave = ones(currentpoles.shape, dtype=bool)
for ip, p in enumerate(poles[j]):
idx, = nonzero(
(abs(currentpoles - p) < epsnm) * nothave)
if len(idx):
nothave[idx[0]] = False
else:
# remember id of pole not in tf
poleset[i][j][3].append(ip)
for h, c in zip(nothave, currentpoles):
if h:
poles[j].append(c)
# remember how many poles now known
poleset[i][j][4] = len(poles[j])
# figure out maximum number of poles, for sizing the den
npmax = max([len(p) for p in poles])
den = zeros((self.inputs, npmax + 1), dtype=float)
num = zeros((max(1, self.outputs, self.inputs),
max(1, self.outputs, self.inputs), npmax + 1),
dtype=float)
denorder = zeros((self.inputs,), dtype=int)
for j in range(self.inputs):
if not len(poles[j]):
# no poles matching this input; only one or more gains
den[j, 0] = 1.0
for i in range(self.outputs):
num[i, j, 0] = poleset[i][j][2]
else:
# create the denominator matching this input polyfromroots
# gives coeffs in opposite order from what we use coefficients
# should be padded on right, ending at np
np = len(poles[j])
den[j, np::-1] = polyfromroots(poles[j]).real
denorder[j] = np
# now create the numerator, also padded on the right
for i in range(self.outputs):
# start with the current set of zeros for this output
nwzeros = list(poleset[i][j][0])
# add all poles not found in the original denominator,
# and the ones later added from other denominators
for ip in chain(poleset[i][j][3],
range(poleset[i][j][4], np)):
nwzeros.append(poles[j][ip])
numpoly = poleset[i][j][2] * polyfromroots(nwzeros).real
# print(numpoly, den[j])
# polyfromroots gives coeffs in opposite order => invert
# numerator polynomial should be padded on left and right
# ending at np to line up with what td04ad expects...
num[i, j, np + 1 - len(numpoly):np + 1] = numpoly[::-1]
# print(num[i, j])
if (abs(den.imag) > epsnm).any():
print("Warning: The denominator has a nontrivial imaginary part: "
" %f" % abs(den.imag).max())
den = den.real
return num, den, denorder
def sample(self, Ts, method='zoh', alpha=None):
"""Convert a continuous-time system to discrete time
Creates a discrete-time system from a continuous-time system by
sampling. Multiple methods of conversion are supported.
Parameters
----------
Ts : float
Sampling period
method : {"gbt", "bilinear", "euler", "backward_diff",
"zoh", "matched"}
Method to use for sampling:
* gbt: generalized bilinear transformation
* bilinear: Tustin's approximation ("gbt" with alpha=0.5)
* euler: Euler (or forward difference) method ("gbt" with alpha=0)
* backward_diff: Backwards difference ("gbt" with alpha=1.0)
* zoh: zero-order hold (default)
alpha : float within [0, 1]
The generalized bilinear transformation weighting parameter, which
should only be specified with method="gbt", and is ignored
otherwise.
Returns
-------
sysd : StateSpace system
Discrete time system, with sampling rate Ts
Notes
-----
1. Available only for SISO systems
2. Uses the command `cont2discrete` from `scipy.signal`
Examples
--------
>>> sys = TransferFunction(1, [1,1])
>>> sysd = sys.sample(0.5, method='bilinear')
"""
if not self.isctime():
raise ValueError("System must be continuous time system")
if not self.issiso():
raise NotImplementedError("MIMO implementation not available")
if method == "matched":
return _c2d_matched(self, Ts)
sys = (self.num[0][0], self.den[0][0])
numd, dend, dt = cont2discrete(sys, Ts, method, alpha)
return TransferFunction(numd[0, :], dend, dt)
def dcgain(self):
"""Return the zero-frequency (or DC) gain
For a continous-time transfer function G(s), the DC gain is G(0)
For a discrete-time transfer function G(z), the DC gain is G(1)
Returns
-------
gain : ndarray
The zero-frequency gain
"""
if self.isctime():
return self._dcgain_cont()