mirror of
https://github.com/romanz/amodem.git
synced 2026-04-21 05:36:42 +08:00
simplify common.iterate()
This commit is contained in:
29
common.py
29
common.py
@@ -1,4 +1,5 @@
|
|||||||
import functools
|
import functools
|
||||||
|
import itertools
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
@@ -59,25 +60,19 @@ def dumps(sym, n=1):
|
|||||||
return data * n
|
return data * n
|
||||||
|
|
||||||
|
|
||||||
def iterate(data, bufsize, offset=0, advance=1, func=None):
|
def iterate(data, size, func=None):
|
||||||
assert bufsize > 0
|
offset = 0
|
||||||
assert offset >= 0
|
data = iter(data)
|
||||||
assert advance > 0
|
|
||||||
buf = np.zeros(bufsize)
|
|
||||||
buf_index = 0
|
|
||||||
for data_index, value in enumerate(data):
|
|
||||||
if data_index < offset:
|
|
||||||
continue
|
|
||||||
|
|
||||||
buf[buf_index] = value
|
while True:
|
||||||
buf_index += 1
|
buf = list(itertools.islice(data, size))
|
||||||
|
if len(buf) < size:
|
||||||
|
return
|
||||||
|
|
||||||
if buf_index == bufsize:
|
buf = np.array(buf)
|
||||||
result = func(buf) if func else buf
|
result = func(buf) if func else buf
|
||||||
yield offset, result
|
yield offset, result
|
||||||
buf[:-advance] = buf[advance:]
|
offset += size
|
||||||
buf_index = max(0, buf_index - advance)
|
|
||||||
offset += advance
|
|
||||||
|
|
||||||
|
|
||||||
class Splitter(object):
|
class Splitter(object):
|
||||||
|
|||||||
4
recv.py
4
recv.py
@@ -28,7 +28,7 @@ CARRIER_THRESHOLD = int(0.95 * CARRIER_DURATION)
|
|||||||
|
|
||||||
def detect(x, freq):
|
def detect(x, freq):
|
||||||
counter = 0
|
counter = 0
|
||||||
for offset, buf in iterate(x, Nsym, advance=Nsym):
|
for offset, buf in iterate(x, Nsym):
|
||||||
coeff = sigproc.coherence(buf, Fc)
|
coeff = sigproc.coherence(buf, Fc)
|
||||||
if abs(coeff) > COHERENCE_THRESHOLD:
|
if abs(coeff) > COHERENCE_THRESHOLD:
|
||||||
counter += 1
|
counter += 1
|
||||||
@@ -185,7 +185,7 @@ def main(fname):
|
|||||||
if data_bits is None:
|
if data_bits is None:
|
||||||
log.warning('Training failed!')
|
log.warning('Training failed!')
|
||||||
else:
|
else:
|
||||||
data = iterate(data_bits, bufsize=8, advance=8, func=to_byte)
|
data = iterate(data_bits, 8, func=to_byte)
|
||||||
data = ''.join(c for _, c in data)
|
data = ''.join(c for _, c in data)
|
||||||
import ecc
|
import ecc
|
||||||
data = ecc.decode(data)
|
data = ecc.decode(data)
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ def extract_symbols(x, freq, offset=0):
|
|||||||
Hc = exp_iwt(-freq, common.Nsym) / (0.5*common.Nsym)
|
Hc = exp_iwt(-freq, common.Nsym) / (0.5*common.Nsym)
|
||||||
func = lambda y: np.dot(Hc, y)
|
func = lambda y: np.dot(Hc, y)
|
||||||
|
|
||||||
iterator = common.iterate(x, common.Nsym, advance=common.Nsym, func=func)
|
iterator = common.iterate(x, common.Nsym, func=func)
|
||||||
for _, symbol in iterator:
|
for _, symbol in iterator:
|
||||||
yield symbol
|
yield symbol
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,26 @@
|
|||||||
import common
|
import common
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
def iterlist(x, *args, **kwargs):
|
def iterlist(x, *args, **kwargs):
|
||||||
x = np.array(x)
|
x = np.array(x)
|
||||||
return [(offset, list(buf)) for offset, buf in common.iterate(x, *args, **kwargs)]
|
return list((i, list(x)) for i, x in common.iterate(x, *args, **kwargs))
|
||||||
|
|
||||||
|
|
||||||
def test_iterate():
|
def test_iterate():
|
||||||
N = 10
|
N = 10
|
||||||
assert iterlist(range(N), 1) == [(i, [i]) for i in range(N)]
|
assert iterlist(range(N), 1) == [
|
||||||
assert iterlist(range(N), 1) == [(i, [i]) for i in range(N)]
|
(i, [i]) for i in range(N)]
|
||||||
assert iterlist(range(N), 2) == [(i, [i, i+1]) for i in range(N-1)]
|
|
||||||
assert iterlist(range(N), 3) == [(i, [i, i+1, i+2]) for i in range(N-2)]
|
assert iterlist(range(N), 2) == [
|
||||||
assert iterlist(range(N), 3, advance=2) == [(i, [i, i+1, i+2]) for i in range(0, N-2, 2)]
|
(i, [i, i+1]) for i in range(0, N-1, 2)]
|
||||||
assert iterlist(range(N), 3, advance=3) == [(i, [i, i+1, i+2]) for i in range(0, N-2, 3)]
|
|
||||||
assert iterlist(range(N), 2, offset=5) == [(i, [i, i+1]) for i in range(5, N-1)]
|
assert iterlist(range(N), 3) == [
|
||||||
assert iterlist(range(N), 1, func=lambda b: -b) == [(i, [-i]) for i in range(N)]
|
(i, [i, i+1, i+2]) for i in range(0, N-2, 3)]
|
||||||
|
|
||||||
|
assert iterlist(range(N), 1, func=lambda b: -b) == [
|
||||||
|
(i, [-i]) for i in range(N)]
|
||||||
|
|
||||||
|
|
||||||
def test_split():
|
def test_split():
|
||||||
L = [(i*2, i*2+1) for i in range(10)]
|
L = [(i*2, i*2+1) for i in range(10)]
|
||||||
@@ -30,6 +36,7 @@ def test_split():
|
|||||||
except IndexError as e:
|
except IndexError as e:
|
||||||
assert e.args == (i,)
|
assert e.args == (i,)
|
||||||
|
|
||||||
|
|
||||||
def test_icapture():
|
def test_icapture():
|
||||||
x = range(100)
|
x = range(100)
|
||||||
y = []
|
y = []
|
||||||
|
|||||||
Reference in New Issue
Block a user