Esempio n. 1
0
    def test_001_t (self):
        for src_data, expected_result in (
            ( # A couple of bits.
                  [1,1,1, 0,0,0, 1,1,1, 0,0,0, 1,1,1, 0,0,0, 0,0,0, 0,0,0]
                + [0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0]
                + [1,1,1, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 1,1,1]
                + [0] * 1000,
                [168, 0, 129],
            ),
        ):
            src = blocks.vector_source_b(src_data)
            decode = habets.ook_decode_b(3, 1, 100)
            dbg = blocks.message_debug()
            self.tb.connect(src, decode)
            self.tb.msg_connect(decode, "pdus", dbg, "store")

            self.tb.start()
            while dbg.num_messages() < 1:
                time.sleep(0.1)
            self.tb.stop()
            self.tb.wait()

            result_data = pmt.to_python(pmt.cdr(dbg.get_message(0)))

            try:
                self.assertFloatTuplesAlmostEqual(expected_result, result_data, 1)
            except AssertionError:
                print "--"
                print "Source: ", src_data
                print "Want", expected_result
                print "Got", result_data
                raise
Esempio n. 2
0
    def test_001_t(self):
        for src_data, padding, expected_result in [
            (
                [],
                10,
                [],
            ),
            (
                [
                    [1, 0, 1],
                ],
                10,
                [
                    (1, 0, 1),
                ],
            ),
            (
                [
                    [1, 0, 1],
                    [0, 1, 0],
                ],
                1,
                [
                    (1, 0, 1),
                    (0, 1, 0),
                ],
            ),
        ]:
            # Set up flowgraph.
            source = habets.pn_source_f(padding)
            dst = habets.pn_decode_f(1, 0, 0, 1)
            dbg = blocks.message_debug()

            self.tb.connect(source, dst)
            self.tb.msg_connect(dst, "pdus", dbg, "store")

            for pack in src_data:
                source.to_basic_block()._post(
                    pmt.intern("pdus"),
                    pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(pack), pack)))
            self.tb.start()
            while dbg.num_messages() < len(expected_result):
                time.sleep(0.1)
            self.tb.stop()
            self.tb.wait()
            for n, msg in enumerate(expected_result):
                res = tuple(pmt.to_python(pmt.cdr(dbg.get_message(n))))
                try:
                    self.assertTupleEqual(expected_result[n], res)
                except AssertionError:
                    print "--"
                    print "Source:", msg
                    print "Padding:", padding
                    print "Want:  ", expected_result[n]
                    print "Got:   ", res
                    raise
Esempio n. 3
0
 def test_001_t (self):
     for src_data, expected_result in (
             (
                 [],
                 [],
             ),
             (
                 [0],
                 [0,0,0,0,0,0,0,0],
             ),
             (
                 [128],
                 [1,0,0,0,0,0,0,0],
             ),
             (
                 [2],
                 [0,0,0,0,0,0,1,0],
             ),
             (
                 [128],
                 [1,0,0,0,0,0,0,0],
             ),
             (
                 [128, 64],
                 [1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0],
             ),
     ):
         pack = habets.bitunpacker()
         dbg = blocks.message_debug()
         self.tb.msg_connect(pack, "out", dbg, "store")
         self.tb.start()
         pack.to_basic_block()._post(
             pmt.intern("in"),
             pmt.cons(
                 pmt.PMT_NIL,
                 pmt.init_u8vector(len(src_data), src_data)
             )
         )
         while dbg.num_messages() < 1:
             time.sleep(0.1)
         self.tb.stop()
         self.tb.wait()
         res = pmt.to_python(pmt.cdr(dbg.get_message(0)))
         try:
             self.assertFloatTuplesAlmostEqual(expected_result, res, 1)
         except AssertionError:
             print "--"
             print "Source:", src_data
             print "Want:  ", expected_result
             print "Got:   ", list(res)
             raise
