CoolFace
Apppublic

mangrovedigital/tide-engine-api

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
align.py31 linesDownload Raw Back to engine
1"""Time alignment: interpolate a Series onto target timestamps without
2fabricating data across real gaps.
3"""
4
5import bisect
6
7
8def interp(series_t, series_v, target_t, max_gap_s):
9    """Linear-interpolate (series_t, series_v) onto target_t.
10
11    Returns list parallel to target_t; entry is None when the nearest bracketing
12    samples are farther than max_gap_s apart (a real gap -> no fabrication).
13    """
14    out = []
15    n = len(series_t)
16    for tt in target_t:
17        i = bisect.bisect_left(series_t, tt)
18        if i < n and series_t[i] == tt:
19            out.append(series_v[i])
20            continue
21        if i == 0 or i >= n:
22            out.append(None)  # outside coverage
23            continue
24        t0, t1 = series_t[i - 1], series_t[i]
25        if (t1 - t0) > max_gap_s:
26            out.append(None)
27            continue
28        f = (tt - t0) / (t1 - t0)
29        out.append(series_v[i - 1] + f * (series_v[i] - series_v[i - 1]))
30    return out
31