How to bias an absolute sine wave in Python - python

I am using the below python code so as to bias an absolute sine wave. I would like to have only the crest part of the wave and not the trough part even after positive biasing.Here I am unable achieve continuous crest signal after positive biasing. Can any one help me in this?
Usage: Keeping the input signals above the threshold even during dynamic shift of threshold.
import matplotlib.pyplot as plt
import numpy as np
Bias=5;
x=np.linspace(-20,20,1000);
y=np.abs(np.sin(x)+Bias);
#Bias=np.zeros_like(x); # This is not working
y[(y<=Bias)]= Bias + y # This is not working
plt.plot(x,y)
plt.grid()
plt.show()

It is a litle bit unclear what you are asking... Maybe you want to try this:
import matplotlib.pyplot as plt
import numpy as np
Bias=5;
x = np.linspace(-20, 20, 1000);
y = np.abs(np.sin(x))
y = y + Bias
plt.plot(x, y)
plt.grid()
plt.show()
or this:
import matplotlib.pyplot as plt
import numpy as np
Bias=5;
x=np.linspace(-20,20,1000);
y=np.abs(np.sin(x) + Bias);
y[(y<=Bias)]= Bias
plt.plot(x,y)
plt.grid()
plt.show()

Related

Fourier series animation effect using by python ArtistAnimation

I'm studying about Fourier Series with python.
I drew it with cosine and sine function.
My code is like this.
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
x = Symbol('x')
fx=0
j=10
for i in range(1,j):
fx=fx+(2)/(np.pi*i)*(1-cos(i*np.pi))*(sin(i*x))
y_func=lambdify(x, fx, "numpy")
x_val=np.linspace(-np.pi,np.pi,315)
y_val=y_func(x_val)
plt.plot(x_val,y_val)
plt.show()
I could get the correct graph.
And I tried to make the graph into animaion effect like this gif file.
enter image description here
I wrote the code like below using by ArtistAnimation , but I couldn't get the animation.
How can i get the animation?
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
from matplotlib.animation import ArtistAnimation
x = Symbol('x')
fx=0
j=10
img=[]
fig, ax=plt.subplots(constrained_layout=True)
for i in range(1,j):
fx=fx+(2)/(np.pi*i)*(1-cos(i*np.pi))*(sin(i*x))
y_func=lambdify(x, fx, "numpy")
x_val=np.linspace(-np.pi,np.pi,315)
y_val=y_func(x_val)
fs=plt.plot(x_val,y_val)
img.append([fs])
anim=ArtistAnimation(fig,img,interval=5)
anim.save("Fourier_Series01.gif",fps=24)
I tried to make the code with ArtistAnimation
Thank you for your answer

Curve fitting using python

I am trying to fit a curve for a set of points using numpy and scipy libraries but am getting a closed curve as shown below.
Could anyone let me know how to fit a curve without closing curve?
The code I followed is:
import numpy as np
from scipy.interpolate import splprep, splev
import matplotlib.pyplot as plt
coords = np.array([(3,8),(3,9),(4,10),(5,11),(6,11), (7,13), (9,13),(10,14),(11,14),(12,14),(14,16),(16,17),(17,18),(18,18),(19,18), (20,19),
(21,19),(22,20),(23,20),(24,21),(26,21),(27,21),(28,21),(30,21),(32,20),(33,20),(32,17),(33,16),(33,15),(34,12), (34,10),(33,10),
(33,9),(33,8),(33,6),(34,6),(34,5)])
tck, u = splprep(coords.T, u=None, s=0.0, per=1)
u_new = np.linspace(u.min(), u.max(), 1000)
x_new, y_new = splev(u_new, tck, der=0)
plt.plot(coords[:,1], coords[:,0], 'ro')
plt.plot(y_new, x_new, 'b--')
plt.show()
Output:
I need output without joining the 1st and last point.
Thank you.
Just set per parameter to 0 in scipy.interpolate.splprep:
tck, u = splprep(coords.T, u=None, s=0.0, per=0)

Why Error on the fitting functions is too large also curve seems to pass from maximum number of points. How to reduce that error?

