Statistics
| Branch: | Tag: | Revision:

root / gr-digital / python / qa_diff_encoder.py @ 362d45bb

History | View | Annotate | Download (3 kB)

1
#!/usr/bin/env python
2
#
3
# Copyright 2006,2007,2010,2012 Free Software Foundation, Inc.
4
#
5
# This file is part of GNU Radio
6
#
7
# GNU Radio is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3, or (at your option)
10
# any later version.
11
#
12
# GNU Radio is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with GNU Radio; see the file COPYING.  If not, write to
19
# the Free Software Foundation, Inc., 51 Franklin Street,
20
# Boston, MA 02110-1301, USA.
21
#
22
23
from gnuradio import gr, gr_unittest
24
import digital_swig as digital
25
import math
26
import random
27
28
def make_random_int_tuple(L, min, max):
29
    result = []
30
    for x in range(L):
31
        result.append(random.randint(min, max))
32
    return tuple(result)
33
34
35
class test_diff_encoder (gr_unittest.TestCase):
36
37
    def setUp (self):
38
        self.tb = gr.top_block ()
39
40
    def tearDown (self):
41
        self.tb = None
42
43
    def test_diff_encdec_000(self):
44
        random.seed(0)
45
        modulus = 2
46
        src_data = make_random_int_tuple(1000, 0, modulus-1)
47
        expected_result = src_data
48
        src = gr.vector_source_b(src_data)
49
        enc = digital.diff_encoder_bb(modulus)
50
        dec = digital.diff_decoder_bb(modulus)
51
        dst = gr.vector_sink_b()
52
        self.tb.connect(src, enc, dec, dst)
53
        self.tb.run()               # run the graph and wait for it to finish
54
        actual_result = dst.data()  # fetch the contents of the sink
55
        self.assertEqual(expected_result, actual_result)
56
57
    def test_diff_encdec_001(self):
58
        random.seed(0)
59
        modulus = 4
60
        src_data = make_random_int_tuple(1000, 0, modulus-1)
61
        expected_result = src_data
62
        src = gr.vector_source_b(src_data)
63
        enc = digital.diff_encoder_bb(modulus)
64
        dec = digital.diff_decoder_bb(modulus)
65
        dst = gr.vector_sink_b()
66
        self.tb.connect(src, enc, dec, dst)
67
        self.tb.run()               # run the graph and wait for it to finish
68
        actual_result = dst.data()  # fetch the contents of the sink
69
        self.assertEqual(expected_result, actual_result)
70
71
    def test_diff_encdec_002(self):
72
        random.seed(0)
73
        modulus = 8
74
        src_data = make_random_int_tuple(40000, 0, modulus-1)
75
        expected_result = src_data
76
        src = gr.vector_source_b(src_data)
77
        enc = digital.diff_encoder_bb(modulus)
78
        dec = digital.diff_decoder_bb(modulus)
79
        dst = gr.vector_sink_b()
80
        self.tb.connect(src, enc, dec, dst)
81
        self.tb.run()               # run the graph and wait for it to finish
82
        actual_result = dst.data()  # fetch the contents of the sink
83
        self.assertEqual(expected_result, actual_result)
84
85
if __name__ == '__main__':
86
    gr_unittest.run(test_diff_encoder, "test_diff_encoder.xml")
87