def test_recovery_over_stream(self): tasks = [ ("tag", "hello"), ("buff", numpy.array(numpy.random.randint(-300, +300, 100), numpy.uint32)), ("tag", 2.0 + 3j), ("buff", numpy.array(numpy.random.randint(-300, +300, 23), numpy.uint32)), ("msg", 16.2), ("buff", numpy.array(numpy.random.randint(-300, +300, 10), numpy.uint32)), ("buff", numpy.array(numpy.random.randint(-300, +300, 66), numpy.uint32)), ("msg", "bye!"), ] src = RandomStuffSource(tasks) ser = gras.make('/grex/serializer', 0, True) #these two slice up the datagrams #can we recover from such harsh slicing? d2s = gras.make('/grex/datagram_to_stream', numpy.dtype(numpy.int32).itemsize) s2d = gras.make('/grex/stream_to_datagram', numpy.dtype(numpy.int32).itemsize, 40) #mtu 40 bytes deser = gras.make('/grex/deserializer', True) #true for recovery on dst = RandomStuffSink() self.tb.connect(src, ser, d2s, s2d, deser, dst) self.tb.run() self.check_equal_helper(dst, tasks)
def test_sync_word_length_not_used(self): for i in range(16): s = gras.make('/grex/scrambler') d = gras.make('/grex/descrambler') poly = 1 | (1 << 14) | (1 << 15) seed = random.randint(-1024, 1024) mode = random.choice(("additive", "multiplicative")) sync = "111100111100000111101101" print poly, seed, mode for obj in (s, d): obj.set_poly(poly) obj.set_seed(seed) obj.set_mode(mode) obj.set_sync(sync) nbits = random.randint(128, 4096) src_data = tuple(numpy.random.randint(0, 2, nbits)) src = TestUtils.VectorSource(numpy.uint8, src_data) dst = TestUtils.VectorSink(numpy.uint8) self.tb.connect(src, s, d, dst) self.tb.run() self.tb.disconnect_all() #lengths wont equal, sync word does history thing n = min(len(src_data), len(dst.data())) self.assertTrue(src_data[:n], dst.data()[:n])
def test_simple_loopback(self): for i in range(16): s = gras.make('/grex/scrambler') d = gras.make('/grex/descrambler') poly = 1 | (1 << 14) | (1 << 15) seed = random.randint(-1024, 1024) mode = random.choice(("additive", "multiplicative")) print poly, seed, mode for obj in (s, d): obj.set_poly(poly) obj.set_seed(seed) obj.set_mode(mode) nbits = random.randint(128, 4096) src_data = tuple(numpy.random.randint(0, 2, nbits)) src = TestUtils.VectorSource(numpy.uint8, src_data) dst = TestUtils.VectorSink(numpy.uint8) self.tb.connect(src, s, d, dst) midst = TestUtils.VectorSink(numpy.uint8) self.tb.connect(s, midst) self.tb.run() self.tb.disconnect_all() self.assertNotEqual(src_data, midst.data()) self.assertTrue(src_data, dst.data())
def test_simple_loopback(self): framer = gras.make('/grex/packet_framer', samples_per_symbol = 2, bits_per_symbol = 1, ) deframer = gras.make('/grex/packet_deframer') src_data = tuple(numpy.random.randint(-1024, 1024, 11)) #40 bytes + padding src = TestUtils.VectorSource(numpy.int32, src_data) dst = TestUtils.VectorSink(numpy.int32) unpack = gras.make('/grex/unpack_bytes') s2d = gras.make('/grex/stream_to_datagram', numpy.dtype(numpy.int32).itemsize, 40) #mtu 40 bytes d2s = gras.make('/grex/datagram_to_stream', numpy.dtype(numpy.int32).itemsize) self.tb.connect(src, s2d, framer, unpack, deframer, d2s, dst) self.tb.start() time.sleep(1.0) self.tb.stop() self.tb.wait() self.tb.disconnect_all() print "src_data", src_data print "dst_data", dst.data() n = min(map(len, (src_data, dst.data()))) print n self.assertTrue(n > 0) self.assertEqual(src_data[:n], dst.data()[:n])
def test_udp_loopback(self): udp_server = gras.make('/grex/socket_message', "UDP_SERVER", 'localhost', '34567', 0) udp_client = gras.make('/grex/socket_message', "UDP_CLIENT", 'localhost', '34567', 0) #client sends to server - must be first self.do_loop(udp_client, udp_server) #server can reply to client self.do_loop(udp_server, udp_client)
def test_simple_loopback(self): src_data = (8, 3, 32, 18, -53) src = TestUtils.VectorSource(numpy.int32, src_data) dst = TestUtils.VectorSink(numpy.int32) s2d = gras.make("/grex/stream_to_datagram", numpy.dtype(numpy.float32).itemsize, 0) d2s = gras.make("/grex/datagram_to_stream", numpy.dtype(numpy.float32).itemsize) self.tb.connect(src, s2d, d2s, dst) self.tb.run() self.assertEqual(src_data, dst.data())
def test_simple_loopback(self): src_data = (8, 3, 32, 18, -53) src = TestUtils.VectorSource(numpy.int32, src_data) dst = TestUtils.VectorSink(numpy.int32) s2d = gras.make('/grex/stream_to_datagram', numpy.dtype(numpy.float32).itemsize, 0) d2s = gras.make('/grex/datagram_to_stream', numpy.dtype(numpy.float32).itemsize) self.tb.connect(src, s2d, d2s, dst) self.tb.run() self.assertEqual(src_data, dst.data())
def test_div_ff_2(self): src1_data =( 5, 9, -15, 1024) src2_data =(10, 3, -5, 64) expected_result =(0.5, 3, 3, 16) op = gras.make('/grex/divide_f32_f32') self.help_ff((src1_data, src2_data), expected_result, op)
def __init__(self, access_code=None, threshold=-1): """ Create a new packet deframer. @param access_code: AKA sync vector @type access_code: string of 1's and 0's @param threshold: detect access_code with up to threshold bits wrong (-1 -> use default) @type threshold: int """ gras.HierBlock.__init__(self, "PacketDeframer") try: import packet_utils except ImportError: from gnuradio.digital import packet_utils if not access_code: access_code = packet_utils.default_access_code if not packet_utils.is_1_0_string(access_code): raise ValueError, "Invalid access_code %r. Must be string of 1's and 0's" % ( access_code, ) if threshold == -1: threshold = 12 # FIXME raise exception try: import digital_swig as gr_digital except ImportError: import gnuradio.digital as gr_digital self.correlator = gr_digital.correlate_access_code_bb( access_code, threshold) self.framer_sink = gras.make('/grex/framer_sink_1') self._queue_to_datagram = _queue_to_datagram() self.connect(self, self.correlator, self.framer_sink, self._queue_to_datagram, self)
def test_sub_ii_2(self): src1_data =(1, 2, 3, 4, 5) src2_data =(8, -3, 4, 8, 2) expected_result =(-7, 5, -1, -4, 3) op = gras.make('/grex/subtract_s32_s32') self.help_ii((src1_data, src2_data), expected_result, op)
def test_make_math_const_vs(self): op = gras.make('/grex/add_const_v_fc32_fc32', [1, 2, 4]) op = gras.make('/grex/add_const_v_f32_f32', [1, 2, 4]) op = gras.make('/grex/add_const_v_sc32_sc32', [1, 2, 4]) op = gras.make('/grex/add_const_v_s32_s32', [1, 2, 4]) op = gras.make('/grex/multiply_const_v_fc32_fc32', [1, 2, 4]) op = gras.make('/grex/multiply_const_v_f32_f32', [1, 2, 4]) op = gras.make('/grex/multiply_const_v_sc32_sc32', [1, 2, 4]) op = gras.make('/grex/multiply_const_v_s32_s32', [1, 2, 4])
def do_loop(self, to_socket, from_socket): src_data = tuple(numpy.random.randint(-1024, 1024, 10)) src = TestUtils.VectorSource(numpy.int32, src_data) dst = TestUtils.VectorSink(numpy.int32) s2d = gras.make('/grex/stream_to_datagram', numpy.dtype(numpy.int32).itemsize, 0) d2s = gras.make('/grex/datagram_to_stream', numpy.dtype(numpy.int32).itemsize) self.tb.connect(src, s2d, to_socket) self.tb.connect(from_socket, d2s, dst) self.tb.start() time.sleep(0.5) self.tb.stop() self.tb.wait() self.tb.disconnect_all() self.assertEqual(src_data, dst.data())
def test_tcp_loopback(self): tcp_server = gras.make('/grex/socket_message', "TCP_SERVER", 'localhost', '34567', 0) tcp_client = gras.make('/grex/socket_message', "TCP_CLIENT", 'localhost', '34567', 0) #client sends to server self.do_loop(tcp_client, tcp_server) #server reply to client self.do_loop(tcp_server, tcp_client) #a second client to replace the first connection print 'doing second client test now...' tcp_client2 = gras.make('/grex/socket_message', "TCP_CLIENT", 'localhost', '34567', 0) self.do_loop(tcp_client2, tcp_server) #must be first to establish new connection self.do_loop(tcp_server, tcp_client2) print '...done second client test'
def test_signal_source_f32(self): expected_result = (1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5) src1 = gras.make('/grex/signal_source_f32') src1.set_frequency(0) src1.set_waveform("CONST") src1.set_amplitude(1.5) op = TestUtils.Head(numpy.float32, 10) dst1 = TestUtils.VectorSink(numpy.float32) self.tb.connect(src1, op, dst1) self.tb.run() dst_data = dst1.data() self.assertEqual(expected_result, dst_data)
def test_simple_loopback(self): framer = gras.make( '/grex/packet_framer', samples_per_symbol=2, bits_per_symbol=1, ) deframer = gras.make('/grex/packet_deframer') src_data = tuple(numpy.random.randint(-1024, 1024, 11)) #40 bytes + padding src = TestUtils.VectorSource(numpy.int32, src_data) dst = TestUtils.VectorSink(numpy.int32) unpack = gras.make('/grex/unpack_bytes') s2d = gras.make('/grex/stream_to_datagram', numpy.dtype(numpy.int32).itemsize, 40) #mtu 40 bytes d2s = gras.make('/grex/datagram_to_stream', numpy.dtype(numpy.int32).itemsize) self.tb.connect(src, s2d, framer, unpack, deframer, d2s, dst) self.tb.start() time.sleep(1.0) self.tb.stop() self.tb.wait() self.tb.disconnect_all() print "src_data", src_data print "dst_data", dst.data() n = min(map(len, (src_data, dst.data()))) print n self.assertTrue(n > 0) self.assertEqual(src_data[:n], dst.data()[:n])
def test_sqrt_float32(self): op = gras.make('/grex/orc_block') op.set_program("volk_32f_sqrt_32f_a_orc_impl", SOURCE) vec = numpy.array(numpy.random.randint(10, +150, 10000), numpy.float32) src = TestUtils.VectorSource(numpy.float32, vec) dst = TestUtils.VectorSink(numpy.float32) self.tb.connect(src, op, dst) self.tb.run() expected_result = list(numpy.sqrt(vec)) actual_result = list(dst.data()) self.assertEqual(expected_result, actual_result)
def test_delay_simple(self): for delay in (0, 100, 1000, 10000): src_data = [1, 2, 3, 4, 5, 6, 7, 8] expected_result = tuple([0] * delay + src_data) src = TestUtils.VectorSource(numpy.float32, src_data) op = gras.make('/grex/delay', numpy.dtype(numpy.float32).itemsize) op.set_delay(delay) dst = TestUtils.VectorSink(numpy.float32) tb = gras.TopBlock() tb.connect(src, op, dst) tb.run() dst_data = dst.data() self.assertEqual(expected_result, dst_data)
def test_stream_selector_simple(self): ss = gras.make('/grex/stream_selector', numpy.dtype(numpy.float32).itemsize) ss.set_paths([1, 0]) #in[0] -> out[1], in[1] -> out[0] src0 = TestUtils.VectorSource(numpy.float32, [1, 2, 3, 4]) src1 = TestUtils.VectorSource(numpy.float32, [5, 6, 7, 8]) dst0 = TestUtils.VectorSink(numpy.float32) dst1 = TestUtils.VectorSink(numpy.float32) tb = gras.TopBlock() tb.connect(src0, (ss, 0), dst0) tb.connect(src1, (ss, 1), dst1) tb.start() time.sleep(0.5) tb.stop() tb.wait() self.assertEqual((1, 2, 3, 4), dst1.data()) self.assertEqual((5, 6, 7, 8), dst0.data())
def test_add_const_float32(self): gras.jit_factory(ADD_CONST_F32_SOURCE, ["-O3", "-I"+gras_inc, "-I"+pmc_inc]) op = gras.make("/tests/my_add_const_f32") offset = 42. op.set_value(offset) #set offset for test self.assertAlmostEqual(op.get_value(), offset) vec = numpy.array(numpy.random.randint(-150, +150, 10000), numpy.float32) src = TestUtils.VectorSource(numpy.float32, vec) dst = TestUtils.VectorSink(numpy.float32) self.tb.connect(src, op, dst) self.tb.run() expected_result = list(vec + offset) actual_result = list(dst.data()) self.assertEqual(expected_result, actual_result)
def test_add_float32(self): gras.jit_factory(ADD_F32_SOURCE, ["-O3", "-I"+gras_inc, "-I"+pmc_inc]) op = gras.make("/tests/my_add_f32") vec0 = numpy.array(numpy.random.randint(-150, +150, 10000), numpy.float32) vec1 = numpy.array(numpy.random.randint(-150, +150, 10000), numpy.float32) src0 = TestUtils.VectorSource(numpy.float32, vec0) src1 = TestUtils.VectorSource(numpy.float32, vec1) dst = TestUtils.VectorSink(numpy.float32) self.tb.connect(src0, (op, 0)) self.tb.connect(src1, (op, 1)) self.tb.connect(op, dst) self.tb.run() expected_result = list(vec0 + vec1) actual_result = list(dst.data()) self.assertEqual(expected_result, actual_result)
def test_add_float32(self): op = gras.make('/grex/orc_block') op.set_program("volk_32f_x2_add_32f_a_orc_impl", SOURCE) vec0 = numpy.array(numpy.random.randint(-150, +150, 10000), numpy.float32) vec1 = numpy.array(numpy.random.randint(-150, +150, 10000), numpy.float32) src0 = TestUtils.VectorSource(numpy.float32, vec0) src1 = TestUtils.VectorSource(numpy.float32, vec1) dst = TestUtils.VectorSink(numpy.float32) self.tb.connect(src0, (op, 0)) self.tb.connect(src1, (op, 1)) self.tb.connect(op, dst) self.tb.run() expected_result = list(vec0 + vec1) actual_result = list(dst.data()) self.assertEqual(expected_result, actual_result)
def test_add_complex64(self): op = gras.make('/grex/opencl_block', "") op.set_program("add_2x_complex64", vector_add_gpu_SOURCE, "") op.set_input_size(0, 8) op.set_output_size(0, 8) vec0 = numpy.array(numpy.random.randint(-150, +150, 1e6), numpy.complex64) vec1 = numpy.array(numpy.random.randint(-150, +150, 1e6), numpy.complex64) src0 = TestUtils.VectorSource(numpy.complex64, vec0) src1 = TestUtils.VectorSource(numpy.complex64, vec1) dst = TestUtils.VectorSink(numpy.complex64) self.tb.connect(src0, (op, 0)) self.tb.connect(src1, (op, 1)) self.tb.connect(op, dst) self.tb.run() expected_result = list(vec0 + vec1) actual_result = list(dst.data()) self.assertEqual(expected_result, actual_result)
def test_float32(self): op = gras.make('/grex/noise_source_f32', 0) op.set_waveform("GAUSSIAN") op.set_amplitude(10) head = TestUtils.Head(numpy.float32, 12) dst = TestUtils.VectorSink(numpy.float32) self.tb.connect(op, head, dst) self.tb.run() # expected results for Gaussian with seed 0, ampl 10 expected_result = (-6.8885869979858398, 26.149959564208984, 20.575775146484375, -7.9340143203735352, 5.3359274864196777, -12.552099227905273, 6.333674430847168, -23.830753326416016, -16.603046417236328, 2.9676761627197266, 1.2176077365875244, 15.100193977355957) dst_data = dst.data() self.assertEqual(len(expected_result), len(dst_data)) for i in range(len(dst_data)): self.assertAlmostEqual(expected_result[i], dst_data[i], places=3)
def test_mult_const_ii(self): src_data = (-1, 0, 1, 2, 3) expected_result = (-5, 0, 5, 10, 15) op = gras.make('/grex/multiply_const_s32_s32', 5) self.help_ii((src_data, ), expected_result, op)
def test_mult_ff(self): src1_data = (1, 2, 3, 4) src2_data = (8, -3, 4, 8) expected_result = (8, -6, 12, 32) op = gras.make('/grex/multiply_f32_f32') self.help_ff((src1_data, src2_data), expected_result, op)
def test_mult_ii(self): src1_data = (1, 2, 3, 4, 5) src2_data = (8, -3, 4, 8, 2) expected_result = (8, -6, 12, 32, 10) op = gras.make('/grex/multiply_s32_s32') self.help_ii((src1_data, src2_data), expected_result, op)
def test_sub_ii_2(self): src1_data = (1, 2, 3, 4, 5) src2_data = (8, -3, 4, 8, 2) expected_result = (-7, 5, -1, -4, 3) op = gras.make('/grex/subtract_s32_s32') self.help_ii((src1_data, src2_data), expected_result, op)
def test_sub_ii_1(self): src1_data = (1, 2, 3, 4, 5) expected_result = (-1, -2, -3, -4, -5) op = gras.make('/grex/subtract_s32_s32') self.help_ii((src1_data, ), expected_result, op)
def test_div_ff_2(self): src1_data = (5, 9, -15, 1024) src2_data = (10, 3, -5, 64) expected_result = (0.5, 3, 3, 16) op = gras.make('/grex/divide_f32_f32') self.help_ff((src1_data, src2_data), expected_result, op)
def test_div_ff_1(self): src1_data = (1, 2, 4, -8) expected_result = (1, 0.5, 0.25, -.125) op = gras.make('/grex/divide_f32_f32') self.help_ff((src1_data, ), expected_result, op)
def test_set_math_const_vs(self): op = gras.make('/grex/add_const_v_fc32_fc32', [1, 2, 4]) op.set_const([0, 5, 6]) op = gras.make('/grex/multiply_const_v_fc32_fc32', [1, 2, 4]) op.set_const([0, 5, 6])
def test_add_const_cc_1(self): src_data = (1, 2, 3, 4, 5) expected_result = (3 + 5j, 4 + 5j, 5 + 5j, 6 + 5j, 7 + 5j) op = gras.make('/grex/add_const_fc32_fc32', 2 + 5j) self.help_cc((src_data, ), expected_result, op)
def test_add_const_ff(self): src_data = (1, 2, 3, 4, 5) expected_result = (6, 7, 8, 9, 10) op = gras.make('/grex/add_const_f32_f32', 5) self.help_ff((src_data, ), expected_result, op)
def test_mult_const_cc(self): src_data = (-1 - 1j, 0 + 0j, 1 + 1j, 2 + 2j, 3 + 3j) expected_result = (1 - 5j, 0 + 0j, -1 + 5j, -2 + 10j, (-3 + 15j)) op = gras.make('/grex/multiply_const_fc32_fc32', 2 + 3j) self.help_cc((src_data, ), expected_result, op)
def test_load_module_cpp(self): my_block = gras.make("/tests/my_block0") self.assertEqual(my_block.get_num(), 42)