Spaces:
Sleeping
Sleeping
Create trajectory/fit_trajectory.py
Browse files- trajectory/fit_trajectory.py +22 -0
trajectory/fit_trajectory.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Polynomial fitting & projection to stumps."""
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
from typing import List, Tuple
|
5 |
+
|
6 |
+
|
7 |
+
class TrajectoryFitter:
|
8 |
+
def __init__(self, degree: int = 2):
|
9 |
+
self.deg = degree
|
10 |
+
self.coeffs = None
|
11 |
+
|
12 |
+
def fit(self, points: List[Tuple[float, float]]):
|
13 |
+
"""Fit y = f(x) polynomial to ball path."""
|
14 |
+
pts = np.array(points)
|
15 |
+
x, y = pts[:, 0], pts[:, 1]
|
16 |
+
self.coeffs = np.polyfit(x, y, self.deg)
|
17 |
+
|
18 |
+
def project(self, x_vals: np.ndarray):
|
19 |
+
if self.coeffs is None:
|
20 |
+
raise RuntimeError("Call fit() before project()")
|
21 |
+
poly = np.poly1d(self.coeffs)
|
22 |
+
return poly(x_vals)
|