簡體   English   中英

找到曲線上的兩個點/導數,它們之間的線是直線/恆定的

[英]Find two points/derivatives on curves between which the line is straight/constant

我正在繪制 x 和 y 點。 這導致了一條曲線,該線首先彎曲,然后在某個點之后變成直線,一段時間后再次彎曲。 我想檢索這兩個點。 盡管 x 是線性的並且 y 是針對 x 繪制的,但 y 不是線性地依賴於 x。

我嘗試使用 matplotlib 進行繪圖和 numpy 多項式函數,並且目前正在研究樣條曲線,但似乎對於這些y需要直接依賴於x

圖片

您的數據是嘈雜的,因此您不能使用簡單的數值導數。 相反,正如您可能已經發現的那樣,您應該使用樣條曲線擬合它,然后檢查樣條曲線的曲率。

關閉此答案,您可以擬合樣條並計算二階導數(曲率),如下所示:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline

x = file['n']
y = file['Ds/2']

y_spline = UnivariateSpline(x, y)
x_range = np.linspace(x[0], x[-1], 1000)  # or could use x_range = x
y_spline_deriv = y_spl.derivative(n=2)
curvature = y_spline_deriv(x_range)

然后你可以像這樣找到直線區域的起點和終點:

straight_points = np.where(curvature.abs() <= 0.1)[0]  # pick your threshold
start_idx = straight_points[0]
end_idx = straight_points[-1]
start_x = x_range[start_idx]
end_x = x_range[end_idx]

或者,如果您主要對找到曲線最平坦的部分感興趣(如您的圖形所示),您可以嘗試計算一階導數,然后找到斜率在最小斜率范圍內任何地方的一小部分區域數據。 在這種情況下,只需替換上面代碼中的y_spline_deriv = y_spl.derivative(n=1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM