Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import logging
import numpy as np
from . import dsp
from . import equalizer
from . import common
log = logging.getLogger(__name__)
class Detector:
COHERENCE_THRESHOLD = 0.9
CARRIER_DURATION = sum(equalizer.prefix)
CARRIER_THRESHOLD = int(0.9 * CARRIER_DURATION)
SEARCH_WINDOW = int(0.1 * CARRIER_DURATION)
START_PATTERN_LENGTH = SEARCH_WINDOW // 4
def __init__(self, config, pylab):
self.freq = config.Fc
self.omega = 2 * np.pi * self.freq / config.Fs
self.Nsym = config.Nsym
self.Tsym = config.Tsym
self.maxlen = config.baud # 1 second of symbols
self.max_offset = config.timeout * config.Fs
self.plt = pylab
def _wait(self, samples):
counter = 0
bufs = collections.deque([], maxlen=self.maxlen)
def test():
length = 1024
data = b'\x12\x34' * length
with mock.patch('ctypes.CDLL') as cdll:
lib = mock.Mock()
lib.Pa_GetErrorText = lambda code: b'Error' if code else b'Success'
lib.Pa_GetDefaultOutputDevice.return_value = 1
lib.Pa_GetDefaultInputDevice.return_value = 2
lib.Pa_OpenStream.return_value = 0
cdll.return_value = lib
interface = audio.Interface(config=config.fastest(), debug=True)
assert interface.load(name='portaudio') is interface
with interface:
s = interface.player()
assert s.params.device == 1
s.stream = 1 # simulate non-zero output stream handle
s.write(data=data)
s.close()
with interface:
s = interface.recorder()
assert s.params.device == 2
s.stream = 2 # simulate non-zero input stream handle
s.read(len(data))
s.close()
with pytest.raises(Exception):
def run(size, chan=None, df=0, success=True, reader=None, cfg=None):
if cfg is None:
cfg = config.fastest()
tx_data = os.urandom(size)
tx_audio = BytesIO()
main.send(config=cfg, src=BytesIO(tx_data), dst=tx_audio, gain=0.5)
data = tx_audio.getvalue()
data = common.loads(data)
if chan is not None:
data = chan(data)
if df:
sampler = sampling.Sampler(data, sampling.Interpolator())
sampler.freq += df
data = sampler.take(len(data))
data = common.dumps(data)
rx_audio = BytesIO(data)
rx_data = BytesIO()
dump = BytesIO()
if reader:
rx_audio = reader(rx_audio)
try:
result = main.recv(config=cfg, src=rx_audio, dst=rx_data,
dump_audio=dump, pylab=None)
finally:
rx_audio.close()
def run(size, chan=None, df=0, success=True, reader=None, cfg=None):
if cfg is None:
cfg = config.fastest()
tx_data = os.urandom(size)
tx_audio = BytesIO()
main.send(config=cfg, src=BytesIO(tx_data), dst=tx_audio, gain=0.5)
data = tx_audio.getvalue()
data = common.loads(data)
if chan is not None:
data = chan(data)
if df:
sampler = sampling.Sampler(data, sampling.Interpolator())
sampler.freq += df
data = sampler.take(len(data))
data = common.dumps(data)
rx_audio = BytesIO(data)
rx_data = BytesIO()
dump = BytesIO()
if reader:
rx_audio = reader(rx_audio)
try:
result = main.recv(config=cfg, src=rx_audio, dst=rx_data,
def test_configs():
default = config.Configuration()
fastest = config.fastest()
slowest = config.slowest()
assert slowest.modem_bps <= default.modem_bps
assert fastest.modem_bps >= default.modem_bps
def test_configs():
default = config.Configuration()
fastest = config.fastest()
slowest = config.slowest()
assert slowest.modem_bps <= default.modem_bps
assert fastest.modem_bps >= default.modem_bps
def test_resample():
x = np.sin(2*np.pi * 10 * np.linspace(0, 1, 1001))
src = BytesIO(common.dumps(x))
dst = BytesIO()
sampling.resample(src=src, dst=dst, df=0.0)
y = common.loads(dst.getvalue())
err = x[:len(y)] - y
assert np.max(np.abs(err)) < 1e-4
dst = BytesIO()
sampling.resample(src=BytesIO(b'\x00\x00'), dst=dst, df=0.0)
assert dst.tell() == 0
def test_detect():
P = sum(equalizer.prefix)
t = np.arange(P * config.Nsym) * config.Ts
x = np.cos(2 * np.pi * config.Fc * t)
detector = detect.Detector(config, pylab=common.Dummy())
samples, amp, freq_err = detector.run(x)
assert abs(1 - amp) < 1e-12
assert abs(freq_err) < 1e-12
x = np.cos(2 * np.pi * (2*config.Fc) * t)
with pytest.raises(ValueError):
detector.run(x)
with pytest.raises(ValueError):
detector.max_offset = 0
detector.run(x)
def test_demux():
freqs = np.array([1e3, 2e3])
omegas = 2 * np.pi * freqs / config.Fs
carriers = [dsp.exp_iwt(2*np.pi*f/config.Fs, config.Nsym) for f in freqs]
syms = [3, 2j]
sig = np.dot(syms, carriers)
res = dsp.Demux(sampling.Sampler(sig.real), omegas, config.Nsym)
res = np.array(list(res))
assert np.max(np.abs(res - syms)) < 1e-12
def symbols_stream(signal):
sampler = sampling.Sampler(signal)
return dsp.Demux(sampler=sampler, omegas=[omega], Nsym=config.Nsym)
r = recv.Receiver(config, pylab=common.Dummy())