From 0c423f30abf401d321cbccc0b1595dbb0f4c6596 Mon Sep 17 00:00:00 2001 From: Chris Frankland-Wright Date: Tue, 24 Jun 2025 21:55:26 +0100 Subject: [PATCH] additional diagnostic --- ltc_probe.py | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/ltc_probe.py b/ltc_probe.py index b1c6e77..9dded3c 100644 --- a/ltc_probe.py +++ b/ltc_probe.py @@ -2,8 +2,8 @@ """ ltc_probe.py -Improved LTC-like signal probe — detects pulse duration patterns -consistent with bi-phase mark code used in SMPTE LTC. +Advanced LTC-like signal probe using pulse duration clustering +for reliable short/long classification — works even with imbalanced timecodes. """ import numpy as np @@ -12,38 +12,59 @@ import sounddevice as sd DURATION = 1.0 # seconds SAMPLERATE = 48000 CHANNELS = 1 -MIN_EDGES = 1000 # sanity threshold +MIN_EDGES = 1000 def detect_rising_edges(signal): above_zero = signal > 0 edges = np.where(np.logical_and(~above_zero[:-1], above_zero[1:]))[0] return edges +def cluster_durations(durations): + if len(durations) < 2: + return None, None + + # Use 2-means clustering (basic method) + durations = np.array(durations) + mean1, mean2 = np.min(durations), np.max(durations) + + for _ in range(10): # converge in a few iterations + group1 = durations[np.abs(durations - mean1) < np.abs(durations - mean2)] + group2 = durations[np.abs(durations - mean1) >= np.abs(durations - mean2)] + if len(group1) == 0 or len(group2) == 0: + break + mean1 = np.mean(group1) + mean2 = np.mean(group2) + + short = group1 if mean1 < mean2 else group2 + long = group2 if mean1 < mean2 else group1 + return short, long + def analyze_pulse_durations(edges, samplerate): durations = np.diff(edges) / samplerate if len(durations) == 0: return None - short_pulse_threshold = np.median(durations) * 1.5 - short = durations[durations <= short_pulse_threshold] - long = durations[durations > short_pulse_threshold] + short, long = cluster_durations(durations) + if short is None or long is None: + return None + total = len(durations) return { - "count": len(durations), + "count": total, "avg_width_ms": np.mean(durations) * 1000, "short_pulses": len(short), "long_pulses": len(long), - "short_pct": (len(short) / len(durations)) * 100, - "long_pct": (len(long) / len(durations)) * 100 + "short_pct": (len(short) / total) * 100, + "long_pct": (len(long) / total) * 100 } def verdict(pulse_data): if pulse_data is None or pulse_data["count"] < MIN_EDGES: return "❌ No signal or not enough pulses" - elif 20 <= pulse_data["short_pct"] <= 80: + elif 10 <= pulse_data["short_pct"] <= 90: return f"✅ LTC-like bi-phase signal detected ({pulse_data['count']} pulses)" else: - return f"⚠️ Inconsistent signal — may be non-LTC or noisy" + return f"⚠️ Pulse imbalance suggests non-LTC or noisy signal" def main(): print("🔍 Capturing 1 second of audio for LTC probing...")