Esempio n. 4
0
    def test_001_t(self):
        try:
            fs = open("jt65.floats").read()
        except IOError:
            print "Test input not found; skipping unit test"
            return
        fs = list(struct.unpack("f" * (len(fs) / 4), fs))
        rnd = lambda x: [random.randint(-120, 120) for _ in range(x)]

        for name, src_data, expected_result in (
            ('clean', fs, "CQ M0THC IO91"),
            ('random-prefix', rnd(44100 * 2) + fs, "CQ M0THC IO91"),
        ):
            # print 'Making data'
            pmt_data = pmt.cons(
                pmt.PMT_NIL, pmt.init_f32vector(len(src_data), list(src_data)))

            # print 'Making flowgraph'
            dec = habets.jt65_decode(44100, int(44100 * 0.372), 10, 8192, 10.8)
            dbg = blocks.message_debug()
            self.tb.msg_connect(dec, "out", dbg, "store")

            # print 'Starting flowgraph'
            self.tb.start()

            # print 'Posting message'
            dec.to_basic_block()._post(
                pmt.intern("in"),
                pmt_data,
            )

            # print 'Waiting for message'
            while dbg.num_messages() < 1:
                time.sleep(0.1)

            # print 'Stopping flowgraph'
            self.tb.stop()
            self.tb.wait()
            # print 'Getting reply'
            res = pmt.to_python(pmt.cdr(dbg.get_message(0)))
            res = ''.join([chr(x) for x in res])
            try:
                # print res
                assert res == expected_result
                print "%s OK" % name
            except AssertionError:
                print "--"
                print "Want:  ", expected_result
                print "Got:   ", res
                raise
Esempio n. 5
0
 def _handle_msg(self, msg_pmt):
     meta = pmt.car(msg_pmt)
     bs = pmt.u8vector_elements(pmt.cdr(msg_pmt))
     fs = numpy.frombuffer(''.join([chr(x) for x in bs]),
                           dtype=numpy.float32)
     #print "Got msg of size %d" % len(fs)
     #print midpoint(fs)
     misc, data = wpcr(fs)
     bits = slice_bits(data)
     vec = pmt.init_u8vector(len(bits), [int(x) for x in bits])
     meta = pmt.dict_add(meta, pmt.intern("magic_decoder"),
                         pmt.to_pmt(misc))
     self.message_port_pub(self.port_out, pmt.cons(meta, vec))
     self.message_port_pub(self.port_info, meta)
 def test_001_t(self):
     for src_data, expected_result in (
         (
             [],
             [],
         ),
         (
             [0],
             [],
         ),
         (
             [1],
             [],
         ),
         (
             [0, 1, 0, 1, 0, 0, 1],
             [0, 0, 1],
         ),
         (  # Garbage beginning.
             [1, 1, 0, 1, 0, 1, 0, 0, 1],
             [0, 0, 1],
         ),
         (  # Pick last preamble.
             [0, 1, 0, 1, 0, 1, 0, 0, 1],
             [0, 0, 1],
         ),
     ):
         if not expected_result:
             continue
         strip = preamble_stripper(prefix=[0, 1, 0, 1])
         dbg = blocks.message_debug()
         self.tb.msg_connect(strip, "out", dbg, "store")
         self.tb.start()
         strip.to_basic_block()._post(
             pmt.intern("in"),
             pmt.cons(pmt.PMT_NIL,
                      pmt.init_u8vector(len(src_data), src_data)))
         while dbg.num_messages() < 1:
             time.sleep(0.1)
         self.tb.stop()
         self.tb.wait()
         res = pmt.to_python(pmt.cdr(dbg.get_message(0)))
         try:
             self.assertFloatTuplesAlmostEqual(expected_result, res, 1)
         except AssertionError:
             print "--"
             print "Source:", src_data
             print "Want:  ", expected_result
             print "Got:   ", list(res)
             raise
Esempio n. 7
0
 def test_001_t(self):
     for src_data, expected_result in (
         (
             [
                 1,
                 1,
                 1,  # Simple bit.
                 -1,
                 0,
                 -1,  # Null sample.
                 0,
                 1,
                 1,
                 1,  # Late bit.
                 -1,
                 -1,  # Short bit.
                 1,
                 1,  # Another short bit.
                 -1,
                 -1,
                 -1,
                 -1,
                 -1,
                 -1,
                 -1,
                 -1,
                 -1
             ] + [0] * 20,
             [1, 0, 1, 0, 1, 0, 0, 0],
         ), ):
         src = blocks.vector_source_f([float(x) for x in src_data])
         decode = habets.pn_decode_f(3, 1, 0.1, 10)
         dbg = blocks.message_debug()
         self.tb.connect(src, decode)
         self.tb.msg_connect(decode, "pdus", dbg, "store")
         self.tb.start()
         while dbg.num_messages() < 1:
             time.sleep(0.1)
         self.tb.stop()
         self.tb.wait()
         res = pmt.to_python(pmt.cdr(dbg.get_message(0)))
         try:
             self.assertFloatTuplesAlmostEqual(expected_result, res, 1)
         except AssertionError:
             print "--"
             print "Source:", src_data
             print "Want:  ", expected_result
             print "Got:   ", res
             raise
Esempio n. 8
0
    def _handle_msg(self, msg_pmt):
        bits = pmt.u8vector_elements(pmt.cdr(msg_pmt))

        found = None
        for i in range(self.search - 1, -1, -1):
            candidate = list(bits[i:i + len(self.prefix)])
            if candidate == self.prefix:
                found = i
                break
        if found is None:
            return
        bits = bits[found + len(self.prefix):]
        vec = pmt.init_u8vector(len(bits), bits)
        self.message_port_pub(pmt.intern('out'),
                              pmt.cons(pmt.car(msg_pmt), vec))
Esempio n. 9
0
 def test_001_t(self):
     for src_data, expected_result in (
             #(
             #    [],
             #    [],
             #),
         (  # Three samples per step.
             [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0],
             [1.0, 1.0, 2.0, 3.0],
         ),
         (  # Discard initial crap.
             [
                 -1, 1, -1, 1, -1, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0,
                 2.0, 3.0, 3.0, 3.0
             ],
             [1.0, 1.0, 2.0, 3.0],
         ),
         (  # Three samples per step, but with one garbage in between.
             [
                 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.5, 2.0, 2.0, 2.0, 0.5, 3.0,
                 3.0, 3.0
             ],
             [1.0, 1.0, 2.0, 3.0],
         ),
     ):
         stair = habets.stairclocker(3, 3, 0.02)
         dbg = blocks.message_debug()
         self.tb.msg_connect(stair, "out", dbg, "store")
         self.tb.start()
         stair.to_basic_block()._post(
             pmt.intern("in"),
             pmt.cons(pmt.PMT_NIL,
                      pmt.init_f32vector(len(src_data), src_data)))
         while dbg.num_messages() < 1:
             time.sleep(0.1)
         self.tb.stop()
         self.tb.wait()
         res = pmt.to_python(pmt.cdr(dbg.get_message(0)))
         res = struct.unpack('f' * (len(res) / 4), res)
         try:
             self.assertFloatTuplesAlmostEqual(expected_result, res, 1)
         except AssertionError:
             print "--"
             print "Source:", src_data
             print "Want:  ", expected_result
             print "Got:   ", res
             raise
Esempio n. 10
0
 def test_001_t (self):
     for src_data, expected_result in (
             (
                 [],
                 [0,1,0,1],
             ),
             (
                 [0],
                 [0,1,0,1,0],
             ),
             (
                 [1],
                 [0,1,0,1,1],
             ),
             (
                 [0,1,0,1,0,0,1],
                 [0,1,0,1,0,1,0,1,0,0,1],
             ),
     ):
         adder = preamble_adder(preamble=[0,1,0,1])
         dbg = blocks.message_debug()
         self.tb.msg_connect(adder, "out", dbg, "store")
         self.tb.start()
         vec = pmt.init_u8vector(len(src_data), src_data)
         adder.to_basic_block()._post(
             pmt.intern("in"),
             pmt.cons(
                 pmt.PMT_NIL,
                 pmt.init_u8vector(len(src_data), src_data),
             )
         )
         while dbg.num_messages() < 1:
             time.sleep(0.1)
         self.tb.stop()
         self.tb.wait()
         res = pmt.to_python(pmt.cdr(dbg.get_message(0)))
         try:
             self.assertFloatTuplesAlmostEqual(expected_result, res, 1)
         except AssertionError:
             print "--"
             print "Source:", src_data
             print "Want:  ", expected_result
             print "Got:   ", res
             raise
