def test_with_tolerable_noise(self): def add_noise(x, step): ''' Add tolerable noise. Return noisy signal Arguments x -- amplitude of signal step -- quantization step ''' noise = random.random() * step / 4 return x + random.choice([-1, 1]) * noise step = 6 d0 = random.uniform(-3, 3) x = random.random() * 100 quantizer = UniformQuantizer(step) emb = BinaryDM(quantizer, d0) # embed a zero sm = emb.embed(x, 0) y = add_noise(sm, step) self.assertEqual(emb.extract(y), 0) # embed a one sm = emb.embed(x, 1) y = add_noise(sm, step) self.assertEqual(emb.extract(y), 1)
def test_without_noise(self): step = 12 d0 = random.uniform(-6, 6) x = random.random() * 100 quantizer = UniformQuantizer(step) emb = BinaryDM(quantizer, d0) # embed a zero self.assertEqual(emb.extract(emb.embed(x, 0)), 0) # embed a one self.assertEqual(emb.extract(emb.embed(x, 1)), 1)
def test_zero_embedded(self): quantizer = MagicMock() emb = BinaryDM(quantizer, d0=5) # emb.embed(x, 0) => 12 # emb.embed(x, 1) => 5 emb.embed = Mock(side_effect=[12, 5]) self.assertEqual(emb.extract(10), 0) self.assertListEqual(emb.embed.call_args_list, [call(10, 0), call(10, 1)]) # Equal distance emb.embed = Mock(side_effect=[2, 2]) self.assertEqual(emb.extract(10), 0) self.assertListEqual(emb.embed.call_args_list, [call(10, 0), call(10, 1)])