Skip to content

Commit 80edad6

Browse files
committed
added frd to matlab compat file, fixed FRD construction from 1d data
1 parent 872fb35 commit 80edad6

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

src/frdata.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class FRD(Lti):
107107
def __init__(self, *args, **kwargs):
108108
"""Construct a transfer function.
109109
110-
The default constructor is FRD(w, d), where w is an iterable of
110+
The default constructor is FRD(d, w), where w is an iterable of
111111
frequency points, and d is the matching frequency data.
112112
113113
If d is a single list, 1d array, or tuple, a SISO system description
@@ -142,8 +142,8 @@ def __init__(self, *args, **kwargs):
142142
# The user provided a response and a freq vector
143143
self.fresp = array(args[0], dtype=complex)
144144
if len(self.fresp.shape) == 1:
145-
self.fresp.reshape(1, 1, len(args[0]))
146-
self.omega = array(args[1])
145+
self.fresp = self.fresp.reshape(1, 1, len(args[0]))
146+
self.omega = array(args[1], dtype=float)
147147
if len(self.fresp.shape) != 3 or \
148148
self.fresp.shape[-1] != self.omega.shape[-1] or \
149149
len(self.omega.shape) != 1:
@@ -189,9 +189,9 @@ def __str__(self):
189189
if mimo:
190190
outstr.append("Input %i to output %i:" % (i + 1, j + 1))
191191
outstr.append('Freq [rad/s] Response ')
192-
outstr.append('------------ ------------------------')
192+
outstr.append('------------ ---------------------')
193193
outstr.extend(
194-
[ '%12.3f %10.4g + %10.4g' % (w, m, p)
194+
[ '%12.3f %10.4g%+10.4gj' % (w, m, p)
195195
for m, p, w in zip(real(self.fresp[j,i,:]), imag(self.fresp[j,i,:]), wt) ])
196196

197197

src/matlab.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
from control.statesp import StateSpace, _rss_generate, _convertToStateSpace
9090
from control.xferfcn import TransferFunction, _convertToTransferFunction
9191
from control.lti import Lti #base class of StateSpace, TransferFunction
92+
from control.frdata import FRD
9293
from control.dtime import sample_system
9394
from control.exception import ControlArgument
9495

@@ -125,7 +126,7 @@
125126
\* :func:`ss` create state-space (SS) models
126127
\ dss create descriptor state-space models
127128
\ delayss create state-space models with delayed terms
128-
\ frd create frequency response data (FRD) models
129+
\* :func:`frd` create frequency response data (FRD) models
129130
\ lti/exp create pure continuous-time delays (TF and
130131
ZPK only)
131132
\ filt specify digital filters
@@ -156,7 +157,7 @@
156157
\* :func:`tf` conversion to transfer function
157158
\ zpk conversion to zero/pole/gain
158159
\* :func:`ss` conversion to state space
159-
\ frd conversion to frequency data
160+
\* :func:`frd` conversion to frequency data
160161
\ c2d continuous to discrete conversion
161162
\ d2c discrete to continuous conversion
162163
\ d2d resample discrete-time model
@@ -569,6 +570,42 @@ def tf(*args):
569570
else:
570571
raise ValueError("Needs 1 or 2 arguments; received %i." % len(args))
571572

573+
def frd(*args):
574+
'''
575+
Construct a Frequency Response Data model, or convert a system
576+
577+
frd models store the (measured) frequency response of a system.
578+
579+
This function can be called in different ways:
580+
581+
``frd(response, freqs)``
582+
Create an frd model with the given response data, in the form of
583+
complex response vector, at matching frequency freqs [in rad/s]
584+
585+
``frd(sys, freqs)``
586+
Convert an Lti system into an frd model with data at frequencies
587+
freqs.
588+
589+
Parameters
590+
----------
591+
response: array_like, or list
592+
complex vector with the system response
593+
freq: array_lik or lis
594+
vector with frequencies
595+
sys: Lti (StateSpace or TransferFunction)
596+
A linear system
597+
598+
Returns
599+
-------
600+
sys: FRD
601+
New frequency response system
602+
603+
See Also
604+
--------
605+
ss, tf
606+
'''
607+
return FRD(*args)
608+
572609

573610
def ss2tf(*args):
574611
"""

tests/matlab_test.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import numpy as np
1414
import scipy as sp
1515
from control.matlab import *
16+
from control.frdata import FRD
1617

1718
class TestMatlab(unittest.TestCase):
1819
def setUp(self):
@@ -408,7 +409,14 @@ def testConnect(self):
408409
np.testing.assert_array_almost_equal(
409410
sysc.D, np.mat('0; 0'))
410411

411-
412+
def testFRD(self):
413+
h = tf([1], [1, 2, 2])
414+
omega = np.logspace(-1, 2, 10)
415+
frd1 = frd(h, omega)
416+
assert isinstance(frd1, FRD)
417+
frd2 = frd(frd1.fresp[0,0,:], omega)
418+
assert isinstance(frd2, FRD)
419+
412420

413421
#! TODO: not yet implemented
414422
# def testMIMOtfdata(self):

0 commit comments

Comments
 (0)