代码之家  ›  专栏  ›  技术社区  ›  newstudent

点集曲率

  •  0
  • newstudent  · 技术社区  · 8 年前

    data file

    import matplotlib.pylab as plt
    import numpy as np
    
    #initial data
    data=np.loadtxt('profile_nonoisebigd02.txt')
    x=data[:,0]
    y=data[:,1]
    

    initial profile

    #first derivatives 
    dx= np.gradient(data[:,0])
    dy = np.gradient(data[:,1])
    
    #second derivatives 
    d2x = np.gradient(dx)
    d2y = np.gradient(dy)
    
    #calculation of curvature from the typical formula
    curvature = np.abs(dx * d2y - d2x * dy) / (dx * dx + dy * dy)**1.5
    

    curvature

    有谁能帮我弄清楚我的曲度哪里出错了吗? 这组点给了我一个抛物线,但曲率不是我所期望的。

    1 回复  |  直到 8 年前
        1
  •  1
  •   hilberts_drinking_problem    8 年前

    似乎你的数据不够平滑;我使用pandas来替换x、y、dx、dy、d2x、d2y和curvature,方法是对不同的值窗口大小使用滚动方式。随着窗口大小的增加,曲率看起来越来越像平滑抛物线的效果(图例给出了窗口大小):

    enter image description here

    作为参考,以下是原始数据的绘图:

    enter image description here

    def get_smooth(smoothing=10, return_df=False):
        data=np.loadtxt('profile_nonoisebigd02.txt')
    
        if return_df:
            return pd.DataFrame(data)
    
        df = pd.DataFrame(data).sort_values(by=0).reset_index(drop=True).rolling(smoothing).mean().dropna()
    
        # first derivatives
        df['dx'] = np.gradient(df[0])
        df['dy'] = np.gradient(df[1])
    
        df['dx'] = df.dx.rolling(smoothing, center=True).mean()
        df['dy'] = df.dy.rolling(smoothing, center=True).mean()
    
        # second derivatives
        df['d2x'] = np.gradient(df.dx)
        df['d2y'] = np.gradient(df.dy)
    
        df['d2x'] = df.d2x.rolling(smoothing, center=True).mean()
        df['d2y'] = df.d2y.rolling(smoothing, center=True).mean()
    
    
        # calculation of curvature from the typical formula
        df['curvature'] = df.eval('abs(dx * d2y - d2x * dy) / (dx * dx + dy * dy) ** 1.5')
        # mask = curvature < 100
    
        df['curvature'] = df.curvature.rolling(smoothing, center=True).mean()
    
        df.dropna(inplace=True)
        return df[0], df.curvature