简体   繁体   English

在 Python 中通过数值微分计算加速度

[英]Calculating acceleration by numerical differentiation in Python

From stylus movement measurements I got two measurements: the time and the distance.从手写笔移动测量中,我得到了两个测量值:时间和距离。 So I have two arrays, first an array of timestamps (in milliseconds) and then an array of the same size of distance measurements.所以我有两个数组,第一个是时间戳数组(以毫秒为单位),然后是一个相同大小的距离测量数组。 For example the two arrays could look like:例如,这两个数组可能如下所示:

distance = [1.4142,1.0000,1,0,1.0000,1.0000,0,0,1.0000,1.0000,0,1.0000,2.0000,2.2361,0,3.0000,3.6056,3.1623,3.1623,0,3.6056,3.1623,3.1623,0,1.4142,2.2361,1.0000,0,0]

timestamps = [1563203.5,1563208,1563210.5,1563213.5,1563218.5,1563223.5,1563226.5,1563229,1563233.5,1563238.5,1563242.5,1563245,1563248.5,1563253.5,1563258,1563260.5,1563263.5,1563268.5,1563273.5,1563276.5,1563279,1563283.5,1563288.5,1563292.5,1563295,1563298.5,1563303.5,1563307,1563317.5]

I think the first derivative gives me the speed and the second derivative gives me the acceleration.我认为一阶导数给了我速度,二阶导数给了我加速度。

I'm interested in calculating the acceleration using numerical differentiation.我对使用数值微分计算加速度很感兴趣。 How can this be done in python?这怎么能在python中完成?

The first derivative can be calculated as (f(x+h) - f(xh)) / (2h) .一阶导数可以计算为(f(x+h) - f(xh)) / (2h) This gives an estimated error on the order of h^2 .这给出了h^2数量级的估计误差。

The second derivative can be calculated as (f(x+h) - 2f(x) + f(xh)) / h^2 .二阶导数可以计算为(f(x+h) - 2f(x) + f(xh)) / h^2 This also gives an estimated error on the order of h^2 .这也给出了h^2数量级的估计误差。

Note: you can use (f(x+h) - f(x))/h or (f(x) - f(xh))/h to estimate the derivative, but these give estimated errors on the order of h , which is larger than h^2 .注意:您可以使用(f(x+h) - f(x))/h(f(x) - f(xh))/h来估计导数,但这些会给出h量级的估计误差,它大于h^2

I haven't looked at your data in detail, but these formulas don't quite work out if you don't have a constant h interval between your data points.我没有详细查看您的数据,但是如果您的数据点之间没有恒定的h间隔,这些公式就不太适用。 In that case I would simply calculate the acceleration in two steps:在这种情况下,我会简单地分两步计算加速度:

v(x_i) = (p(x_(i+1) - p(x_(i-1))) / (x_(i+1) - x_(i-1))
a(x_i) = (v(v_(i+1) - v(x_(i-1))) / (x_(i+1) - x_(i-1))

You basically just feed in your data for the first derivative back into your derivation algorithm and you get the second derivative.您基本上只需将一阶导数的数据输入到您的推导算法中,然后您就可以得到二阶导数。

Alternatively to the two-stage differentiation scheme described by @SirGuy, assuming locally constant acceleration, you can consider parabolic fits between successive triples of points,作为@SirGuy 描述的两阶段微分方案的替代方案,假设局部加速度恒定,您可以考虑连续三元组点之间的抛物线拟合,

a T0² / 2 + b T0 + c = X0
a T1² / 2 + b T1 + c = X1
a T2² / 2 + b T2 + c = X2

You easily eliminate c by pairwise subtractions, and after simplification您可以通过成对减法轻松消除c ,并在简化后

a (T1 + T0) / 2 + b = (X1 - X0) / (T1 - T0) = V01
a (T2 + T1) / 2 + b = (X2 - X1) / (T2 - T1) = V12

Finally,最后,

a = 2 (V12 - V01) / (T2 - T0).

If the data is noisy, you can also think of taking more points into account and fitting a sliding parabolic model on K points (say 5 or 7), with weights decreasing on either sides of the central point, for smoothing.如果数据有噪声,您还可以考虑考虑更多点并在 K 个点(例如 5 或 7)上拟合滑动抛物线模型,中心点两侧的权重减少,以进行平滑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM