haa*_*oll 1 python scipy dataframe pandas
我有随时间变化的加速度值的表格数据,如下例所示:
time(s) acc_x acc_y
0.1 0 0
0.2 -0.98 1.66
0.3 1.42 1.72
0.4 -1.98 -0.3
0.5 -0.3 -0.79
0.6 -1.15 1.65
0.7 1.2 -0.5
0.8 1.97 0.51
0.9 -0.74 -0.39
1 -0.47 -1.06
1.1 1.77 0.87
1.2 -0.35 -0.67
1.3 1.4 0.51
1.4 1.72 1.47
1.5 -0.37 -0.83
1.6 1.65 -0.07
1.7 1.51 -0.53
1.8 -0.46 -0.8
1.9 -0.35 -0.18
2 0 0
Run Code Online (Sandbox Code Playgroud)
由此,我想通过双重积分来计算位置值,以使用位置坐标作为搅拌器中的关键帧位置。因为我并不总是知道输入数据的时基,所以我想将其重新采样为帧间时间间隔。
这是我到目前为止所尝试的,主要是尝试调整此代码示例:Rollingintegrationoverpandasdataframewithtimeindex
import pandas as pd
from scipy import integrate
cur_fps=25 #bpy.context.scene.render.fps
acc_table=pd.read_excel("C:/Temp/exampleaccelerations.xlsx",index_col=0) #read the table from disk
timedeltastrings={"interp":"%d"%(timedelta_base/100)+"us","vel":"%d"%(timedelta_base/10)+"us","pos":"%d"%(timedelta_base)+"us"}
acc_table_interp=acc_table.resample(timedeltastrings["interp"]).interpolate(method="linear")
vel_table=acc_table_interp.rolling(timedeltastrings["vel"]).apply(integrate.trapz)
vel_table_interp=vel_table.resample(timedeltastrings["vel"]).interpolate(method="linear")
pos_table=vel_table.rolling(timedeltastrings["pos"]).apply(integrate.trapz)
pos_table_interp=pos_table.resample(timedeltastrings["pos"]).interpolate(method="linear")
Run Code Online (Sandbox Code Playgroud)
代码可能不是特别整洁,但可以工作并给出结果。然而,与手动评估(例如在 Excel 中)相比,结果值太高。我完全不知道如何在结果和输入之间建立心理联系。
如果您想知道,重采样应该为滚动积分器提供一些可以使用的值。如果没有重采样和 100 毫秒的窗口大小(类似于我对上面链接的答案的理解),集成的结果是全零数据帧。
有人可以指出如何正确使用 scipy 积分器(或任何其他等效函数)的方向,以便我可以获得正确的结果吗?
您可以用于scipy.integrate.cumtrapz数值积分。
假设您的数据存储在pd.DataFrame df
from scipy.integrate import cumtrapz
velocities = cumtrapz(df[['acc_x','acc_y']], df['time(s)'], axis=0)
positions = cumtrapz(velocities, dx=0.1, axis=0)
Run Code Online (Sandbox Code Playgroud)
要解释积分的结果,您可以绘制position,velocity和acceleration
import matplotlib.pyplot as plt
plt.figure(figsize=(8,8))
plt.scatter(positions[:,0], positions[:,1], label='Position')
plt.quiver(
positions[:,0], positions[:,1],
velocities[1:,0], velocities[1:,1], color='blue',
width=0.003, angles='xy', label='Velocity')
plt.quiver(
positions[:,0], positions[:,1],
df['acc_x'].iloc[2:], df['acc_y'].iloc[2:], color='green',
width=0.003, angles='xy', label='Acceleration')
plt.legend();
Run Code Online (Sandbox Code Playgroud)
出去:
| 归档时间: |
|
| 查看次数: |
1087 次 |
| 最近记录: |