简体   繁体   English

scipy.optimize高频正弦函数

[英]scipy.optimize on high frequency sine function

I am using Python 2.7. 我正在使用Python 2.7。 I am wondering why the optimize function of SciPy doesn't converge to the right function when the target is a high frequency sinus wave. 我想知道为什么当目标是高频正弦波时,SciPy的优化功能不能收敛到正确的功能。

import numpy as np
from scipy import optimize

test_func = lambda x: 5*np.sin(15*x+3)+1
t = linspace(0,25,100000)
y_t = test_func(t)
plot(t,y_t)

fitfunc = lambda p, x: p[0]*np.sin(p[1]*x+p[2])+p[3]
errfunc = lambda p, x, y: fitfunc(p, x) - y 

p0 = [max(y_t),10,2,0]
p1, success = optimize.leastsq(errfunc, p0, args=(t,y_t))


plot(t,fitfunc(p1,t))

One can clearly see that the end solution diverges from the target clearly. 可以清楚地看到最终解决方案明显偏离了目标。 Am I doing something wrong ? 难道我做错了什么 ? Is the error function ill adapted here ? 错误功能不适用于此处吗?

Thanks for any input 感谢您的输入

Your problem is that there are a large number of local minima in your residuals function as the phase and frequency shift with respect to their true values; 您的问题是,残差函数中存在大量局部最小值,这是相对于其真实值的相移和频移。 without really good initial guesses for the phase and frequency you will converge into one instead of falling into the much deeper, global minimum: 如果没有关于相位和频率的很好的初步猜测,您将收敛于一个而不是陷入更深远的全局最小值:

If you don't have any more information about the phase and frequency, you can either estimate them from a FFT of the data or rewrite your formula as 如果您没有关于相位和频率的更多信息,则可以根据数据的FFT估算它们,也可以将公式重写为

Asin(bx + phi) + d = Acos(phi)sin(bx) + Asin(phi)cos(bx) + d Asin(bx + phi)+ d = Acos(phi)sin(bx)+ Asin(phi)cos(bx)+ d

which has only one nonlinear parameter (b): you can use a grid-search for b and much faster and more reliable linear least-squares fitting for the rest (a1 = Acos(phi), a2 = Asin(phi) and d). 它只有一个非线性参数(b):您可以对b使用网格搜索,对其余部分使用更快,更可靠的线性最小二乘拟合(a1 = Acos(phi),a2 = Asin(phi)和d) 。

Here's a plot of the rms residual as the frequency, b varies, showing the various minima: 这是随着频率b变化而产生的均方根残值图,显示了各种最小值:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM