[英]How to plot a deadband for a simple sine wave using python
I am using the below codes so as to plot the dead band for a sine wave so that the dead band appears on the x axis as y=0.我正在使用下面的代码来绘制正弦波的死区,以便死区出现在 x 轴上为 y=0。 The output is minimized by the value of upper limit[y-0.5] and the lower limit.The dead band needs to be displayed here.Could any one help me in this.
输出通过上限[y-0.5]和下限的值最小化。这里需要显示死区。任何人都可以帮我解决这个问题。
import matplotlib.pyplot as plt
import numpy as np
UpperLimit = 0.5;
LowerLimit = -0.5;
x=np.linspace(-20,20,100);
y=np.sin(x);
if y < 0:
y=np.sin(x)-LowerLimit
if y > 0:
y=np.sin(x)-UpperLimit
else:
y=0
plt.plot(x,y)
plt.grid()
plt.show()
I guess you should work with vectorized values, so try this我想你应该使用矢量化值,所以试试这个
import matplotlib.pyplot as plt
import numpy as np
LL = -0.5
UL = 0.5
x=np.linspace(-20,20,100)
y=np.sin(x)
# plot original sine
plt.plot(x,y)
# zero output value for the dead zone
y[(y>=LL) & (y<=UL)] = 0
y[y>UL] -= UL
y[y<LL] -= LL
# plot "simulinked" ....
plt.plot(x,y)
plt.grid()
plt.show()
PS it would much easier to understand what you want if you would provide a link to the "Dead Zone" algorithm , because not all of us aware of it PS,如果您提供指向“死区”算法的链接,则更容易理解您想要什么,因为并非所有人都知道这一点
Instead of having an if
statement acting on your array, you need to iterate through your array of y
values and modify each one .您需要遍历
y
值数组并修改每个值,而不是让if
语句作用于您的数组。 Also, with an if
statement of y > 0
or y < 0
will not achieve the deadband result you are looking for.此外,使用
y > 0
或y < 0
的if
语句将无法实现您正在寻找的死区结果。 Instead, you should use y > UpperLimit
or y < LowerLimit
相反,您应该使用
y > UpperLimit
或y < LowerLimit
This should work:这应该有效:
import matplotlib.pyplot as plt
import numpy as np
UpperLimit = 0.5;
LowerLimit = -0.5;
x=np.linspace(-20,20,100);
y=np.sin(x);
for y_point in np.nditer(y, op_flags=['readwrite']):
if y_point > UpperLimit:
y_point[...] -= UpperLimit
elif y_point < LowerLimit:
y_point[...] -= LowerLimit
else:
y_point[...] = 0
plt.plot(x,y)
plt.grid()
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.