![]() ![]() This does not work very well (can lead to extremely poor fits). Note: as a later edit, frequency fitting has also been added. In case of fitting a sin function, the 3 parameters to fit are the offset ('a'), amplitude ('b') and the phase ('c').Īs long as you provide a reasonable first guess of the parameters, the optimization should converge well.Fortunately for a sine function, first estimates of 2 of these are easy: the offset can be estimated by taking the mean of the data and the amplitude via the RMS (3*standard deviation/sqrt(2)). You can use the least-square optimization function in scipy to fit any arbitrary function to another. Plt.plot(tt, yynoise, "ok", label="y with noise") ![]() Print( "Amplitude=%(amp)s, Angular freq.=%(omega)s, phase=%(phase)s, offset=%(offset)s, Max. Yy = amp*numpy.sin(omega*tt + phase) + offset #N, amp, omega, phase, offset, noise = 50, 1. N, amp, omega, phase, offset, noise = 500, 1., 2. ![]() The fitting result is almost perfect assuming there is only one dominant frequency (other than the zero frequency peak). The initial frequency guess is given by the peak frequency in the frequency domain using FFT. Popt, pcov = _fit(sinfunc, tt, yy, p0=guess)įitfunc = lambda t: A * numpy.sin(w*t + p) + c ![]() Guess = numpy.array()ĭef sinfunc(t, A, w, p, c): return A * numpy.sin(w*t + p) + c Guess_freq = abs(ff)+1]) # excluding the zero frequency "peak", which is related to offset '''Fit sin to the input time sequence, and return fitting parameters "amp", "omega", "phase", "offset", "freq", "period" and "fitfunc"'''įf = (len(tt), (tt-tt)) # assume uniform spacing Here is a parameter-free fitting function fit_sin() that does not require manual guess of frequency: import numpy, scipy.optimize ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2023
Categories |