Using python scipy.interpolate
for 1d interpolation
You should avoid “A value in x_new is above the interpolation”, which means the range of inserted value (x) must smaller or equel to the basic interpolation value, in order to find the corresponding y.
Because my range of data is unstable, sometimes will above the interpolation range, I pop()
the last 2 value of inserted value (x).
from scipy.interpolate import interp1d
# compare the timelength, smaller value timestamp[0] means longer timelength
# shorter interpolate into longer one
if self.__robot1.timestamp[0] < self.__robot2.timestamp[0]:
robot_long = self.__robot1
robot_short = self.__robot2
else:
robot_long = self.__robot2
robot_short = self.__robot1
# pop last 2 timestamp, in order to avoid "A value in x_new is above the interpolation "
robot_short.timestamp.pop()
robot_short.timestamp.pop()
# longer one build interpolation dataset
y_long = np.reshape(robot_long.torque[:,joint], len(robot_long.timestamp))
x_long = np.reshape(robot_long.timestamp,len(robot_long.timestamp))
x_short = np.reshape(robot_short.timestamp,len(robot_short.timestamp))
interpld_func = interp1d(x_long, y_long, kind='quadratic')
y_long = interpld_func(x_short)
y_short = np.reshape(robot_short.torque[:-2,joint], -1)
error = mean_absolute_error(y_short,y_long)
joint_error.append(error)
plt.figure("traj "+str(traj_index)+" joint "+str(joint))
plt.plot(robot_short.timestamp, y_short, label = robot_short.name)
plt.plot(robot_short.timestamp, y_long, label = robot_long.name)
plt.legend(loc = "best")
Reference:
插值interpolate模块