似乎你的数据不够平滑;我使用pandas来替换x、y、dx、dy、d2x、d2y和curvature,方法是对不同的值窗口大小使用滚动方式。随着窗口大小的增加,曲率看起来越来越像平滑抛物线的效果(图例给出了窗口大小):
作为参考,以下是原始数据的绘图:
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