I want to make a sinusoidal fit to my data. I got it working for a constant amplitude, see the Figure below and my code:
Besides that, my data is also slightly skewed to the right. Is it possible to encorporate this as well in to fit? See below the code on how far I got.
def Fit_Sine(tt, yy):
tt = np.array(tt)
yy = np.array(yy)
ff = np.fft.fftfreq(len(tt), (tt[1]-tt[0])) # assume uniform spacing
Fyy = abs(np.fft.fft(yy))
guess_freq = abs(ff[np.argmax(Fyy[1:])+1]) # excluding the zero frequency "peak", which is related to offset
guess_amp = np.std(yy) * 2.**0.5
guess_offset = np.mean(yy)
guess_phase = 0.
guess_damping = 1.5
guess = np.array([guess_amp, 2.*np.pi*guess_freq, guess_phase])
def sinfunc(t, A, w, p): return A * np.sin(w*t + p)
popt, pcov = scipy.optimize.curve_fit(sinfunc, tt, yy, p0=guess)
A, w, p = popt
f = w/(2.*np.pi)
fitfunc = lambda t: A * np.sin(w*t + p)
res = {"amp": A, "omega": w, "phase": p, "freq": f,
"period": 1./f, "fitfunc": fitfunc, "maxcov": np.max(pcov), "rawres": (guess,popt,pcov)}
x_fit = np.arange(0, len(tt))
fit = res["fitfunc"](x_fit)
return fit
The reason for this is that I want to smooth out the bump in the top:
Other suggestions for achieving this are also welcome.
Regards,
Dante