Last active
July 31, 2023 15:33
-
-
Save winstxnhdw/f5ced25b0563cfa2e30b9c534932e829 to your computer and use it in GitHub Desktop.
Linear interpolation of a 2D line with SciPy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy import arange, array, concatenate, cumsum, ediff1d, float64, hypot | |
from numpy.typing import NDArray | |
from scipy.interpolate import interp1d | |
def linear_interpolation( | |
x: NDArray[float64], | |
y: NDArray[float64], | |
linear_displacement: float | |
) -> tuple[NDArray[float64], NDArray[float64]]: | |
""" | |
Summary | |
------- | |
interpolate a line with a desired linear displacement between each point | |
Parameters | |
---------- | |
x (NDArray[float64]): x-coordinates of the coarse line [m] | |
y (NDArray[float64]): y-coordinates of the coarse line [m] | |
linear_displacement (float): desired linear displacement between each point [m] | |
Returns | |
------- | |
interpolated_x (NDArray[float64]): x-coordinates of the interpolated line [m] | |
interpolated_y (NDArray[float64]): y-coordinates of the interpolated line [m] | |
""" | |
distance = concatenate(([0], cumsum(hypot(ediff1d(x), ediff1d(y))))) | |
points = array([x, y]).T | |
interp = interp1d(distance, points, axis=0, kind='linear') | |
s = arange(0, distance[-1], linear_displacement) | |
return interp(s).T # type: ignore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment