Refactor a bit

This commit is contained in:
Roman Zeyde
2014-06-27 18:23:12 +03:00
parent b276440b01
commit 5a854be751
7 changed files with 24 additions and 17 deletions

1
.gitignore vendored
View File

@@ -1,2 +1 @@
*.int16
*.pyc

View File

@@ -34,7 +34,11 @@ def to_bytes(bits):
return chr(byte)
def load(fname):
x = np.fromfile(open(fname, 'rb'), dtype='int16')
data = open(fname, 'rb').read()
return loads(data)
def loads(data):
x = np.fromstring(data, dtype='int16')
x = x / scaling
t = np.arange(len(x)) / Fs
return t, x
@@ -45,6 +49,9 @@ def dumps(sym, n=1):
data = sym.tostring()
return data * n
def norm(x):
return np.sqrt(np.dot(x.conj(), x).real)
if __name__ == '__main__':
import pylab

4
ecc.py
View File

@@ -34,6 +34,10 @@ def decode(data, nsym=DEFAULT_NSYM):
except ReedSolomonError:
break
overhead = (i - len(dec)) / float(i)
blocks = i / BLOCK_SIZE
log.debug('Decoded %d blocks = %d bytes (ECC overhead %.1f%%)', blocks, len(dec), overhead * 100)
n = struct.calcsize(LEN_FMT)
payload, length = dec[n:], dec[:n]
length, = struct.unpack(LEN_FMT, length)

View File

@@ -15,9 +15,6 @@ COHERENCE_THRESHOLD = 0.9
CARRIER_DURATION = 300
CARRIER_THRESHOLD = int(0.9 * CARRIER_DURATION)
def norm(x):
return np.sqrt(np.dot(x.conj(), x).real)
def power(x):
return np.dot(x.conj(), x).real / len(x)
@@ -38,7 +35,7 @@ def exp_iwt(freq, n):
def coherence(x, freq):
n = len(x)
Hc = exp_iwt(freq, n) / np.sqrt(0.5*n)
Hc = exp_iwt(-freq, n) / np.sqrt(0.5*n)
return np.dot(Hc, x) / norm(x)
def detect(freq):

View File

@@ -62,7 +62,8 @@ if __name__ == '__main__':
r = record('rx.int16')
start = time.time()
p = play(fd)
p = play(fd.name)
p.wait()
log.debug('Took %.2f seconds', time.time() - start)
time.sleep(0.1)
r.stop()

View File

@@ -61,7 +61,7 @@ class QAM(object):
index = np.argmin(np.abs(s - keys))
yield self._dec[ keys[index] ]
modulator = QAM(bits_per_symbol=4, radii=[0.6, 1.0])
modulator = QAM(bits_per_symbol=2, radii=[1.0])
def test():
q = QAM(bits_per_symbol=8, radii=[0.25, 0.5, 0.75, 1.0])

17
wave.py
View File

@@ -4,15 +4,14 @@ import subprocess as sp
from common import Fs
def play(fd):
args = ['aplay', fd.name, '-q', '-f', 'S16_LE', '-c', '1', '-r', str(int(Fs))]
ret = sp.call(args=args)
assert ret == 0
def play(fname, **kwargs):
return launch('aplay', fname, '-q', '-f', 'S16_LE', '-c', '1', '-r', str(int(Fs)), **kwargs)
def record(fname):
args = ['arecord', fname, '-q', '-f', 'S16_LE', '-c', '1', '-r', str(int(Fs))]
p = sp.Popen(args=args)
def record(fname, **kwargs):
return launch('arecord', fname, '-q', '-f', 'S16_LE', '-c', '1', '-r', str(int(Fs)), **kwargs)
def launch(*args, **kwargs):
print args
p = sp.Popen(args=args, **kwargs)
p.stop = lambda: os.kill(p.pid, signal.SIGINT)
return p