The ESP-IDF project, docs, tools, tests and web UI move from v2/ to the root, and the Makefile, README, build scripts and test runner drop the v2/ prefix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
98 lines
3.7 KiB
Python
Executable File
98 lines
3.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Beat latency from a logic analyzer capture.
|
|
|
|
Reads a CSV with a time column and two digital channels: the controller beat
|
|
marker (CONFIG_BEAT_TEST_GPIO, toggles when the first copy of a beat goes out)
|
|
and the driver beat marker (`beat_pin`, toggles when the driver fires). For
|
|
every controller edge it finds the next driver edge and reports the delay minus
|
|
the deliberate lead (CONFIG_BEAT_LEAD_MS, default 30 ms).
|
|
|
|
Works with Saleae ("Time [s],Channel 0,Channel 1") and sigrok CSV exports, or
|
|
any CSV whose first column is time and whose other columns are 0/1 levels.
|
|
|
|
analyze_edges.py capture.csv --ctrl 0 --drv 1
|
|
"""
|
|
import argparse
|
|
import csv
|
|
import statistics
|
|
import sys
|
|
|
|
|
|
def read(path, time_scale):
|
|
times, rows = [], []
|
|
with open(path, newline="") as f:
|
|
for row in csv.reader(f):
|
|
if not row or row[0].lstrip().startswith((";", "#")):
|
|
continue
|
|
try:
|
|
t = float(row[0]) * time_scale
|
|
levels = [int(float(c)) for c in row[1:]]
|
|
except ValueError:
|
|
continue # header line
|
|
times.append(t)
|
|
rows.append(levels)
|
|
return times, rows
|
|
|
|
|
|
def edges(times, rows, ch):
|
|
out, prev = [], None
|
|
for t, r in zip(times, rows):
|
|
v = r[ch]
|
|
if prev is not None and v != prev:
|
|
out.append(t)
|
|
prev = v
|
|
return out
|
|
|
|
|
|
def analyze(ctrl, drv, lead_s, window_s):
|
|
deltas, missed, j = [], 0, 0
|
|
for i, t in enumerate(ctrl):
|
|
nxt = ctrl[i + 1] if i + 1 < len(ctrl) else float("inf")
|
|
while j < len(drv) and drv[j] < t:
|
|
j += 1
|
|
if j < len(drv) and drv[j] - t <= min(window_s, nxt - t):
|
|
deltas.append(drv[j] - t - lead_s)
|
|
j += 1
|
|
else:
|
|
missed += 1
|
|
return deltas, missed
|
|
|
|
|
|
def pct(sorted_vals, p):
|
|
if not sorted_vals:
|
|
return float("nan")
|
|
k = min(len(sorted_vals) - 1, int(round(p / 100 * (len(sorted_vals) - 1))))
|
|
return sorted_vals[k]
|
|
|
|
|
|
def main(argv=None):
|
|
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
ap.add_argument("csv")
|
|
ap.add_argument("--ctrl", type=int, default=0, help="column index of the controller marker (0 = first channel)")
|
|
ap.add_argument("--drv", type=int, default=1, help="column index of the driver marker")
|
|
ap.add_argument("--lead-ms", type=float, default=30.0, help="deliberate lead to subtract (CONFIG_BEAT_LEAD_MS)")
|
|
ap.add_argument("--window-ms", type=float, default=200.0, help="longest delay still counted as the same beat")
|
|
ap.add_argument("--time-unit", choices=("s", "ms", "us", "ns"), default="s", help="unit of the time column")
|
|
a = ap.parse_args(argv)
|
|
|
|
scale = {"s": 1.0, "ms": 1e-3, "us": 1e-6, "ns": 1e-9}[a.time_unit]
|
|
times, rows = read(a.csv, scale)
|
|
if not times:
|
|
sys.exit("no samples read")
|
|
ctrl, drv = edges(times, rows, a.ctrl), edges(times, rows, a.drv)
|
|
deltas, missed = analyze(ctrl, drv, a.lead_ms / 1000, a.window_ms / 1000)
|
|
ms = sorted(d * 1000 for d in deltas)
|
|
print(f"controller edges: {len(ctrl)} driver edges: {len(drv)} matched: {len(ms)} unmatched: {missed}")
|
|
if not ms:
|
|
sys.exit("no matched edges: check channel numbers and --time-unit")
|
|
print(f"delay beyond the {a.lead_ms:g} ms lead (ms): min {ms[0]:.2f} mean {statistics.mean(ms):.2f} "
|
|
f"median {pct(ms, 50):.2f} p95 {pct(ms, 95):.2f} max {ms[-1]:.2f}")
|
|
if len(ms) > 1:
|
|
print(f"jitter (std dev): {statistics.stdev(ms):.2f} ms")
|
|
if a.lead_ms and ms[0] < -1:
|
|
print("note: negative delays mean the lead is larger than the real one; check --lead-ms", file=sys.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|