曲集的点

时间:2018-05-28 08:36:31

标签: python curve

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 个答案:

答案 0 :(得分:0)

您的数据似乎不够平稳;我使用pandas通过滚动方式替换x,y,dx,dy,d2x,d2y和曲率,以获得不同的窗口大小。随着窗口尺寸的增加,曲率开始变得越来越像您期望看到的平滑抛物线(图例给出窗口大小):

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