diff --git a/common.py b/common.py index 6795fbb..6a3f963 100644 --- a/common.py +++ b/common.py @@ -1,3 +1,4 @@ +import cStringIO import numpy as np import logging @@ -38,14 +39,11 @@ def load(fname): t = np.arange(len(x)) / Fs return t, x -class Signal(object): - def __init__(self, fd): - self.fd = fd - def send(self, sym, n=1): - sym = sym.imag * scaling - sym = sym.astype('int16') - for i in range(n): - sym.tofile(self.fd) +def dumps(sym, n=1): + sym = sym.imag * scaling + sym = sym.astype('int16') + data = sym.tostring() + return data * n if __name__ == '__main__': diff --git a/send.py b/send.py index a8b8d9b..0a6f93a 100644 --- a/send.py +++ b/send.py @@ -19,16 +19,19 @@ sym = Symbol() data = open('data.send', 'r').read() +def write(fd, sym, n=1): + fd.write(dumps(sym, n)) + def start(sig, c): - sig.send(c*0, n=100) - sig.send(c*1, n=300) - sig.send(c*0, n=100) + write(sig, c*0, n=100) + write(sig, c*1, n=300) + write(sig, c*0, n=100) def train(sig, c): for i in range(20): - sig.send(c*1, n=10) - sig.send(c*0, n=10) - sig.send(c*0, n=100) + write(sig, c*1, n=10) + write(sig, c*0, n=10) + write(sig, c*0, n=100) def modulate(sig, bits): symbols_iter = sigproc.modulator.encode(list(bits)) @@ -37,7 +40,7 @@ def modulate(sig, bits): while True: symbols = itertools.islice(symbols_iter, len(sym.carrier)) symbols = np.array(list(symbols)) - sig.send(np.dot(symbols, carriers)) + write(sig, np.dot(symbols, carriers)) if all(symbols == 0): break @@ -47,13 +50,12 @@ if __name__ == '__main__': log.info('Running MODEM @ {:.1f} kbps'.format(bps / 1e3)) with open('tx.int16', 'wb') as fd: - sig = Signal(fd) - start(sig, sym.carrier[carrier_index]) + start(fd, sym.carrier[carrier_index]) for c in sym.carrier: - train(sig, c) + train(fd, c) bits = to_bits(ecc.encode(data)) - modulate(sig, bits) + modulate(fd, bits) from wave import play, record