您可以 在scipy中使用最小二乘优化函数,以将任意函数拟合到另一个函数。如果拟合
正弦函数,则要拟合的三个参数是偏移量(’a’),幅度(’b’)和相位(’c’)。
只要您对参数进行合理的初步猜测,优化就可以很好地收敛。幸运的是,对于正弦函数,
可以很容易地对其中的2个进行首次估算:可以通过取数据平均值和幅值来估算偏移量。 RMS(3 *标准偏差/ sqrt(2))。
注意:作为以后的编辑,还添加了频率拟合。这不能很好地工作(可能导致极差的拟合)。因此,根据您的
判断,我的建议是除非频率误差小于百分之几,否则不要使用频率拟合。
这将导致以下代码:
import numpy as npfrom scipy.optimize import leastsqimport pylab as pltN = 1000 # number of data pointst = np.linspace(0, 4*np.pi, N)f = 1.15247 # Optional!! Advised not to usedata = 3.0*np.sin(f*t+0.001) + 0.5 + np.random.randn(N) # create artificial data with noiseguess_mean = np.mean(data)guess_std = 3*np.std(data)/(2**0.5)/(2**0.5)guess_phase = 0guess_freq = 1guess_amp = 1# we'll use this to plot our first estimate. This might already be good enough for youdata_first_guess = guess_std*np.sin(t+guess_phase) + guess_mean# Define the function to optimize, in this case, we want to minimize the difference# between the actual data and our "guessed" parametersoptimize_func = lambda x: x[0]*np.sin(x[1]*t+x[2]) + x[3] - dataest_amp, est_freq, est_phase, est_mean = leastsq(optimize_func, [guess_amp, guess_freq, guess_phase, guess_mean])[0]# recreate the fitted curve using the optimized parametersdata_fit = est_amp*np.sin(est_freq*t+est_phase) + est_mean# recreate the fitted curve using the optimized parametersfine_t = np.arange(0,max(t),0.1)data_fit=est_amp*np.sin(est_freq*fine_t+est_phase)+est_meanplt.plot(t, data, '.')plt.plot(t, data_first_guess, label='first guess')plt.plot(fine_t, data_fit, label='after fitting')plt.legend()plt.show()
编辑:我假设您知道正弦波中的周期数。如果您不这样做,则安装起来会有些棘手。您可以尝试
通过手动绘制来猜测周期数,并尝试将其优化为第6个参数。



