Interpolation method that does not add unnecessary extremums

后端 未结 3 1667
梦毁少年i
梦毁少年i 2020-12-20 01:50

This question is half programming but also half mathematics. I want to interpolate a set of points by a curve without adding unnecessary extremums staying \"close to the lin

3条回答
  •  清酒与你
    2020-12-20 02:30

    You can use the linear interpolation and then filter it (with a mean filter) :

    size = 51.0;    
    fun = interpolate.interp1d(xp, yp,kind='linear');
    filt = (1/size)*np.ones(size);
    yc = signal.convolve( fun(xc),filt,'same');
    

    With the parameter sizeyou can control the smoothing degree.

    enter image description here

    This is the integrated code:

    import numpy as np
    from scipy.interpolate import interp1d
    import matplotlib.pyplot as plt
    from scipy import interpolate,signal
    
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.spines['left'].set_position('zero')
    ax.spines['right'].set_color('none')
    ax.spines['bottom'].set_position('zero')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    
    list_points=[(-3,0.1),(-2,0.15),(0,4),(2,-6),(4,-2),(7,-0.15),(8,-0.1)]
    (xp,yp)=zip(*list_points)
    xc=np.linspace(min(xp),max(xp),300)
    
    ########################################################
    size = 41.0;#Put here any odd number
    fun = interpolate.interp1d(xp, yp,kind='linear');
    filt = (1/size)*np.ones(size);
    yc = signal.convolve(fun(xc),filt,'same');
    ########################################################
    
    plt.plot(xp,yp,'o',color='black',ms=5)
    plt.plot(xc,yc)
    plt.plot(xc,fun(xc))
    plt.show()
    

提交回复
热议问题