1

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. 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.

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()

2 Answers 2

1

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()

enter image description here

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

Sign up to request clarification or add additional context in comments.

4 Comments

do you have an algorithm, describing how should it be calculated?
The output must be zero for certain x values in addition being symmetric about the x axis.The output is similar to that of the dead zone block used in Simulink.
:Please consider the upper limit and the lower limit as the thresholds.The y value needs to exceed the threshold value so that the output value appears .When the y value is below the threshold ,the output appears as zero on x axis.The x values have no control over the dead band of the output value.The dead band is controlled by the value of upper limit and the lower limit.
Ausgezeichnet.This is exactly what I was looking for.
0

Instead of having an if statement acting on your array, you need to iterate through your array of y values and modify each one. Also, with an if statement of y > 0 or y < 0 will not achieve the deadband result you are looking for. Instead, you should use y > UpperLimit or 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()

2 Comments

please do not iterate through numpy arrays, pandas data frames/series, other vectorized data unless it's absolutely necessary ...
OK. Thank you for the tip. I didn't realize it can be done the way you did it. What is it called when you aren't iterating over the array? When you do this: y[y>something] = stuff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.