Esempio n. 11
0
 def _handle_msg(self, msg_pmt):
     bits = list(self.preamble) + list(
         pmt.u8vector_elements(pmt.cdr(msg_pmt)))
     vec = pmt.init_u8vector(len(bits), bits)
     self.message_port_pub(pmt.intern("out"), pmt.cons(pmt.PMT_NIL, vec))
Esempio n. 12
0
    def test_001_t(self):
        for src_data, expected_result in (
                # 128 instead of -1 to get the types right.
            (
                [128, 1, 1, 1, 1, 1, 1, 1, 0],
                [127],
            ),
            (
                [
                    128,
                    1,
                    128,
                    1,
                    128,
                    1,
                    128,
                    1,
                    128,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    128,
                    1,
                    128,
                    1,
                    128,
                    1,
                    128,
                    1,
                    0,
                ],
                [85, 127, 85],
            ),
        ):
            src = blocks.vector_source_b(src_data)
            decode = pn_decode_identity_b([0])
            dbg = blocks.message_debug()
            self.tb.connect(src, decode)
            self.tb.msg_connect(decode, "pdus", dbg, "store")

            self.tb.start()
            while dbg.num_messages() < 1:
                time.sleep(0.1)
            self.tb.stop()
            self.tb.wait()

            result_data = pmt.to_python(pmt.cdr(dbg.get_message(0)))

            try:
                self.assertFloatTuplesAlmostEqual(expected_result, result_data,
                                                  1)
            except AssertionError:
                print "--"
                print "Source: ", src_data
                print "Want", expected_result
                print "Got", result_data
                raise
Esempio n. 13
0
    def test_001_t(self):
        for name, src_data, tags, packets in (
            (
                "One packet exactly",
                [1, -1, 0],
                [
                    make_tag(0, 'burst', True),
                    make_tag(2, 'burst', False),
                ],
                [
                    [1, -1, 0],
                ],
            ),
            (
                "Surprising tag order",
                [1, -1, 0],
                [
                    make_tag(2, 'burst', False),
                    make_tag(0, 'burst', True),
                ],
                [
                    [1, -1, 0],
                ],
            ),
            (
                "One packet with spacing",
                [0, 1, 0, 0],
                [
                    make_tag(1, 'burst', True),
                    make_tag(2, 'burst', False),
                ],
                [
                    [1, 0],
                ],
            ),
            (
                "Two packets without spacing",
                [9, 1, 8, -1, -2],
                [
                    make_tag(1, 'burst', True),
                    make_tag(2, 'burst', False),
                    make_tag(3, 'burst', True),
                    make_tag(4, 'burst', False),
                ],
                [
                    [1, 8],
                    [-1, -2],
                ],
            ),
            (
                "Two packets with spacing",
                [9, 1, 8, 7, -1, -2],
                [
                    make_tag(1, 'burst', True),
                    make_tag(2, 'burst', False),
                    make_tag(4, 'burst', True),
                    make_tag(5, 'burst', False),
                ],
                [
                    [1, 8],
                    [-1, -2],
                ],
            ),
            (
                "A huge packet",
                [-1] + range(2000),
                [
                    make_tag(1, 'burst', True),
                    make_tag(2000, 'burst', False),
                ],
                [
                    range(2000),
                ],
            ),
        ):
            src = blocks.vector_source_f(src_data, tags=tags)
            p = habets.packetize_burst('burst', 100)
            dbg = blocks.message_debug()
            self.tb.connect(src, p)
            self.tb.msg_connect(p, "pdus", dbg, "store")
            self.tb.start()
            while dbg.num_messages() < len(packets):
                time.sleep(0.1)
            self.tb.stop()
            self.tb.wait()

            for n, packet in enumerate(packets):
                r = pmt.to_python(pmt.cdr(dbg.get_message(n)))
                result_data = numpy.frombuffer(''.join([chr(x) for x in r]),
                                               dtype=numpy.float32)

                try:
                    self.assertFloatTuplesAlmostEqual(packet, result_data, 1)
                except AssertionError:
                    print "--"
                    print "Test name: ", name
                    print "Output packet index: ", n
                    print "Source: ", src_data
                    print "Want", packets
                    print "Got", result_data
                    raise