曲線擬合並不總是那麼簡單。算法基於最小二乘曲線擬合,通常需要對輸入參數進行初始猜測。根據你想要的功能種類,你的初步猜測必須是一個好的選擇。
即使您嘗試了初步猜測,我會說您還有一個額外的問題,與您的採樣頻率和波的頻率有關。欲瞭解更多信息,你可以看看維基百科的Nyquist-Shannon sampling theorem。簡單地說,你的波的頻率是1.759 /(2 * pi)= 0.28,這與你的陣列的採樣頻率非常接近(〜0.33)。另一個可能出現的問題是有太多的擺動來適應你的功能。
爲了讓您的代碼正常工作,我會建議您增加波形的頻率(a> 4 * 0.33),或者增加採樣頻率並減少空間向量長度x
。
我跑了以下代碼,得到的結果如圖所示here:
# -*- coding: utf-8 -*-
import numpy as np
import pylab as pl
from scipy.optimize import curve_fit
def mysine(x, a):
return 1. * np.sin(a * x)
a = 1.759 # Wave frequency
x = np.linspace(0, 10, 100) # <== This is what I changed
y = np.sin(a * x) + 0. * np.random.normal(size=len(x))
# Runs curve fitting with initial guess.
popt, pcov = curve_fit(mysine, x, y, p0=[1.5])
# Calculates the fitted curve
yf = mysine(x, *popt)
# Plots results for comparison.
pl.ion()
pl.close('all')
fig = pl.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, '-', c=[0.5, 0.5, 0.5])
ax.plot(x, yf, 'k-', linewidth=2.0)
ax.text(0.97, 0.97, ur'a=%.4f, ã=%.4f' % (a, popt[0]), ha='right', va='top',
fontsize=14, transform=ax.transAxes)
fig.savefig('stow_curve_fit.png')
非常感謝,regeirk - 我非常感謝!乾杯,丹。 –