mirror of
https://github.com/cjfranko/NTP-Timeturner.git
synced 2025-11-08 18:32:02 +00:00
additional diagnostic
This commit is contained in:
parent
3726b96b86
commit
0c423f30ab
1 changed files with 32 additions and 11 deletions
43
ltc_probe.py
43
ltc_probe.py
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
"""
|
"""
|
||||||
ltc_probe.py
|
ltc_probe.py
|
||||||
Improved LTC-like signal probe — detects pulse duration patterns
|
Advanced LTC-like signal probe using pulse duration clustering
|
||||||
consistent with bi-phase mark code used in SMPTE LTC.
|
for reliable short/long classification — works even with imbalanced timecodes.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
@ -12,38 +12,59 @@ import sounddevice as sd
|
||||||
DURATION = 1.0 # seconds
|
DURATION = 1.0 # seconds
|
||||||
SAMPLERATE = 48000
|
SAMPLERATE = 48000
|
||||||
CHANNELS = 1
|
CHANNELS = 1
|
||||||
MIN_EDGES = 1000 # sanity threshold
|
MIN_EDGES = 1000
|
||||||
|
|
||||||
def detect_rising_edges(signal):
|
def detect_rising_edges(signal):
|
||||||
above_zero = signal > 0
|
above_zero = signal > 0
|
||||||
edges = np.where(np.logical_and(~above_zero[:-1], above_zero[1:]))[0]
|
edges = np.where(np.logical_and(~above_zero[:-1], above_zero[1:]))[0]
|
||||||
return edges
|
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):
|
def analyze_pulse_durations(edges, samplerate):
|
||||||
durations = np.diff(edges) / samplerate
|
durations = np.diff(edges) / samplerate
|
||||||
if len(durations) == 0:
|
if len(durations) == 0:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
short_pulse_threshold = np.median(durations) * 1.5
|
short, long = cluster_durations(durations)
|
||||||
short = durations[durations <= short_pulse_threshold]
|
if short is None or long is None:
|
||||||
long = durations[durations > short_pulse_threshold]
|
return None
|
||||||
|
|
||||||
|
total = len(durations)
|
||||||
return {
|
return {
|
||||||
"count": len(durations),
|
"count": total,
|
||||||
"avg_width_ms": np.mean(durations) * 1000,
|
"avg_width_ms": np.mean(durations) * 1000,
|
||||||
"short_pulses": len(short),
|
"short_pulses": len(short),
|
||||||
"long_pulses": len(long),
|
"long_pulses": len(long),
|
||||||
"short_pct": (len(short) / len(durations)) * 100,
|
"short_pct": (len(short) / total) * 100,
|
||||||
"long_pct": (len(long) / len(durations)) * 100
|
"long_pct": (len(long) / total) * 100
|
||||||
}
|
}
|
||||||
|
|
||||||
def verdict(pulse_data):
|
def verdict(pulse_data):
|
||||||
if pulse_data is None or pulse_data["count"] < MIN_EDGES:
|
if pulse_data is None or pulse_data["count"] < MIN_EDGES:
|
||||||
return "❌ No signal or not enough pulses"
|
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)"
|
return f"✅ LTC-like bi-phase signal detected ({pulse_data['count']} pulses)"
|
||||||
else:
|
else:
|
||||||
return f"⚠️ Inconsistent signal — may be non-LTC or noisy"
|
return f"⚠️ Pulse imbalance suggests non-LTC or noisy signal"
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print("🔍 Capturing 1 second of audio for LTC probing...")
|
print("🔍 Capturing 1 second of audio for LTC probing...")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue