I am having a rough time trying to use the lti portion of scipy.signal. For starters, I just wanted to generate a step response for an integrator (1/s). I tried creating this system two ways, and neither seems to work correctly. I think this system has a numerator of 1 and a denominator of [1,0]. If I create the system based on the num, den I get: In [51]: mysys=scipy.signal.lti(1,[1,0]) In [52]: mysys.A Out[52]: NumPy array, format: long [[-0.]] In [53]: mysys.B Out[53]: NumPy array, format: long [[1]] In [54]: mysys.C Out[54]: NumPy array, format: long [[ 1.]] In [55]: mysys.D Out[55]: NumPy array, format: long [ 0.] I get an error when I try to find the step response of this system. I think part of the problem is that the matrices aren't right. I think they should be In [46]: A=array([[0,1],[0,0]]) In [47]: B=array([[0],[1]]) In [48]: C=array([[1,0]]) In [49]: D=array([[0]]) but for some reason I can't create a system with these matrices: In [50]: mysys=scipy.signal.lti(A,B,C,D) --------------------------------------------------------------------------- exceptions.TypeError Traceback (most recent call last) /home/ryan/siue/mechatronics/system_modeling/<ipython console> /usr/lib/python2.4/site-packages/scipy/signal/ltisys.py in __init__(self, *args, **kwords) 223 self.__dict__['C'], \ 224 self.__dict__['D'] = abcd_normalize(*args)--> 225 self.__dict__['zeros'], self.__dict__['poles'], \ 226 self.__dict__['gain'] = ss2zpk(*args) 227 self.__dict__['num'], self.__dict__['den'] = ss2tf(*args) /usr/lib/python2.4/site-packages/scipy/signal/ltisys.py in ss2zpk(A, B, C, D, input) 183 z, p, k -- zeros and poles in sequences and gain constant. 184 """ --> 185 return tf2zpk(*ss2tf(A,B,C,D,input=input)) 186 187 class lti: /usr/lib/python2.4/site-packages/scipy/signal/ltisys.py in ss2tf(A, B, C, D, input) 154 for k in range(nout): 155 Ck = atleast_2d(C[k,:]) --> 156 num[k] = poly(A - dot(B,Ck)) + (D[k]-1)*den 157 158 return num, den TypeError: array cannot be safely cast to required type What am I doing wrong? Thanks, Ryan
Hi Ryan On Thu, Sep 07, 2006 at 09:55:29PM -0500, Ryan Krauss wrote:
I am having a rough time trying to use the lti portion of scipy.signal. For starters, I just wanted to generate a step response for an integrator (1/s). I tried creating this system two ways, and neither seems to work correctly. I think this system has a numerator of 1 and a denominator of [1,0].
<snip>
In [46]: A=array([[0,1],[0,0]])
In [47]: B=array([[0],[1]])
In [48]: C=array([[1,0]])
In [49]: D=array([[0]])
but for some reason I can't create a system with these matrices:
In [50]: mysys=scipy.signal.lti(A,B,C,D)
<snip>
TypeError: array cannot be safely cast to required type
Are you running a 64-bit platform? Maybe try import numpy as N import scipy.signal A=N.array([[0,1],[0,0]],dtype=N.int32) B=N.array([[0],[1]],dtype=N.int32) C=N.array([[1,0]],dtype=N.int32) D=N.array([[0]],dtype=N.int32) mysys=scipy.signal.lti(A,B,C,D) Regards Stéfan
Thanks for your thought Stefan. No, I am not running on a 64 bit machine. I tried your idea and tried dtype=N.float32 (which is what I think it should be). Same error message. Ryan On 9/8/06, Stefan van der Walt <stefan@sun.ac.za> wrote:
Hi Ryan
On Thu, Sep 07, 2006 at 09:55:29PM -0500, Ryan Krauss wrote:
I am having a rough time trying to use the lti portion of scipy.signal. For starters, I just wanted to generate a step response for an integrator (1/s). I tried creating this system two ways, and neither seems to work correctly. I think this system has a numerator of 1 and a denominator of [1,0].
<snip>
In [46]: A=array([[0,1],[0,0]])
In [47]: B=array([[0],[1]])
In [48]: C=array([[1,0]])
In [49]: D=array([[0]])
but for some reason I can't create a system with these matrices:
In [50]: mysys=scipy.signal.lti(A,B,C,D)
<snip>
TypeError: array cannot be safely cast to required type
Are you running a 64-bit platform? Maybe try
import numpy as N import scipy.signal
A=N.array([[0,1],[0,0]],dtype=N.int32) B=N.array([[0],[1]],dtype=N.int32) C=N.array([[1,0]],dtype=N.int32) D=N.array([[0]],dtype=N.int32) mysys=scipy.signal.lti(A,B,C,D)
Regards Stéfan _______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
Ryan Krauss wrote:
I am having a rough time trying to use the lti portion of scipy.signal. For starters, I just wanted to generate a step response for an integrator (1/s).
It turns out your starting-point is a special-case not handled by the current code (I checked this on old SciPy and it raised errors as well). The current code is "general-purpose" but does not handle lonely integrators. Thus, any time your denominator ends in a zero, you will have trouble. This case needs to be dealt with separately but so far isn't (the error could be more informative of course). You would be better of trying a simple first-order system as your starting point. 1/(s+1) Which works fine for me with current SciPy. from pylab import * from scipy import * plot(*signal.step((1,[1,1]))) -Travis
On Fri, Sep 08, 2006 at 07:12:35PM -0600, Travis Oliphant wrote:
Ryan Krauss wrote:
I am having a rough time trying to use the lti portion of scipy.signal. For starters, I just wanted to generate a step response for an integrator (1/s).
It turns out your starting-point is a special-case not handled by the current code (I checked this on old SciPy and it raised errors as well). The current code is "general-purpose" but does not handle lonely integrators. Thus, any time your denominator ends in a zero, you will have trouble. This case needs to be dealt with separately but so far isn't (the error could be more informative of course). You would be better of trying a simple first-order system as your starting point.
Interestingly, he didn't get the IndexError: index out of bounds that I see, but rather TypeError: array cannot be safely cast to required type which is what lead me to ask about the 64-bit platform. Same problem? Regards Stéfan
I thought about this over the weekend. I was wrong about the matrices not being correct. The A matrix really is just 0. So, passing in the numerator and denominator polynomials does correctly create a system, you just can't take its step response. I will play with other systems for now. Is anyone maintaining this code? I am going to be using it more the systems modeling portion of my mechatronics class and I am trying to convert students from Matlab. I will also be teaching Dynamic System Modeling in the spring which is basically transfer functions and impulse and step responses. I would be willing to invest some time in this if no one is doing it. But if someone already knows the code and can easily modify it, that would be great. Ryan On 9/8/06, Travis Oliphant <oliphant@ee.byu.edu> wrote:
Ryan Krauss wrote:
I am having a rough time trying to use the lti portion of scipy.signal. For starters, I just wanted to generate a step response for an integrator (1/s).
It turns out your starting-point is a special-case not handled by the current code (I checked this on old SciPy and it raised errors as well). The current code is "general-purpose" but does not handle lonely integrators. Thus, any time your denominator ends in a zero, you will have trouble. This case needs to be dealt with separately but so far isn't (the error could be more informative of course). You would be better of trying a simple first-order system as your starting point.
1/(s+1)
Which works fine for me with current SciPy.
from pylab import * from scipy import *
plot(*signal.step((1,[1,1])))
-Travis
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
Ryan Krauss wrote:
I thought about this over the weekend. I was wrong about the matrices not being correct. The A matrix really is just 0. So, passing in the numerator and denominator polynomials does correctly create a system, you just can't take its step response.
I will play with other systems for now.
Is anyone maintaining this code?
Yes. If it's in the SciPy space it's being maintained (by me at least). Some things were pulled out into the sandbox (ga, cow) because they were not being maintained. I use the lti stuff whenever I teach our signals class. -Travis
Hello,
Ryan Krauss wrote:
I thought about this over the weekend. I was wrong about the matrices not being correct. The A matrix really is just 0. So, passing in the numerator and denominator polynomials does correctly create a system, you just can't take its step response.
shouldn't signal.lsim2 be able to handle such systems? I tried the following: #*************** start shell session from scipy import signal from numpy import * sys1=signal.lti((1),(1,0)) signal.lsim2(sys1, ones(10), arange(10)) Traceback (most recent call last): File "<input>", line 1, in ? File "/usr/lib/python2.4/site-packages/scipy/signal/ltisys.py", line 333, in lsim2 ufunc = interpolate.linear_1d(T, U, axis=0, bounds_error=0, fill_value=0) AttributeError: 'module' object has no attribute 'linear_1d' #***************** end shell session What about this error? I get the same errror with sys=signal.lti((1),(1,1)). Thanks Lars
On Tue, Sep 12, 2006 at 07:04:06AM +0200, Lars Friedrich wrote:
I thought about this over the weekend. I was wrong about the matrices not being correct. The A matrix really is just 0. So, passing in the numerator and denominator polynomials does correctly create a system, you just can't take its step response.
shouldn't signal.lsim2 be able to handle such systems? I tried the following:
#*************** start shell session from scipy import signal from numpy import *
sys1=signal.lti((1),(1,0))
signal.lsim2(sys1, ones(10), arange(10))
Traceback (most recent call last): File "<input>", line 1, in ? File "/usr/lib/python2.4/site-packages/scipy/signal/ltisys.py", line 333, in lsim2 ufunc = interpolate.linear_1d(T, U, axis=0, bounds_error=0, fill_value=0) AttributeError: 'module' object has no attribute 'linear_1d' #***************** end shell session
Should be fixed in SVN now. Cheers Stéfan
Lars Friedrich wrote:
Hello,
Ryan Krauss wrote:
I thought about this over the weekend. I was wrong about the matrices not being correct. The A matrix really is just 0. So, passing in the numerator and denominator polynomials does correctly create a system, you just can't take its step response.
shouldn't signal.lsim2 be able to handle such systems? I tried the following:
#*************** start shell session from scipy import signal from numpy import *
sys1=signal.lti((1),(1,0))
signal.lsim2(sys1, ones(10), arange(10))
Traceback (most recent call last): File "<input>", line 1, in ? File "/usr/lib/python2.4/site-packages/scipy/signal/ltisys.py", line 333, in lsim2 ufunc = interpolate.linear_1d(T, U, axis=0, bounds_error=0, fill_value=0) AttributeError: 'module' object has no attribute 'linear_1d' #***************** end shell session
What about this error? I get the same errror with sys=signal.lti((1),(1,1)).
Thanks Lars
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
BTW, an "entry" for lsim2 is missing help (signal) yields ... Linear Systems: lti -- linear time invariant system object. lsim -- continuous-time simulation of output to linear system. impulse -- impulse response of linear, time-invariant (LTI) system. step -- step response of continous-time LTI system. Nils
I think Lars is right. I made 1 change to line 333 of signal/ltisys.py: # ufunc = interpolate.linear_1d(T, U, axis=0, bounds_error=0, fill_value=0) ufunc = interpolate.interp1d(T, U, axis=0, bounds_error=0, fill_value=0) It seems like interp1d replaces linear_1d. With this change, I was able to generate this fabulous step response of an intergrator based on this code: T=2.0 fs=100 dt=1.0/fs t=arange(0.0,T+dt,dt) u=where(t>0.5,1,0) ylim([-0.1,1.1]) mysys=signal.lti(1,[1,0]) yo=signal.lsim2(mysys,u,t) cla() plot(t,u) plot(t,yo[1]) legend(['input','output'],2) xlabel('Time (sec)') ylabel('Amplitude') Ryan On 9/12/06, Lars Friedrich <lfriedri@imtek.de> wrote:
Hello,
Ryan Krauss wrote:
I thought about this over the weekend. I was wrong about the matrices not being correct. The A matrix really is just 0. So, passing in the numerator and denominator polynomials does correctly create a system, you just can't take its step response.
shouldn't signal.lsim2 be able to handle such systems? I tried the following:
#*************** start shell session from scipy import signal from numpy import *
sys1=signal.lti((1),(1,0))
signal.lsim2(sys1, ones(10), arange(10))
Traceback (most recent call last): File "<input>", line 1, in ? File "/usr/lib/python2.4/site-packages/scipy/signal/ltisys.py", line 333, in lsim2 ufunc = interpolate.linear_1d(T, U, axis=0, bounds_error=0, fill_value=0) AttributeError: 'module' object has no attribute 'linear_1d' #***************** end shell session
What about this error? I get the same errror with sys=signal.lti((1),(1,1)).
Thanks Lars
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
On Tue, Sep 12, 2006 at 10:37:28PM -0500, Ryan Krauss wrote:
I think Lars is right. I made 1 change to line 333 of signal/ltisys.py: # ufunc = interpolate.linear_1d(T, U, axis=0, bounds_error=0, fill_value=0) ufunc = interpolate.interp1d(T, U, axis=0, bounds_error=0, fill_value=0)
It seems like interp1d replaces linear_1d. With this change, I was able to generate this fabulous step response of an intergrator based on this code:
As mentioned yesterday: http://projects.scipy.org/scipy/scipy/changeset/2196 Regards Stéfan
Hello scipy users, for a short time now, I am using python, numpy and scipy as a replacement for matlab. My setup at the moment is the "scite"-editor and the "ipython"-shell. I am happy using the "run -i" command to access the current workspace with my scripts written with scite. There is one feature that I know from matlab that I miss a little. Maybe its not crucial but it would be nice to have: When I made a change to my script in scite, I have to *save it *change scope to ipython *recall the "run -i myScript.py" command *press <enter> In matlab it is possible to hit <F5> to get the same effect. How can I achieve this with scite/ipython? I think, I need a way to tell ipython "from extern" to execute the "run"-command. I would put this to my scite-config-file to connect it with, say, the <F5> button and would be fine. Is this possible? Thanks for every comment Lars
Hello Lars, I don't think there is an obvious way of doing this with ipython (though you might get a different answer from a ipython wizard). I have a suggestion. I haven't tested this thoroughly, but you can have a look. Use SPE (Stany's Python Editor) as an editor. Spe has a python shell. To use pylab in the python shell you should use the WXAgg backend (edit your ~/.matplotlib/.matplotlibrc and set backend : WXAgg Then in the shell you can use python interactively or you can run the scripts you are editing in it. One big caveat: you create an display a figure before plot to it: You should do from numpy import * from pylab import * t = arange(1,10) figure() # Important: create the figure before attempting to plot to it show() # Important: display the figure before attempting to plot to it plot(t,cos(t)) show() The python shell embedded in SPE isn't as nice as ipython (Robert, if you are listening, didn't I hear you say that you were working on a front-end independent ipython that could replace Wx's pyshell one day). One big feature that you will miss is the interactivity of pylab that you have in ipython (not have to enter the show() command, and the ability to zoom. Maybe it is possible to get matplotlib to do this here to, you can try asking on the matplotlib list. Using SPE is just an idea an needs to be explored a bit more, but maybe you can do something out of it. Gaël On Fri, Sep 15, 2006 at 09:33:59AM +0200, Lars Friedrich wrote:
Hello scipy users,
for a short time now, I am using python, numpy and scipy as a replacement for matlab. My setup at the moment is the "scite"-editor and the "ipython"-shell. I am happy using the "run -i" command to access the current workspace with my scripts written with scite.
There is one feature that I know from matlab that I miss a little. Maybe its not crucial but it would be nice to have:
When I made a change to my script in scite, I have to *save it *change scope to ipython *recall the "run -i myScript.py" command *press <enter>
In matlab it is possible to hit <F5> to get the same effect. How can I achieve this with scite/ipython? I think, I need a way to tell ipython "from extern" to execute the "run"-command. I would put this to my scite-config-file to connect it with, say, the <F5> button and would be fine. Is this possible?
Thanks for every comment
Lars
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
"Gael" == Gael Varoquaux <gael.varoquaux@normalesup.org> writes:
Gael> Hello Lars, I don't think there is an obvious way of Gael> doing this with ipython (though you might get a different Gael> answer from a ipython wizard). I have a suggestion. I Gael> haven't tested this thoroughly, but you can have a look. Gael> Use SPE (Stany's Python Editor) as an editor. Spe has a Gael> python shell. To use pylab in the python shell you should Gael> use the WXAgg backend (edit your ~/.matplotlib/.matplotlibrc Gael> and set Gael> backend : WXAgg Gael> Then in the shell you can use python interactively or you Gael> can run the scripts you are editing in it. One big caveat: Gael> you create an display a figure before plot to it: Gael> You should do from numpy import * from pylab import * t = Gael> arange(1,10) figure() # Important: create the figure before Gael> attempting to plot to it show() # Important: display the Gael> figure before attempting to plot to it plot(t,cos(t)) show() This use of show should not be necessary (and is explicitly not supported) if you follow the instructions at http://matplotlib.sourceforge.net/interactive.html and http://matplotlib.sourceforge.net/faq.html#SHOW JDH
On Friday 15 September 2006 09:33, Lars Friedrich wrote:
Hello scipy users,
for a short time now, I am using python, numpy and scipy as a replacement for matlab. My setup at the moment is the "scite"-editor and the "ipython"-shell. I am happy using the "run -i" command to access the current workspace with my scripts written with scite.
There is one feature that I know from matlab that I miss a little. Maybe its not crucial but it would be nice to have:
When I made a change to my script in scite, I have to *save it *change scope to ipython *recall the "run -i myScript.py" command *press <enter>
In matlab it is possible to hit <F5> to get the same effect. How can I achieve this with scite/ipython? I think, I need a way to tell ipython "from extern" to execute the "run"-command. I would put this to my scite-config-file to connect it with, say, the <F5> button and would be fine. Is this possible?
Thanks for every comment
If you use KDE, maybe KHotKeys (under KControl -> Region and Accessibility) will be of use. It is quite powerful and can simulate arbitrary keyboard input. However I do not know if it is possible to activate a particular window using keyboard. Johannes
Have you tried editing the file python.properties in the scite's installatio dir? I'm refering to this part of the file: if PLAT_WIN command.go.*.py=python -u "$(FileNameExt)" command.go.subsystem.*.py=0 command.go.*.pyw=pythonw -u "$(FileNameExt)" command.go.subsystem.*.pyw=1 command.build.SConscript=scons.bat --up . command.build.SConstruct=scons.bat . if PLAT_GTK command.go.*.py=python -u $(FileNameExt) command.build.SConscript=scons --up . command.build.SConstruct=scons . You can probably set: command.go.*.py=python -u $(FileNameExt) to command.go.*.py=ipython -i $(FileNameExt) HTH, Edin On 9/15/06, Johannes Loehnert <a.u.r.e.l.i.a.n@gmx.net> wrote:
On Friday 15 September 2006 09:33, Lars Friedrich wrote:
Hello scipy users,
for a short time now, I am using python, numpy and scipy as a replacement for matlab. My setup at the moment is the "scite"-editor and the "ipython"-shell. I am happy using the "run -i" command to access the current workspace with my scripts written with scite.
There is one feature that I know from matlab that I miss a little. Maybe its not crucial but it would be nice to have:
When I made a change to my script in scite, I have to *save it *change scope to ipython *recall the "run -i myScript.py" command *press <enter>
In matlab it is possible to hit <F5> to get the same effect. How can I achieve this with scite/ipython? I think, I need a way to tell ipython "from extern" to execute the "run"-command. I would put this to my scite-config-file to connect it with, say, the <F5> button and would be fine. Is this possible?
Thanks for every comment
If you use KDE, maybe KHotKeys (under KControl -> Region and Accessibility) will be of use. It is quite powerful and can simulate arbitrary keyboard input. However I do not know if it is possible to activate a particular window using keyboard.
Johannes _______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
On 9/15/06, Lars Friedrich <lfriedri@imtek.de> wrote:
Hello scipy users,
for a short time now, I am using python, numpy and scipy as a replacement for matlab. My setup at the moment is the "scite"-editor and the "ipython"-shell. I am happy using the "run -i" command to access the current workspace with my scripts written with scite.
There is one feature that I know from matlab that I miss a little. Maybe its not crucial but it would be nice to have:
When I made a change to my script in scite, I have to *save it *change scope to ipython *recall the "run -i myScript.py" command *press <enter>
In matlab it is possible to hit <F5> to get the same effect. How can I achieve this with scite/ipython? I think, I need a way to tell ipython "from extern" to execute the "run"-command. I would put this to my scite-config-file to connect it with, say, the <F5> button and would be fine. Is this possible?
Unfortunately not currently in an automatic way. Here are a few comments that may help to some degree: * keep in mind that ipython uses a partial-matching search on your history, so if you type 'r', up-arrow, it will probably find the 'run' command quickly without having to up-arrow too many times. This can save some time (that's how I work: C-s, alt-tab, r-up, <enter>). * you can save a macro: macro m <line number where you last ran your script> and then invoke it again by just typing 'm' (or whatever you call it). You can save your macros across sessions by typing store m Macros are a little-used but /extremely/ useful feature of ipython. * (X)Emacs can sort of do what you want, and it can probably be extended to do exactly what you want. If you configure ipython/emacs to work together as explained in http://ipython.scipy.org/doc/manual/node3.html#SECTION00034000000000000000 running your script is one keystroke away. Note that this does NOT do 'run -i', but rather a more pedestrian execfile. But it does support ipdb source tracking, which can be extremely useful. And I'm sure a tiny bit of hacking in the ipython.el file could provide options to call run/run -i instead of just execfile(). * We could probably implement something like this using a named pipe or a socket, but I've really stopped touching the trunk codebase as the chainsaw branch is really far better suited to do this. I created this ticket so we don't forget: http://projects.scipy.org/ipython/ipython/ticket/88 Note that it /can/ be done in the trunk, but I don't have the bandwith to work there right now. If you cook up a patch, by all means send it in. Cheers, f
participants (10)
-
Edin Salković -
Fernando Perez -
Gael Varoquaux -
Johannes Loehnert -
John Hunter -
Lars Friedrich -
Nils Wagner -
Ryan Krauss -
Stefan van der Walt -
Travis Oliphant