diff --git a/common.py b/common.py index 8dd768f..2ac38e0 100644 --- a/common.py +++ b/common.py @@ -46,6 +46,12 @@ class SaturationError(ValueError): pass +def check_saturation(x): + peak = np.max(np.abs(x)) + if peak > SATURATION_THRESHOLD: + raise SaturationError(peak) + + def load(fileobj, time=False): return loads(fileobj.read(), time=time) @@ -53,10 +59,6 @@ def load(fileobj, time=False): def loads(data, time=False): x = np.fromstring(data, dtype='int16') x = x / scaling - peak = np.max(np.abs(x)) - if peak > SATURATION_THRESHOLD: - raise SaturationError(peak) - if time: t = np.arange(len(x)) / Fs return t, x @@ -121,6 +123,10 @@ def icapture(iterable, result): result.append(i) yield i + +def take(iterable, n): + return np.array(list(itertools.islice(iterable, n))) + if __name__ == '__main__': import pylab diff --git a/recv.py b/recv.py index faae96c..97c8786 100755 --- a/recv.py +++ b/recv.py @@ -85,11 +85,6 @@ def find_start(buf, length): return np.argmax(cumsumP[length:] - cumsumP[:-length]) -def take(symbols, n): - symbols = itertools.islice(symbols, n) - return np.array(list(symbols)) - - def receive_prefix(symbols): S = take(symbols, len(train.prefix))[:, carrier_index] sliced = np.round(S) @@ -228,14 +223,13 @@ def main(fname): log.info('Running MODEM @ {:.1f} kbps'.format(sigproc.modem_bps / 1e3)) fd = sys.stdin if (fname == '-') else open(fname, 'rb') - samples = stream.iread(fd) - result = detect(samples, Fc) - if result is None: - log.warning('No carrier detected') - return + signal = stream.iread(fd) + take(signal, 100) # skip initial 0.1 second, due to spurious spikes + stream.check = check_saturation size = 0 - bits = receive(result, frequencies) + signal = detect(signal, Fc) + bits = receive(signal, frequencies) try: for chunk in decode(bits): sys.stdout.write(chunk) diff --git a/stream.py b/stream.py index 5473921..fc59b93 100644 --- a/stream.py +++ b/stream.py @@ -14,6 +14,7 @@ class Reader(object): def __init__(self, fd): self.fd = fd + self.check = None def __iter__(self): return self @@ -26,8 +27,12 @@ class Reader(object): data = self.fd.read(left) if data: block.extend(data) + if len(block) == self.BUFSIZE: - return common.loads(str(block)) + values = common.loads(str(block)) + if self.check: + self.check(values) + return values time.sleep(self.WAIT)