我尝试在Python中进行一些信号处理,但我有" TypeError:/不支持的操作数类型/:"对于此代码:
from numbers import Real#Real class:To Complex, 'Real' adds the operations
that work on real numbers.
def as_sys(s):
if isinstance(s,Real):
return SYS([s],[1])
return s
class SYS(object):
def __init__(self,num,den):
self.num=num
self.den=den
def feedback(self):#1
return self / (self + 1)
def __mul__(self,s):
s=as_sys(s)
"""
np.polymul()
Find the product of two polynomials.
Finds the polynomial resulting from the multiplication of the two input polynomials.
Each input must be either a poly1d object or a 1D sequence of polynomial coefficients, from highest to lowest degree.
"""
num=np.polymul(self.num,s.num)
den = np.polymul(self.den,s.den)
return SYS(num,den)
def __add__(self,s):
s=as_sys(s)
den=np.polymul(self.den,s.den)
num=np.polyadd(np.polymul(self.num,s.den),
np.polymul(s.num,self.den))
return SYS(num,den)
def __sadd__(self,s):
return self+s
def __div__(self,s):
s=as_sys(s)
return self*SYS(s.den,s.num)
def __iter__(self):#2
return iter((self.num,self.den))
当我尝试运行此代码时:
M,b,k=1.0,10,20
plant=SYS([1],[M,b,k])#1
pi_settings=[(10,1e-10),(200,1e-10),
(200,100),(50,100)]
fig,ax=pl.subplots(figsize=(8,3))
for pi_setting in pi_settings:
pi_ctrl=SYS(pi_setting,[1,1e-6])#2
feedback=(pi_ctrl*plant).feedback()#3
_,x=signal.step(feedback,T=t)
label="$K_p={:d},K_i={:3.0f}$".format(*pi_setting)
ax.plot(t,x,label=label)
ax.legend(loc="best",ncol=2)
ax.set_xlabel(u'time(s)')
ax.set_ylabel(u'position(m)')
我收到如下错误:
TypeError Traceback (most recent call last)
<ipython-input-318-6657660ad435> in <module>()
9 for pi_setting in pi_settings:
10 pi_ctrl=SYS(pi_setting,[1,1e-6])#2
---> 11 feedback=(pi_ctrl*plant).feedback()#3
12
13 _,x=signal.step(feedback,T=t)
<ipython-input-317-a24ff95d8d45> in feedback(self)
12
13 def feedback(self):#1
---> 14 return self / (self + 1)
15
16 def __mul__(self,s):
TypeError: unsupported operand type(s) for /: 'SYS' and 'SYS'
我使用的系统是Window 7 + Python 3.5 + Anaconda + Jupyter笔记本。
请帮我弄清楚我的代码有什么问题。谢谢。