简体   繁体   English

拟合傅立叶函数时,scipy curve_fit无法生成平滑图形

[英]scipy curve_fit not producing smooth graph when fitting fourier function

I'm getting a strange result when using scipy curve_fit when fitting my data to this function. 在将数据拟合到此函数时使用scipy curve_fit时,我得到一个奇怪的结果。

def func(t, freq, offset, a0, b0, a1, b1, a2, b2, a3, b3):
    return (
        + a0*np.sin(2.*0*np.pi*freq*t)
        + b0*np.cos(2.*0*np.pi*freq*t)    
        + a1*np.sin(2.*1*np.pi*freq*t)
        + b1*np.cos(2.*1*np.pi*freq*t)    
        + a2*np.sin(2.*2*np.pi*freq*t)
        + b2*np.cos(2.*2*np.pi*freq*t)    
        + a3*np.sin(2.*3*np.pi*freq*t)
        + b3*np.cos(2.*3*np.pi*freq*t)
        + offset)

This is the result of the fit plotted 这是绘制拟合的结果 凹凸曲线拟合线

The fit itself is okay, the only problem being is that the line is bumpy. 贴合性本身还可以,唯一的问题是线条凹凸不平。 Since I am only fitting sines and cosines with a constant, how could this be happening? 由于我只用一个常数拟合正弦和余弦,这怎么可能发生? Is this happening in matplotlib or in the curve_fitting function? 这是发生在matplotlib还是curve_fitting函数中? Another thing is that depending on whether I add more or less terms to the function, the function will either smooth itself out, or get bumpy again. 另一件事是,取决于我是否向函数中添加更多或更少的术语,该函数将使自身变平滑,或者再次变得颠簸。

You may leave the a0 term out, as this is constant zero. 您可以省略a0项,因为它是恒定的零。 You may also leave b0 out as this is the same as offset. 您也可以省略b0 ,因为它与offset相同。 This is not the cause of the bumps, but removes two redundant fitting parameters. 这不是造成颠簸的原因,而是删除了两个冗余的拟合参数。

def func(t, freq, offset, a1, b1, a2, b2, a3, b3):
    return (   
        + a1*np.sin(2.*1*np.pi*freq*t)
        + b1*np.cos(2.*1*np.pi*freq*t)    
        + a2*np.sin(2.*2*np.pi*freq*t)
        + b2*np.cos(2.*2*np.pi*freq*t)    
        + a3*np.sin(2.*3*np.pi*freq*t)
        + b3*np.cos(2.*3*np.pi*freq*t)
        + offset)

Apart from that the result is expected. 除此之外,预期结果。 The more frequencies you allow, the more frequencies you have in your plot. 允许的频率越多,曲线图中的频率就越多。 The bumps are the sin or cos functions with low amplitude and high frequency. 凹凸是具有低幅度和高频的sin或cos函数。 Using more datapoints on the x-axis when plotting the fit curve will allow you to observe that the bumps are smooth like sin functions as well. 绘制拟合曲线时,在x轴上使用更多数据点将使您观察到凹凸像sin函数一样平滑。

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

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