scipy.signal.bode doesn't work for state space (A, B, C, D) argument (Trac #1863) #2382

Closed
scipy-gitbot opened this Issue Apr 25, 2013 · 6 comments

Comments

Projects
None yet
5 participants

Original ticket http://projects.scipy.org/scipy/ticket/1863 on 2013-03-11 by @hazelnusse, assigned to @cournape.

See attached for an example of what fails. This is the error I get:

/home/hazelnusse/usr/lib64/python3.2/site-packages/scipy/signal/filter_design.py:306: BadCoefficients: Badly conditioned filter coefficients (numerator): the results may be meaningless
"results may be meaningless", BadCoefficients)
Traceback (most recent call last):
File "test_bode.py", line 19, in
bode((A, B, C, D))
File "/home/hazelnusse/usr/lib64/python3.2/site-packages/scipy/signal/ltisys.py", line 944, in bode
y = numpy.polyval(sys.num, jw) / numpy.polyval(sys.den, jw)
File "/home/hazelnusse/usr/lib64/python3.2/site-packages/numpy/lib/polynomial.py", line 671, in polyval
y = x * y + p[i]
ValueError: operands could not be broadcast together with shapes (100) (3)

Attachment added by @hazelnusse on 2013-03-11: test_bode.py

@hazelnusse wrote on 2013-03-13

PR gh-959 (#432) was recently mereged but this bug still remains.

@WarrenWeckesser @bjornfor: I just wanted to give you guys a heads up that this issue is still arround. Am I using signal.bode() incorrectly or is it just that the state space interface hasn't been tested as much as the transfer function interface?

Contributor

bjornfor commented Apr 25, 2013

Hm, maybe I never tested state space? Not sure what happens here.

/home/bfo/.local/lib/python2.7/site-packages/scipy/signal/ltisys.pyc in bode(system, w, n)
    927 
    928     jw = w * 1j
--> 929     y = numpy.polyval(sys.num, jw) / numpy.polyval(sys.den, jw)
    930     mag = 20.0 * numpy.log10(abs(y))
    931     phase = numpy.arctan2(y.imag, y.real) * 180.0 / numpy.pi

/usr/lib/python2.7/dist-packages/numpy/lib/polynomial.pyc in polyval(p, x)
    640         y = NX.zeros_like(x)
    641     for i in range(len(p)):
--> 642         y = x * y + p[i]
    643     return y
    644 

ValueError: operands could not be broadcast together with shapes (100) (3) 
> /usr/lib/python2.7/dist-packages/numpy/lib/polynomial.py(642)polyval()
    641     for i in range(len(p)):
--> 642         y = x * y + p[i]
    643     return y
ipdb> p x * y
array([ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
        0.+0.j,  0.+0.j])
ipdb> p p[i]
array([0, 0, 1])

An array of shape 100x1 doesn't add with an array of shape 3x1...

Member

WarrenWeckesser commented Apr 26, 2013

@hazelnusse: Thanks for reporting the problem. As you pointed out in #432, the problem is that when an lti instance is created from state space matrices, sys.num is a 2D array containing a single row. I would like to fix that problem, too, but that has been the behavior for a long time, and I don't know if there would be a reasonable way to deprecate it.

I have submitted a pull request that fixes bode (and freqresp,which has the same problem) here: #2382

rgommers closed this in 3560b67 May 6, 2013

Owner

rgommers commented May 6, 2013

gh-2382 merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment