def report_carrier(bufs, begin): x = np.concatenate(tuple(bufs)[-CARRIER_THRESHOLD:-1]) Hc = sigproc.exp_iwt(-config.Fc, len(x)) Zc = np.dot(Hc, x) / (0.5*len(x)) amp = abs(Zc) log.info('Carrier detected at ~%.1f ms @ %.1f kHz:' ' coherence=%.3f%%, amplitude=%.3f', begin * config.Tsym * 1e3 / config.Nsym, config.Fc / 1e3, np.abs(sigproc.coherence(x, config.Fc)) * 100, amp) return amp
def detect(samples, freq): counter = 0 bufs = collections.deque([], maxlen=config.baud) # 1 second of symbols for offset, buf in common.iterate(samples, config.Nsym): bufs.append(buf) coeff = sigproc.coherence(buf, config.Fc) if abs(coeff) > COHERENCE_THRESHOLD: counter += 1 else: counter = 0 if counter == CARRIER_THRESHOLD: length = (CARRIER_THRESHOLD - 1) * config.Nsym begin = offset - length amplitude = report_carrier(bufs, begin=begin) break else: raise ValueError('No carrier detected') log.debug('Buffered %d ms of audio', len(bufs)) to_append = SEARCH_WINDOW + (CARRIER_DURATION - CARRIER_THRESHOLD) bufs_iterator = common.iterate(samples, config.Nsym) for _, buf in itertools.islice(bufs_iterator, to_append): bufs.append(buf) bufs = tuple(bufs)[-CARRIER_DURATION-2*SEARCH_WINDOW:] buf = np.concatenate(bufs) offset = find_start(buf, length=config.Nsym*CARRIER_DURATION) start = begin - config.Nsym * SEARCH_WINDOW + offset log.info('Carrier starts at %.3f ms', start * config.Tsym * 1e3 / config.Nsym) return itertools.chain(buf[offset:], samples), amplitude