'Trying to fit data into sine cosine curve fit using scipy

I am new to signal processing in python. Here I am trying fit data to sine cosine curve using the equation - A * np.sin(x) + B * np.cos(x) + C.

here is the snippet of the code

def func(x, A, B, C):
    return A * np.sin(x) + B * np.cos(x) + C

p0 = 0, 25, 10
popt, pcov = curve_fit(func, x, y)

times = np.linspace(x[0], x[-1], num=21)
plt.plot(x, y, 'o', color='red', label="data")
plt.plot(times, func(times, *popt), '--', color='blue', label="optimized data")
# plt.plot(x, func(x, *popt), '--', color='blue', label="optimized data")
plt.legend()
plt.show()

I am getting below output (in image)

enter image description here

Could anyone help me spotting the mistake or any suggestion with the code



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source