1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2021 Daniel Estevez <daniel@destevez.net>
#
# This file is part of gr-satellites
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
from gnuradio import gr, blocks, gr_unittest
import numpy as np
import pmt
# bootstrap satellites module, even from build dir
try:
import python as satellites
except ImportError:
pass
else:
import sys
sys.modules['satellites'] = satellites
from satellites import convolutional_encoder, viterbi_decoder
class qa_viterbi(gr_unittest.TestCase):
def test_viterbi(self):
tb = gr.top_block()
dbg = blocks.message_debug()
k = 5
p = [25, 23]
enc = convolutional_encoder(k, p)
dec = viterbi_decoder(k, p)
data = np.random.randint(2, size=1000, dtype='uint8')
pdu = pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(data), data))
tb.msg_connect((enc, 'out'), (dec, 'in'))
tb.msg_connect((dec, 'out'), (dbg, 'store'))
enc.to_basic_block()._post(pmt.intern('in'), pdu)
enc.to_basic_block()._post(
pmt.intern('system'),
pmt.cons(pmt.intern('done'), pmt.from_long(1)))
tb.start()
tb.wait()
out = pmt.u8vector_elements(pmt.cdr(dbg.get_message(0)))
np.testing.assert_equal(
out, np.array(data),
'Encoded and decoded message does not match original')
if __name__ == '__main__':
gr_unittest.run(qa_viterbi)
|