Merged r4605:4612 from jcorgan/glfsr branch into trunk. Implements Galois LFSR sourc...
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / qa_glfsr_source_b.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2007 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 2, 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
25 class test_glfsr_source_b(gr_unittest.TestCase):
26
27     def setUp (self):
28         self.fg = gr.flow_graph ()
29
30     def tearDown (self):
31         self.fg = None
32
33     def test_000_make(self):
34         src = gr.glfsr_source_b(16)
35         self.assertEquals(src.mask(), 0x8016)
36         self.assertEquals(src.period(), 2**16-1)
37
38     def test_001_degree(self):
39         self.assertRaises(RuntimeError,
40                           lambda: gr.glfsr_source_b(0))
41         self.assertRaises(RuntimeError,
42                           lambda: gr.glfsr_source_b(33))
43         
44     def test_002_correlation(self):
45         for degree in range(1,11):                # Higher degrees take too long to correlate
46             src = gr.glfsr_source_b(degree, False)
47             b2f = gr.chunks_to_symbols_bf((-1.0,1.0), 1)
48             dst = gr.vector_sink_f()
49             self.fg.connect(src, b2f, dst)
50             self.fg.run()
51
52             actual_result = dst.data()
53             R = auto_correlate(actual_result)
54             self.assertEqual(R[0], float(len(R))) # Auto-correlation peak at origin
55             for i in range(len(R)-1):
56                 self.assertEqual(R[i+1], -1.0)    # Auto-correlation minimum everywhere else
57
58 def auto_correlate(data):
59     l = len(data)
60     R = [0,]*l
61     for lag in range(l):
62         for i in range(l):
63             R[lag] += data[i]*data[i-lag]
64     return R
65
66 if __name__ == '__main__':
67     gr_unittest.main ()