I want to use power law to fit on my data points because I have to calculate the value of v. But the error on my fitting parameters is too large, although curve seems to pass all data points. How to reduce is error?
`import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import math
import scipy
from scipy import optimize
x_data= np.array([30, 45, 60, 75])
y_data= np.array([0.42597867, 0.26249343, 0.19167837, 0.08116507])
fig = plt.figure()
ax= fig.add_subplot(111)
def ff(L,v,c):
return (L**(-1/v)+c)
ax2.scatter(x_data, y_data, marker='s',s=4**2,)
pfit,pcov = optimize.curve_fit(ff,x_data,y_data)
print("pfit: ",pfit)
print("pcov: ",pcov.shape)
#print(pcov)
perr = np.sqrt(np.diag(pcov))
x=np.linspace(20,85,1000)
ax2.plot(x,ff(x,*pfit),color='red')`

Tangent to curve interpolated from discrete data

I was wondering if there's a way to find tangents to curve from discrete data.
For example:
x = np.linespace(-100,100,100001)
y = sin(x)
so here x values are integers, but what if we want to find tangent at something like x = 67.875?
I've been trying to figure out if numpy.interp would work, but so far no luck.
I also found a couple of similar examples, such as this one, but haven't been able to apply the techniques to my case :(
I'm new to Python and don't entirely know how everything works yet, so any help would be appreciated...
this is what I get:
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-100,100,10000)
y = np.sin(x)
tck, u = interpolate.splprep([y])
ti = np.linspace(-100,100,10000)
dydx = interpolate.splev(ti,tck,der=1)
plt.plot(x,y)
plt.plot(ti,dydx[0])
plt.show()
There is a comment in this answer, which tells you that there is a difference between splrep and splprep. For the 1D case you have here, splrep is completely sufficient.
You may also want to limit your curve a but to be able to see the oscilations.
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-15,15,1000)
y = np.sin(x)
tck = interpolate.splrep(x,y)
dydx = interpolate.splev(x,tck,der=1)
plt.plot(x,y)
plt.plot(x,dydx, label="derivative")
plt.legend()
plt.show()
While this is how the code above would be made runnable, it does not provide a tangent. For the tangent you only need the derivative at a single point. However you need to have the equation of a tangent somewhere and actually use it; so this is more a math question.
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-15,15,1000)
y = np.sin(x)
tck = interpolate.splrep(x,y)
x0 = 7.3
y0 = interpolate.splev(x0,tck)
dydx = interpolate.splev(x0,tck,der=1)
tngnt = lambda x: dydx*x + (y0-dydx*x0)
plt.plot(x,y)
plt.plot(x0,y0, "or")
plt.plot(x,tngnt(x), label="tangent")
plt.legend()
plt.show()
It should be noted that you do not need to use splines at all if the points you have are dense enough. In that case obtaining the derivative is just taking the differences between the nearest points.
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-15,15,1000)
y = np.sin(x)
x0 = 7.3
i0 = np.argmin(np.abs(x-x0))
x1 = x[i0:i0+2]
y1 = y[i0:i0+2]
dydx, = np.diff(y1)/np.diff(x1)
tngnt = lambda x: dydx*x + (y1[0]-dydx*x1[0])
plt.plot(x,y)
plt.plot(x1[0],y1[0], "or")
plt.plot(x,tngnt(x), label="tangent")
plt.legend()
plt.show()
The result will be visually identical to the one above.

How to locate the median in a (seaborn) KDE plot?

I am trying to do a Kernel Density Estimation (KDE) plot with seaborn and locate the median. The code looks something like this:
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
sns.set_palette("hls", 1)
data = np.random.randn(30)
sns.kdeplot(data, shade=True)
# x_median, y_median = magic_function()
# plt.vlines(x_median, 0, y_median)
plt.show()
As you can see I need a magic_function() to fetch the median x and y values from the kdeplot. Then I would like to plot them with e.g. vlines. However, I can't figure out how to do that. The result should look something like this (obviously the black median bar is wrong here):
I guess my question is not strictly related to seaborn and also applies to other kinds of matplotlib plots. Any ideas are greatly appreciated.
You need to:
Extract the data of the kde line
Integrate it to calculate the cumulative distribution function (CDF)
Find the value that makes CDF equal 1/2, that is the median
import numpy as np
import scipy
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_palette("hls", 1)
data = np.random.randn(30)
p=sns.kdeplot(data, shade=True)
x,y = p.get_lines()[0].get_data()
#care with the order, it is first y
#initial fills a 0 so the result has same length than x
cdf = scipy.integrate.cumtrapz(y, x, initial=0)
nearest_05 = np.abs(cdf-0.5).argmin()
x_median = x[nearest_05]
y_median = y[nearest_05]
plt.vlines(x_median, 0, y_median)
plt.show()

Categories

Resources