问题
I am using the following code (adapted from Resample or normalize trajectory data so points are evenly spaced) to interpolate 2D X & Y positional data (with no time index) so that the points are evenly spaced. From my understanding, the answer for that question assumed that the x values follow a certain curve or pattern (e.g. exponential curve) but that isn't the case for all my trajectories.
I believe I need to interpolate X and Y separately. However, this code does not seem to produce evenly spaced output and I'm not sure how to solve this. The arrays are already 1000 points before interpolation and vary in 'shape'. Thus I don't know how to define x & x_new:
# Interpolation for X values
from scipy.interpolate import interp1d
y = df['X']
x = np.linspace(y.min(),y.max(),1000)
# define interpolation function:
f = interp1d(x,y)
# create new df with desired x vals, generate y with interp function:
x_new = np.linspace(y.min(),y.max(),1000)
y_new = f(x_new)
X_interp = pd.DataFrame(np.array([y_new]).T, columns=["x_interp"])
# Interpolation for Y values
from scipy.interpolate import interp1d
y = df['Y']
x = np.linspace(y.min(),y.max(),1000)
# define interpolation function:
f = interp1d(x,y)
# create new df with desired x vals, generate y with interp function:
x_new = np.linspace(y.min(),y.max(),1000)
y_new = f(x_new)
Y_interp = pd.DataFrame(np.array([y_new]).T, columns=["y_interp"])
But it doesnt change the data in any way.
As an example, the 2D data (X & Y positions with no time index) for one of the trajectories looks like this when plotted as a scatterplot:
# Note the interpolation doesnt change the data so it looks the same whether I plot using:
plt.scatter(X_interp['x_interp'], Y_interp['y_interp'])
# or the original data
plt.scatter(df['X'], df['Y']))
来源:https://stackoverflow.com/questions/66221994/interpolation-to-evenly-space-trajectory-data-for-different-curves