feat: find_beat(): bias with f_hint - once we know the beat freq

This commit is contained in:
2026-05-17 16:55:13 +02:00
parent e349278c06
commit f3f580f923

14
beat.py
View File

@@ -1,5 +1,6 @@
import numpy as np import numpy as np
import matplotlib.pyplot as plt # for debug only import matplotlib.pyplot as plt # for debug only
from sqi import gauss
# note: may be called ZxingDetector instead? # note: may be called ZxingDetector instead?
class SsfZxing: class SsfZxing:
@@ -161,14 +162,25 @@ class RegularBeatFinder:
num_freqs = 200 #: number of freq steps to evaluate num_freqs = 200 #: number of freq steps to evaluate
range_f1 = 0.5 #: lowest detection frequency in Hz range_f1 = 0.5 #: lowest detection frequency in Hz
range_f2 = 4.0 #: highest detection frequency in Hz range_f2 = 4.0 #: highest detection frequency in Hz
f_bias_width = 0.2 #: gaussian std relative to num_freqs within f1..f2
def __init__(self): pass def __init__(self): pass
def find_beat(self, fs, ssf_zxings, debug_fe=False, debug_i=None): def find_beat(self, fs, ssf_zxings, f_hint=None, debug_fe=False, debug_i=None):
"""Find the optimal beat frequency.""" """Find the optimal beat frequency."""
act_ibis = np.diff(ssf_zxings) act_ibis = np.diff(ssf_zxings)
# evaluate mean absolute errors for all frequencies # evaluate mean absolute errors for all frequencies
freqs, freq_errs = self._get_opt_ibi_freq_2(fs, act_ibis, debug_i) freqs, freq_errs = self._get_opt_ibi_freq_2(fs, act_ibis, debug_i)
# bias with f_hint - once we know the beat freq, make it more likely for it to be found everywhere
nf, f1, f2 = RegularBeatFinder.num_freqs, RegularBeatFinder.range_f2, RegularBeatFinder.range_f1
bias = gauss(
nf,
(f_hint - f1) / (f2 - f1) * nf,
RegularBeatFinder.f_bias_width * nf
)
freqs_bias = 1.0 / (np.max(bias)+bias) # make 'f_hint' at most 2x more likely -- (1+bias) if normalized
freq_errs *= freqs_bias
#
if debug_fe: if debug_fe:
plt.figure(figsize=(8,2)) plt.figure(figsize=(8,2))
plt.plot(freqs, freq_errs) plt.plot(freqs, freq_errs)