I am fitting a damped sine curve to my plot and I do not want it to take into account values after a certain range because those values result from noise. I still want to make the plot in the region where noise is present and that is why it is not obvious how I can prevent the fit from taking those points. Otherwise, I could have just restricted my plot in the region without the noise and then make a fit and that would have done it.
Here is my program:
(Note: x0 and y0 are defined in a previous cell)
x0=np.array(x0)
y0=np.array(y0)
def damped_sine(t, A, lbda, omega):
r"""y(t) = A \cdot \exp(-\lambda t) \cdot \left( \sin \left( \omega t + \phi ) \right)"""
return A * np.exp(-lbda * t) * np.cos(omega * t)
popt, pcov = curve_fit(damped_sine,x0,y0)
print(*[f"{val:.2f}+/-{err:.2f}" for val, err in zip(popt, np.sqrt(np.diag(pcov)))])
plt.plot(x0,y0)
plt.plot(x0,damped_sine(x0,*popt),'-r')
OUTPUT:
I want the fitted curve to take into account only points till x-axis value =20 as the small oscillations after that seem to come from the noise.
