-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathfrdata.py
More file actions
1103 lines (901 loc) · 43.8 KB
/
frdata.py
File metadata and controls
1103 lines (901 loc) · 43.8 KB
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
# frdata.py - frequency response data representation and functions
#
# Initial author: M.M. (Rene) van Paassen (using xferfcn.py as basis)
# Creation date: 02 Oct 2012
"""Frequency response data representation and functions.
This module contains the `FrequencyResponseData` (FRD) class and also
functions that operate on FRD data.
"""
from collections.abc import Iterable
from copy import copy
from warnings import warn
import numpy as np
from numpy import absolute, array, empty, eye, imag, linalg, ones, real, sort
from scipy.interpolate import splev, splprep
from . import bdalg, config
from .exception import pandas_check
from .iosys import InputOutputSystem, NamedSignal, _extended_system_name, \
_process_iosys_keywords, _process_subsys_index, common_timebase
from .lti import LTI, _process_frequency_response
__all__ = ['FrequencyResponseData', 'FRD', 'frd']
class FrequencyResponseData(LTI):
"""FrequencyResponseData(frdata, omega[, smooth])
Input/output model defined by frequency response data (FRD).
The FrequencyResponseData (FRD) class is used to represent systems in
frequency response data form. It can be created manually using the
class constructor, using the `frd` factory function, or
via the `frequency_response` function.
Parameters
----------
frdata : 1D or 3D complex array_like
The frequency response at each frequency point. If 1D, the system is
assumed to be SISO. If 3D, the system is MIMO, with the first
dimension corresponding to the output index of the FRD, the second
dimension corresponding to the input index, and the 3rd dimension
corresponding to the frequency points in `omega`. When accessed as an
attribute, `frdata` is always stored as a 3D array.
omega : iterable of real frequencies
List of monotonically increasing frequency points for the response.
smooth : bool, optional
If True, create an interpolation function that allows the frequency
response to be computed at any frequency within the range of
frequencies give in `omega`. If False (default), frequency response
can only be obtained at the frequencies specified in `omega`.
dt : None, True or float, optional
System timebase. 0 (default) indicates continuous time, True
indicates discrete time with unspecified sampling time, positive
number is discrete time with specified sampling time, None
indicates unspecified timebase (either continuous or discrete time).
squeeze : bool
By default, if a system is single-input, single-output (SISO) then
the outputs (and inputs) are returned as a 1D array (indexed by
frequency) and if a system is multi-input or multi-output, then the
outputs are returned as a 2D array (indexed by output and
frequency) or a 3D array (indexed by output, trace, and frequency).
If `squeeze` = True, access to the output response will remove
single-dimensional entries from the shape of the inputs and outputs
even if the system is not SISO. If `squeeze` = False, the output is
returned as a 3D array (indexed by the output, input, and
frequency) even if the system is SISO. The default value can be set
using `config.defaults['control.squeeze_frequency_response']`.
sysname : str or None
Name of the system that generated the data.
Attributes
----------
complex : array
Complex frequency response, indexed by output index, input index, and
frequency point, with squeeze processing.
magnitude : array
Magnitude of the frequency response, indexed by output index, input
index, and frequency point, with squeeze processing.
phase : array
Phase of the frequency response, indexed by output index, input index,
and frequency point, with squeeze processing.
frequency : 1D array
Array of frequency points for which data are available.
ninputs, noutputs : int
Number of input and output signals.
shape : tuple
2-tuple of I/O system dimension, (noutputs, ninputs).
input_labels, output_labels : array of str
Names for the input and output signals.
name : str
System name. For data generated using
`frequency_response`, stores the name of the
system that created the data.
Other Parameters
----------------
plot_type : str, optional
Set the type of plot to generate with `~FrequencyResponseData.plot`
('bode', 'nichols').
title : str, optional
Set the title to use when plotting.
plot_magnitude, plot_phase : bool, optional
If set to False, don't plot the magnitude or phase, respectively.
return_magphase : bool, optional
If True, then a frequency response data object will enumerate
as a tuple of the form ``(mag, phase, omega)`` where where `mag`
is the magnitude (absolute value, not dB or log10) of the system
frequency response, `phase` is the wrapped phase in radians of the
system frequency response, and `omega` is the (sorted) frequencies
at which the response was evaluated.
See Also
--------
frd, frequency_response, InputOutputSystem, TransferFunction
Notes
-----
The main data members are `omega` and `frdata`, where `omega` is a 1D
array of frequency points and and `frdata` is a 3D array of frequency
responses, with the first dimension corresponding to the output index of
the FRD, the second dimension corresponding to the input index, and the
3rd dimension corresponding to the frequency points in omega. For example,
>>> frdata[2,5,:] = numpy.array([1., 0.8-0.2j, 0.2-0.8j]) # doctest: +SKIP
means that the frequency response from the 6th input to the 3rd output at
the frequencies defined in omega is set to the array above, i.e. the rows
represent the outputs and the columns represent the inputs.
A frequency response data object is callable and returns the value of the
transfer function evaluated at a point in the complex plane (must be on
the imaginary axis). See `FrequencyResponseData.__call__`
for a more detailed description.
Subsystem response corresponding to selected input/output pairs can be
created by indexing the frequency response data object::
subsys = sys[output_spec, input_spec]
The input and output specifications can be single integers, lists of
integers, or slices. In addition, the strings representing the names
of the signals can be used and will be replaced with the equivalent
signal offsets.
"""
#
# Class attributes
#
# These attributes are defined as class attributes so that they are
# documented properly. They are "overwritten" in __init__.
#
#: Number of system inputs.
#:
#: :meta hide-value:
ninputs = 1
#: Number of system outputs.
#:
#: :meta hide-value:
noutputs = 1
#: Squeeze processing parameter.
#:
#: By default, if a system is single-input, single-output (SISO) then
#: the outputs (and inputs) are returned as a 1D array (indexed by
#: frequency) and if a system is multi-input or multi-output, then the
#: outputs are returned as a 2D array (indexed by output and frequency)
#: or a 3D array (indexed by output, trace, and frequency). If
#: `squeeze` = True, access to the output response will remove
#: single-dimensional entries from the shape of the inputs and outputs
#: even if the system is not SISO. If `squeeze` = False, the output is
#: returned as a 3D array (indexed by the output, input, and frequency)
#: even if the system is SISO. The default value can be set using
#: config.defaults['control.squeeze_frequency_response'].
#:
#: :meta hide-value:
squeeze = None
_epsw = 1e-8 #: Bound for exact frequency match
def __init__(self, *args, **kwargs):
"""FrequencyResponseData(response, omega[, dt])
Construct a frequency response data (FRD) object.
The default constructor is `FrequencyResponseData(response, omega)`,
where `omega` is an iterable of frequency points and `response` is
the matching frequency data. If `response` is a single list, 1D
array, or tuple, a SISO system description is assumed. `response`
can also be a 2D array, in which case a MIMO response is created.
To call the copy constructor, call `FrequencyResponseData(sys)`,
where `sys` is a FRD object. The timebase for the frequency
response can be provided using an optional third argument or the
`dt` keyword.
To construct frequency response data for an existing LTI object,
other than an FRD, call `FrequencyResponseData(sys, omega)`. This
functionality can also be obtained using `frequency_response`
(which has additional options available).
See `FrequencyResponseData` and `frd` for more
information.
"""
smooth = kwargs.pop('smooth', False)
#
# Process positional arguments
#
if len(args) == 3:
# Discrete time transfer function
dt = args[-1]
if 'dt' in kwargs:
warn("received multiple dt arguments, "
"using positional arg dt = %s" % dt)
kwargs['dt'] = dt
args = args[:-1]
if len(args) == 2:
if not isinstance(args[0], FRD) and isinstance(args[0], LTI):
# not an FRD, but still an LTI system, second argument
# should be the frequency range
otherlti = args[0]
self.omega = sort(np.asarray(args[1], dtype=float))
# calculate frequency response at specified points
if otherlti.isctime():
s = 1j * self.omega
self.frdata = otherlti(s, squeeze=False)
else:
z = np.exp(1j * self.omega * otherlti.dt)
self.frdata = otherlti(z, squeeze=False)
arg_dt = otherlti.dt
# Copy over signal and system names, if not specified
kwargs['inputs'] = kwargs.get('inputs', otherlti.input_labels)
kwargs['outputs'] = kwargs.get(
'outputs', otherlti.output_labels)
if not otherlti._generic_name_check():
kwargs['name'] = kwargs.get('name', _extended_system_name(
otherlti.name, prefix_suffix_name='sampled'))
else:
# The user provided a response and a freq vector
self.frdata = array(args[0], dtype=complex, ndmin=1)
if self.frdata.ndim == 1:
self.frdata = self.frdata.reshape(1, 1, -1)
self.omega = array(args[1], dtype=float, ndmin=1)
if self.frdata.ndim != 3 or self.omega.ndim != 1 or \
self.frdata.shape[-1] != self.omega.shape[-1]:
raise TypeError(
"The frequency data constructor needs a 1-d or 3-d"
" response data array and a matching frequency vector"
" size")
arg_dt = None
elif len(args) == 1:
# Use the copy constructor.
if not isinstance(args[0], FRD):
raise TypeError(
"The one-argument constructor can only take in"
" an FRD object. Received %s." % type(args[0]))
self.omega = args[0].omega
self.frdata = args[0].frdata
arg_dt = args[0].dt
# Copy over signal and system names, if not specified
kwargs['inputs'] = kwargs.get('inputs', args[0].input_labels)
kwargs['outputs'] = kwargs.get('outputs', args[0].output_labels)
else:
raise ValueError(
"Needs 1 or 2 arguments; received %i." % len(args))
#
# Process keyword arguments
#
# If data was generated by a system, keep track of that (used when
# plotting data). Otherwise, use the system name, if given.
self.sysname = kwargs.pop('sysname', kwargs.get('name', None))
# Keep track of default properties for plotting
self.plot_phase = kwargs.pop('plot_phase', None)
self.title = kwargs.pop('title', None)
self.plot_type = kwargs.pop('plot_type', 'bode')
# Keep track of return type
self.return_magphase=kwargs.pop('return_magphase', False)
if self.return_magphase not in (True, False):
raise ValueError("unknown return_magphase value")
self._return_singvals=kwargs.pop('_return_singvals', False)
# Determine whether to squeeze the output
self.squeeze=kwargs.pop('squeeze', None)
if self.squeeze not in (None, True, False):
raise ValueError("unknown squeeze value")
defaults = {
'inputs': self.frdata.shape[1] if not getattr(
self, 'input_index', None) else self.input_labels,
'outputs': self.frdata.shape[0] if not getattr(
self, 'output_index', None) else self.output_labels,
'name': getattr(self, 'name', None)}
if arg_dt is not None:
if isinstance(args[0], LTI):
arg_dt = common_timebase(args[0].dt, arg_dt)
kwargs['dt'] = arg_dt
# Process signal names
name, inputs, outputs, states, dt = _process_iosys_keywords(
kwargs, defaults)
InputOutputSystem.__init__(
self, name=name, inputs=inputs, outputs=outputs, dt=dt, **kwargs)
# create interpolation functions
if smooth:
# Set the order of the fit
if self.omega.size < 2:
raise ValueError("can't smooth with only 1 frequency")
degree = 3 if self.omega.size > 3 else self.omega.size - 1
self._ifunc = empty((self.frdata.shape[0], self.frdata.shape[1]),
dtype=tuple)
for i in range(self.frdata.shape[0]):
for j in range(self.frdata.shape[1]):
self._ifunc[i, j], u = splprep(
u=self.omega, x=[real(self.frdata[i, j, :]),
imag(self.frdata[i, j, :])],
w=1.0/(absolute(self.frdata[i, j, :]) + 0.001),
s=0.0, k=degree)
else:
self._ifunc = None
#
# Frequency response properties
#
# Different properties of the frequency response that can be used for
# analysis and characterization.
#
@property
def magnitude(self):
"""Magnitude of the frequency response.
Magnitude of the frequency response, indexed by either the output
and frequency (if only a single input is given) or the output,
input, and frequency (for multi-input systems). See
`FrequencyResponseData.squeeze` for a description of how this
can be modified using the `squeeze` keyword.
Input and output signal names can be used to index the data in
place of integer offsets.
:type: 1D, 2D, or 3D array
"""
frdata = _process_frequency_response(
self, self.omega, self.frdata, squeeze=self.squeeze)
return NamedSignal(
np.abs(frdata), self.output_labels, self.input_labels)
@property
def phase(self):
"""Phase of the frequency response.
Phase of the frequency response in radians/sec, indexed by either
the output and frequency (if only a single input is given) or the
output, input, and frequency (for multi-input systems). See
`FrequencyResponseData.squeeze` for a description of how this
can be modified using the `squeeze` keyword.
Input and output signal names can be used to index the data in
place of integer offsets.
:type: 1D, 2D, or 3D array
"""
frdata = _process_frequency_response(
self, self.omega, self.frdata, squeeze=self.squeeze)
return NamedSignal(
np.angle(frdata), self.output_labels, self.input_labels)
@property
def frequency(self):
"""Frequencies at which the response is evaluated.
:type: 1D array
"""
return self.omega
@property
def complex(self):
"""Complex value of the frequency response.
Value of the frequency response as a complex number, indexed by
either the output and frequency (if only a single input is given)
or the output, input, and frequency (for multi-input systems). See
`FrequencyResponseData.squeeze` for a description of how this
can be modified using the `squeeze` keyword.
Input and output signal names can be used to index the data in
place of integer offsets.
:type: 1D, 2D, or 3D array
"""
frdata = _process_frequency_response(
self, self.omega, self.frdata, squeeze=self.squeeze)
return NamedSignal(
frdata, self.output_labels, self.input_labels)
@property
def response(self):
warn("response property is deprecated; use complex", FutureWarning)
return self.complex
@property
def fresp(self):
warn("fresp attribute is deprecated; use frdata", FutureWarning)
return self.frdata
def __str__(self):
"""String representation of the transfer function."""
mimo = self.ninputs > 1 or self.noutputs > 1
outstr = [f"{InputOutputSystem.__str__(self)}"]
nl = "\n " if mimo else "\n"
sp = " " if mimo else ""
for i in range(self.ninputs):
for j in range(self.noutputs):
if mimo:
outstr.append(
"\nInput %i to output %i:" % (i + 1, j + 1))
outstr.append(nl + 'Freq [rad/s] Response')
outstr.append(sp + '------------ ---------------------')
outstr.extend(
[sp + '%12.3f %10.4g%+10.4gj' % (w, re, im)
for w, re, im in zip(self.omega,
real(self.frdata[j, i, :]),
imag(self.frdata[j, i, :]))])
return '\n'.join(outstr)
def _repr_eval_(self):
# Loadable format
out = "FrequencyResponseData(\n{d},\n{w}{smooth}".format(
d=repr(self.frdata), w=repr(self.omega),
smooth=(self._ifunc and ", smooth=True") or "")
out += self._dt_repr()
if len(labels := self._label_repr()) > 0:
out += ",\n" + labels
out += ")"
return out
def __neg__(self):
"""Negate a transfer function."""
return FRD(-self.frdata, self.omega)
def __add__(self, other):
"""Add two LTI objects (parallel connection)."""
if isinstance(other, FRD):
# verify that the frequencies match
if len(other.omega) != len(self.omega) or \
(other.omega != self.omega).any():
warn("Frequency points do not match; expect "
"truncation and interpolation.")
# Convert the second argument to a frequency response function.
# or re-base the frd to the current omega (if needed)
if isinstance(other, (int, float, complex, np.number)):
other = _convert_to_frd(
other, omega=self.omega,
inputs=self.ninputs, outputs=self.noutputs)
else:
other = _convert_to_frd(other, omega=self.omega)
# Promote SISO object to compatible dimension
if self.issiso() and not other.issiso():
self = np.ones((other.noutputs, other.ninputs)) * self
elif not self.issiso() and other.issiso():
other = np.ones((self.noutputs, self.ninputs)) * other
# Check that the input-output sizes are consistent.
if self.ninputs != other.ninputs:
raise ValueError(
"The first summand has %i input(s), but the " \
"second has %i." % (self.ninputs, other.ninputs))
if self.noutputs != other.noutputs:
raise ValueError(
"The first summand has %i output(s), but the " \
"second has %i." % (self.noutputs, other.noutputs))
return FRD(self.frdata + other.frdata, other.omega)
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)):
return FRD(self.frdata * other, self.omega,
smooth=(self._ifunc is not None))
else:
other = _convert_to_frd(other, omega=self.omega)
# Promote SISO object to compatible dimension
if self.issiso() and not other.issiso():
self = bdalg.append(*([self] * other.noutputs))
elif not self.issiso() and other.issiso():
other = bdalg.append(*([other] * self.ninputs))
# Check that the input-output sizes are consistent.
if self.ninputs != other.noutputs:
raise ValueError(
"H = G1*G2: input-output size mismatch: "
"G1 has %i input(s), G2 has %i output(s)." %
(self.ninputs, other.noutputs))
inputs = other.ninputs
outputs = self.noutputs
frdata = empty((outputs, inputs, len(self.omega)),
dtype=self.frdata.dtype)
for i in range(len(self.omega)):
frdata[:, :, i] = self.frdata[:, :, i] @ other.frdata[:, :, i]
return FRD(frdata, self.omega,
smooth=(self._ifunc is not None) and
(other._ifunc is not None))
def __rmul__(self, other):
"""Right Multiply two LTI objects (serial connection)."""
# Convert the second argument to an frd function.
if isinstance(other, (int, float, complex, np.number)):
return FRD(self.frdata * other, self.omega,
smooth=(self._ifunc is not None))
else:
other = _convert_to_frd(other, omega=self.omega)
# Promote SISO object to compatible dimension
if self.issiso() and not other.issiso():
self = bdalg.append(*([self] * other.ninputs))
elif not self.issiso() and other.issiso():
other = bdalg.append(*([other] * self.noutputs))
# Check that the input-output sizes are consistent.
if self.noutputs != other.ninputs:
raise ValueError(
"H = G1*G2: input-output size mismatch: "
"G1 has %i input(s), G2 has %i output(s)." %
(other.ninputs, self.noutputs))
inputs = self.ninputs
outputs = other.noutputs
frdata = empty((outputs, inputs, len(self.omega)),
dtype=self.frdata.dtype)
for i in range(len(self.omega)):
frdata[:, :, i] = other.frdata[:, :, i] @ self.frdata[:, :, i]
return FRD(frdata, self.omega,
smooth=(self._ifunc is not None) and
(other._ifunc is not None))
# 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)):
return FRD(self.frdata * (1/other), self.omega,
smooth=(self._ifunc is not None))
else:
other = _convert_to_frd(other, omega=self.omega)
if (other.ninputs > 1 or other.noutputs > 1):
# FRD.__truediv__ is currently only implemented for SISO systems
return NotImplemented
return FRD(self.frdata/other.frdata, self.omega,
smooth=(self._ifunc is not None) and
(other._ifunc is not None))
# 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)):
return FRD(other / self.frdata, self.omega,
smooth=(self._ifunc is not None))
else:
other = _convert_to_frd(other, omega=self.omega)
if (self.ninputs > 1 or self.noutputs > 1):
# FRD.__rtruediv__ is currently only implemented for SISO systems
return NotImplemented
return other / self
def __pow__(self, other):
if not type(other) == int:
raise ValueError("Exponent must be an integer")
if other == 0:
return FRD(ones(self.frdata.shape), self.omega,
smooth=(self._ifunc is not None)) # unity
if other > 0:
return self * (self**(other-1))
if other < 0:
return (FRD(ones(self.frdata.shape), self.omega) / self) * \
(self**(other+1))
# Define the `eval` function to evaluate an FRD at a given (real)
# frequency. Note that we choose to use `eval` instead of `evalfr` to
# avoid confusion with `evalfr`, which takes a complex number as its
# argument. Similarly, we don't use `__call__` to avoid confusion between
# G(s) for a transfer function and G(omega) for an FRD object.
# update Sawyer B. Fuller 2020.08.14: __call__ added to provide a uniform
# interface to systems in general and the lti.frequency_response method
def eval(self, omega, squeeze=None):
"""Evaluate a transfer function at a frequency point.
Note that a "normal" FRD only returns values for which there is an
entry in the `omega` vector. An interpolating FRD can return
intermediate values.
Parameters
----------
omega : float or 1D array_like
Frequency(s) for evaluation, in radians per second.
squeeze : bool, optional
If `squeeze` = True, remove single-dimensional entries from the
shape of the output even if the system is not SISO. If
`squeeze` = False, keep all indices (output, input and, if
`omega` is array_like, frequency) even if the system is
SISO. The default value can be set using
`config.defaults['control.squeeze_frequency_response']`.
Returns
-------
frdata : complex ndarray
The frequency response of the system. If the system is SISO
and `squeeze` is not True, the shape of the array matches the
shape of `omega`. If the system is not SISO or `squeeze` is
False, the first two dimensions of the array are indices for
the output and input and the remaining dimensions match `omega`.
If `squeeze` is True then single-dimensional axes are removed.
"""
omega_array = np.array(omega, ndmin=1) # array of frequencies
# Make sure that we are operating on a simple list
if len(omega_array.shape) > 1:
raise ValueError("input list must be 1D")
# Make sure that frequencies are all real-valued
if any(omega_array.imag > 0):
raise ValueError("eval can only accept real-valued frequencies")
if self._ifunc is None:
elements = np.isin(self.omega, omega) # binary array
if sum(elements) < len(omega_array):
raise ValueError(
"not all frequencies are in frequency list of FRD "
"system. Try an interpolating FRD for additional points.")
else:
out = self.frdata[:, :, elements]
else:
out = empty((self.noutputs, self.ninputs, len(omega_array)),
dtype=complex)
for i in range(self.noutputs):
for j in range(self.ninputs):
for k, w in enumerate(omega_array):
frraw = splev(w, self._ifunc[i, j], der=0)
out[i, j, k] = frraw[0] + 1.0j * frraw[1]
return _process_frequency_response(self, omega, out, squeeze=squeeze)
def __call__(self, x=None, squeeze=None, return_magphase=None):
"""Evaluate system transfer function at point in complex plane.
Returns the value of the system's transfer function at a point `x`
in the complex plane, where `x` is `s` for continuous-time systems
and `z` for discrete-time systems. For a frequency response data
object, the argument should be an imaginary number (since only the
frequency response is defined) and only the imaginary component of
`x` will be used.
By default, a (complex) scalar will be returned for SISO systems
and a p x m array will be return for MIMO systems with m inputs and
p outputs. This can be changed using the `squeeze` keyword.
To evaluate at a frequency `omega` in radians per second, enter ``x
= omega * 1j`` for continuous-time systems, ``x = exp(1j * omega *
dt)`` for discrete-time systems, or use the
`~LTI.frequency_response` method.
If `x` is not given, this function creates a copy of a frequency
response data object with a different set of output settings.
Parameters
----------
x : complex scalar or 1D array_like
Imaginary value(s) at which frequency response will be evaluated.
The real component of `x` is ignored. If not specified, return
a copy of the frequency response data object with updated
settings for output processing (`squeeze`, `return_magphase`).
squeeze : bool, optional
Squeeze output, as described below. Default value can be set
using `config.defaults['control.squeeze_frequency_response']`.
return_magphase : bool, optional
(`x` = None only) If True, then a frequency response data object
will enumerate as a tuple of the form ``(mag, phase, omega)``
where where `mag` is the magnitude (absolute value, not dB or
log10) of the system frequency response, `phase` is the wrapped
phase in radians of the system frequency response, and `omega` is
the (sorted) frequencies at which the response was evaluated.
Returns
-------
frdata : complex ndarray
The value of the system transfer function at `x`. If the system
is SISO and `squeeze` is not True, the shape of the array matches
the shape of `x`. If the system is not SISO or `squeeze` is
False, the first two dimensions of the array are indices for the
output and input and the remaining dimensions match `x`. If
`squeeze` is True then single-dimensional axes are removed.
Raises
------
ValueError
If `s` is not purely imaginary, because `FrequencyResponseData`
systems are only defined at imaginary values (corresponding to
real frequencies).
"""
if x is None:
# Create a copy of the response with new keywords
response = copy(self)
# Update any keywords that we were passed
response.squeeze = self.squeeze if squeeze is None else squeeze
response.return_magphase = self.return_magphase \
if return_magphase is None else return_magphase
return response
if return_magphase is not None:
raise ValueError("return_magphase not allowed when x != None")
# Make sure that we are operating on a simple list
if len(np.atleast_1d(x).shape) > 1:
raise ValueError("input list must be 1D")
if any(abs(np.atleast_1d(x).real) > 0):
raise ValueError("__call__: FRD systems can only accept "
"purely imaginary frequencies")
# need to preserve array or scalar status
if hasattr(x, '__len__'):
return self.eval(np.asarray(x).imag, squeeze=squeeze)
else:
return self.eval(complex(x).imag, squeeze=squeeze)
# Implement iter to allow assigning to a tuple
def __iter__(self):
frdata = _process_frequency_response(
self, self.omega, self.frdata, squeeze=self.squeeze)
if self._return_singvals:
# Legacy processing for singular values
return iter((self.frdata[:, 0, :], self.omega))
elif not self.return_magphase:
return iter((self.omega, frdata))
return iter((np.abs(frdata), np.angle(frdata), self.omega))
def __getitem__(self, key):
if not isinstance(key, Iterable) or len(key) != 2:
# Implement (thin) getitem to allow access via legacy indexing
return list(self.__iter__())[key]
# Convert signal names to integer offsets (via NamedSignal object)
iomap = NamedSignal(
self.frdata[:, :, 0], self.output_labels, self.input_labels)
indices = iomap._parse_key(key, level=1) # ignore index checks
outdx, outputs = _process_subsys_index(indices[0], self.output_labels)
inpdx, inputs = _process_subsys_index(indices[1], self.input_labels)
# Create the system name
sysname = config.defaults['iosys.indexed_system_name_prefix'] + \
self.name + config.defaults['iosys.indexed_system_name_suffix']
return FrequencyResponseData(
self.frdata[outdx, :][:, inpdx], self.omega, self.dt,
inputs=inputs, outputs=outputs, name=sysname)
# Implement (thin) len to emulate legacy testing interface
def __len__(self):
return 3 if self.return_magphase else 2
def freqresp(self, omega):
"""(deprecated) Evaluate transfer function at complex frequencies.
.. deprecated::0.9.0
Method has been given the more Pythonic name
`FrequencyResponseData.frequency_response`. Or use
`freqresp` in the MATLAB compatibility module.
"""
warn("FrequencyResponseData.freqresp(omega) will be removed in a "
"future release of python-control; use "
"FrequencyResponseData.frequency_response(omega), or "
"freqresp(sys, omega) in the MATLAB compatibility module "
"instead", FutureWarning)
return self.frequency_response(omega)
def feedback(self, other=1, sign=-1):
"""Feedback interconnection between two FRD objects.
Parameters
----------
other : `LTI`
System in the feedback path.
sign : float, optional
Gain to use in feedback path. Defaults to -1.
"""
other = _convert_to_frd(other, omega=self.omega)
if (self.noutputs != other.ninputs or self.ninputs != other.noutputs):
raise ValueError(
"FRD.feedback, inputs/outputs mismatch")
# TODO: handle omega re-mapping
# reorder array axes in order to leverage numpy broadcasting
myfrdata = np.moveaxis(self.frdata, 2, 0)
otherfrdata = np.moveaxis(other.frdata, 2, 0)
I_AB = eye(self.ninputs)[np.newaxis, :, :] + otherfrdata @ myfrdata
resfrdata = (myfrdata @ linalg.inv(I_AB))
frdata = np.moveaxis(resfrdata, 0, 2)
return FRD(frdata, other.omega, smooth=(self._ifunc is not None))
def append(self, other):
"""Append a second model to the present model.
The second model is converted to FRD if necessary, inputs and
outputs are appended and their order is preserved.
Parameters
----------
other : `LTI`
System to be appended.
Returns
-------
sys : `FrequencyResponseData`
System model with `other` appended to `self`.
"""
other = _convert_to_frd(other, omega=self.omega, inputs=other.ninputs,
outputs=other.noutputs)
# TODO: handle omega re-mapping
new_frdata = np.zeros(
(self.noutputs + other.noutputs, self.ninputs + other.ninputs,
self.omega.shape[-1]), dtype=complex)
new_frdata[:self.noutputs, :self.ninputs, :] = np.reshape(
self.frdata, (self.noutputs, self.ninputs, -1))
new_frdata[self.noutputs:, self.ninputs:, :] = np.reshape(
other.frdata, (other.noutputs, other.ninputs, -1))
return FRD(new_frdata, self.omega, smooth=(self._ifunc is not None))
# Plotting interface
def plot(self, plot_type=None, *args, **kwargs):
"""Plot the frequency response using Bode or singular values plot.
Plot the frequency response using either a standard Bode plot
(plot_type='bode', default) or a singular values plot
(plot_type='svplot'). See `bode_plot` and `singular_values_plot`
for more detailed descriptions.
"""
from .freqplot import bode_plot, singular_values_plot
from .nichols import nichols_plot
if plot_type is None:
plot_type = self.plot_type
if plot_type == 'bode':
return bode_plot(self, *args, **kwargs)
elif plot_type == 'nichols':
return nichols_plot(self, *args, **kwargs)
elif plot_type == 'svplot':
return singular_values_plot(self, *args, **kwargs)
else:
raise ValueError(f"unknown plot type '{plot_type}'")
# Convert to pandas
def to_pandas(self):
"""Convert response data to pandas data frame.
Creates a pandas data frame for the value of the frequency
response at each `omega`. The frequency response values are
labeled in the form "H_{<out>, <in>}" where "<out>" and "<in>"
are replaced with the output and input labels for the system.
"""
if not pandas_check():
ImportError('pandas not installed')
import pandas
# Create a dict for setting up the data frame
data = {'omega': self.omega}
data.update(
{'H_{%s, %s}' % (out, inp): self.frdata[i, j] \
for i, out in enumerate(self.output_labels) \
for j, inp in enumerate(self.input_labels)})
return pandas.DataFrame(data)
#
# Allow FRD as an alias for the FrequencyResponseData class
#
# Note: This class was initially given the name "FRD", but this caused
# problems with documentation on MacOS platforms, since files were generated
# for control.frd and control.FRD, which are not differentiated on most MacOS
# file systems, which are case insensitive. Renaming the FRD class to be
# FrequencyResponseData and then assigning FRD to point to the same object
# fixes this problem.
#
FRD = FrequencyResponseData
def _convert_to_frd(sys, omega, inputs=1, outputs=1):
"""Convert a system to frequency response data form (if needed).
If `sys` is already a frequency response data object, and its frequency
range matches or overlaps the range given in `omega` then it is
returned. If `sys` is another LTI object or a transfer function, then
it is converted to a frequency response data system at the specified
values in `omega`. If `sys` is a scalar, then the number of inputs and
outputs can be specified manually, as in:
>>> import numpy as np
>>> from control.frdata import _convert_to_frd
>>> omega = np.logspace(-1, 1)
>>> frd = _convert_to_frd(3., omega) # Assumes inputs = outputs = 1
>>> frd.ninputs, frd.noutputs
(1, 1)
>>> frd = _convert_to_frd(1., omega, inputs=3, outputs=2)
>>> frd.ninputs, frd.noutputs
(3, 2)
In the latter example, sys's matrix transfer function is [[1., 1., 1.]
[1., 1., 1.]].
"""
if isinstance(sys, FRD):
omega.sort()
if len(omega) == len(sys.omega) and \
(abs(omega - sys.omega) < FRD._epsw).all():
# frequencies match, and system was already frd; simply use
return sys
raise NotImplementedError(
"Frequency ranges of FRD do not match, conversion not implemented")
elif isinstance(sys, LTI):
omega = np.sort(omega)