From 90dd3e55f043c34d447f4288d81f3fc6dccc7473 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Mon, 16 Feb 2015 19:11:19 +0200 Subject: [PATCH] detect: find actual starting offset of the carrier --- amodem/detect.py | 28 +++++++++++++++++++--------- tests/test_detect.py | 4 ++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/amodem/detect.py b/amodem/detect.py index f15643c..5adfb5b 100644 --- a/amodem/detect.py +++ b/amodem/detect.py @@ -66,7 +66,7 @@ class Detector(object): bufs.append(np.array(trailing)) buf = np.concatenate(bufs) - offset = self.find_start(buf, duration=self.CARRIER_DURATION) + offset = self.find_start(buf) start_time += (offset / self.Nsym - self.SEARCH_WINDOW) * self.Tsym log.debug('Carrier starts at %.3f ms', start_time * 1e3) @@ -76,14 +76,24 @@ class Detector(object): amplitude, freq_err = self.estimate(buf[:prefix_length]) return itertools.chain(buf, samples), amplitude, freq_err - def find_start(self, buf, duration): - filt = dsp.FIR(dsp.exp_iwt(self.omega, self.Nsym)) - p = np.abs(list(filt(buf))) ** 2 - p = np.cumsum(p)[self.Nsym-1:] - p = np.concatenate([[0], p]) - length = (duration - 1) * self.Nsym - correlations = np.abs(p[length:] - p[:-length]) - offset = np.argmax(correlations) + def find_start(self, buf): + carrier = dsp.exp_iwt(self.omega, self.Nsym) + carrier = np.tile(carrier, self.SEARCH_WINDOW // 4) + zeroes = carrier * 0.0 + signal = np.concatenate([zeroes, carrier]) + signal = (2 ** 0.5) * signal / dsp.norm(signal) + + coeffs = [] + for i in range(len(buf) - len(signal)): + b = buf[i:i+len(signal)] + c = 0.0 + if dsp.norm(b): + c = np.abs(np.dot(b, signal)) / dsp.norm(b) + coeffs.append(c) + + index = np.argmax(coeffs) + log.debug('Starting coherence: %.3f%%', coeffs[index] * 100) + offset = index + len(zeroes) return offset def estimate(self, buf, skip=5): diff --git a/tests/test_detect.py b/tests/test_detect.py index f6d9ad4..a0c96a4 100644 --- a/tests/test_detect.py +++ b/tests/test_detect.py @@ -19,7 +19,7 @@ def test_detect(): detector = detect.Detector(config, pylab=common.Dummy()) samples, amp, freq_err = detector.run(x) assert abs(1 - amp) < 1e-12 - assert abs(freq_err) < 1e-16 + assert abs(freq_err) < 1e-12 x = np.cos(2 * np.pi * (2*config.Fc) * t) with pytest.raises(ValueError): @@ -56,6 +56,6 @@ def test_find_start(): for offset in range(32): bufs = [prefix, [0] * offset, carrier, postfix] buf = np.concatenate(bufs) - start = detector.find_start(buf, length) + start = detector.find_start(buf) expected = offset + len(prefix) assert expected == start