HEMS
HEMS

Reputation: 307

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

Upvotes: 1

Views: 1053

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210972

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

Upvotes: 1

pjbollinger
pjbollinger

Reputation: 41

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

Upvotes: 0

Related Questions