A robot replay tool often needs a command value at a timestamp that was not sampled by the controller. NumPy's `interp` performs one-dimensional piecewise-linear interpolation, but the input contract matters as much as the formula. The sample coordinates must be increasing when a period is not specified, and values outside the sample range use the endpoint values by default. A replay that skips those details can look smooth while hiding a timestamp-order bug or inventing a misleading endpoint command.

This article uses an invented joint-position trace. The values are a teaching fixture, not a measured trajectory or a claim about a particular robot. The objective is to make the sample order, insertion boundaries, interpolation result, and out-of-range policy explicit.

Define a sparse command trace

Use seconds as the coordinate and degrees as the command value:

xp = np.array([0., 1., 3., 4.])

fp = np.array([0., 10., 10., 20.])

There is a one-second ramp from 0 to 10 degrees, a two-second hold at 10 degrees, and a one-second ramp to 20 degrees. Ask for commands at three replay times:

x = np.array([0.5, 2.0, 3.5])

np.interp(x, xp, fp) -> [5., 10., 15.]

The first and last queries are halfway through their surrounding segments. The middle query lies in the hold segment, so the result is exactly 10. The result is a new value on each interval; it is not a nearest-neighbor lookup.

Why monotonicity is a data contract

`numpy.interp` is documented for monotonically increasing sample points. A timestamp stream can become non-monotonic after concatenating logs, sorting by a secondary key, or mixing controller clocks. The function may still return numbers for malformed input, but those numbers no longer have a defensible piecewise-linear interpretation. Validate the time vector before interpolation:

assert np.all(np.diff(xp) > 0)

If duplicate timestamps are possible, define how the duplicate samples are resolved before calling `interp`. Keeping two different commands at the same time is a policy question, not something a plotting curve can decide safely.

Use searchsorted to explain each segment

`numpy.searchsorted` exposes the insertion boundary used to locate a query. For `xp = [0, 1, 3, 4]`, a query at exactly 1 gives index 1 with `side='left'` and index 2 with `side='right'`. The left rule satisfies `a[i-1] < v <= a[i]`; the right rule satisfies `a[i-1] <= v < a[i]`. This distinction is useful when a replay engine needs to log which sample interval owns an exact timestamp.

For the non-boundary query 0.5, `searchsorted(xp, 0.5, side='right')` returns 1, so the enclosing segment is xp[0] to xp[1]. For 2.0 it returns 2, identifying the segment from 1 to 3. For 3.5 it returns 3, identifying the segment from 3 to 4. Store that segment index in a diagnostic trace when a reviewer must reproduce a command.

Out-of-range values are not extrapolated by default

With the same trace, `np.interp([-1., 5.], xp, fp)` returns `[0., 20.]`: values below the first sample use `fp[0]`, and values above the last sample use `fp[-1]`. Those defaults are clamping, not a physical extension of the trajectory. A replay policy may prefer an explicit hold, a fault, or a missing value instead. Pass `left` and `right` deliberately, for example `left=np.nan, right=np.nan`, when an out-of-range query should stop the replay rather than command an endpoint.

Periodic interpolation is a different contract

The `period` parameter normalizes sample coordinates modulo the period and sorts the normalized coordinates internally. Use this option when the independent coordinate is cyclic, such as a query angle in degrees. A time axis stays nonperiodic even when the joint positions on that axis are angles. Choose the contract from the signal definition, then record the period and units alongside the data.

Check the fixture, not only the curve

A replay test should assert the exact values and the endpoint policy:

got = np.interp([0.5, 2.0, 3.5], xp, fp)

assert np.allclose(got, [5., 10., 15.])

assert np.array_equal(np.interp([-1., 5.], xp, fp), [0., 20.])

assert np.searchsorted(xp, [1., 3.], side='left').tolist() == [1, 2]

assert np.searchsorted(xp, [1., 3.], side='right').tolist() == [2, 3]

Also assert the units. The interpolation formula is unit-preserving only when `xp` and `fp` have consistent definitions. Mixing milliseconds with seconds changes the slope by a factor of one thousand while producing plausible-looking numbers.

Keep an audit trail for every replay

Store the sorted sample arrays or their content hash, the query timestamps, `left`, `right`, and `period` arguments, the library version, and the segment index returned by the boundary check. If the source stream was repaired, retain the pre-repair ordering and the exact repair rule. A later reviewer can then distinguish a genuine command change from a changed timestamp contract.

The practical rule is simple: sort or reject timestamps before interpolation, use `searchsorted` when interval ownership matters, and choose an explicit out-of-range action. NumPy's defaults are deterministic and useful for a quick chart, but a robot replay needs a declared policy before a value becomes a command. Treat monotonicity, endpoint handling, and periodicity as part of the motion data schema.

One more operational check is to compare the query horizon with the source horizon before a replay begins. If the requested interval extends beyond the last sample by several seconds, silently clamping every command to the final value can make a stalled stream look like a controlled hold. A replay report can count in-range and out-of-range queries, show the first and last sample times, and fail closed when the out-of-range count exceeds a declared limit. That policy is independent of the interpolation math and should be tested with the same fixture.

For multiple joints, apply the same timestamp vector to each column only after verifying that every column shares the same sampling contract. If one joint has a missing sample, interpolating the full matrix can hide that asymmetry. Keep a per-joint validity mask and carry it through the replay result. The small one-dimensional example scales safely when the metadata travels with the values rather than being inferred from a smooth-looking plot.

Sources

[1] NumPy, numpy.interp: https://numpy.org/doc/stable/reference/generated/numpy.interp.html

[2] NumPy, numpy.searchsorted: https://numpy.org/doc/stable/reference/generated/numpy.searchsorted.html