def test_too_noisy(): r = random.Random(0) # generate random binary signal signal = np.array([r.choice([-1, 1]) for i in range(int(config.Fs))]) src = BytesIO(common.dumps(signal * 0.5)) for r in calib.detector(config, src=src): assert not r['success'] assert r['msg'] == 'too noisy signal'
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_drift(freq_err): freq = config.Fc * (1 + freq_err / 1e6) t = np.arange(int(1.0 * config.Fs)) * config.Ts frame_length = 100 rms = 0.5 signal = rms * np.cos(2 * np.pi * freq * t) src = BytesIO(common.dumps(signal)) iters = 0 for r in calib.detector(config, src, frame_length=frame_length): assert r['success'] is True assert abs(r['rms'] - rms) < 1e-3 assert abs(r['total'] - rms) < 1e-3 iters += 1 assert iters > 0 assert iters == config.baud / frame_length
def run(size, chan=None, df=0, success=True, 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() try: result = main.recv(config=cfg, src=rx_audio, dst=rx_data, dump_audio=dump, pylab=None) finally: rx_audio.close() rx_data = rx_data.getvalue() assert data.startswith(dump.getvalue()) assert result == success if success: assert rx_data == tx_data
def resample(src, dst, df=0.0): x = common.load(src) sampler = Sampler(x, Interpolator()) sampler.freq += df y = sampler.take(len(x)) dst.write(common.dumps(y))
def test_dumps_loads(): x = np.array([.1, .4, .2, .6, .3, .5]) y = common.loads(common.dumps(x)) assert all(x == y)