3 # Copyright 2007 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
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)
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.
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.
23 from gnuradio import gr, gr_unittest
25 class test_glfsr_source(gr_unittest.TestCase):
28 self.tb = gr.top_block ()
33 def test_000_make_b(self):
34 src = gr.glfsr_source_b(16)
35 self.assertEquals(src.mask(), 0x8016)
36 self.assertEquals(src.period(), 2**16-1)
38 def test_001_degree_b(self):
39 self.assertRaises(RuntimeError,
40 lambda: gr.glfsr_source_b(0))
41 self.assertRaises(RuntimeError,
42 lambda: gr.glfsr_source_b(33))
44 def test_002_correlation_b(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 del self.tb # Discard existing top block
50 self.tb = gr.top_block()
51 self.tb.connect(src, b2f, dst)
53 self.tb.disconnect_all()
54 actual_result = dst.data()
55 R = auto_correlate(actual_result)
56 self.assertEqual(R[0], float(len(R))) # Auto-correlation peak at origin
57 for i in range(len(R)-1):
58 self.assertEqual(R[i+1], -1.0) # Auto-correlation minimum everywhere else
60 def test_003_make_f(self):
61 src = gr.glfsr_source_f(16)
62 self.assertEquals(src.mask(), 0x8016)
63 self.assertEquals(src.period(), 2**16-1)
65 def test_004_degree_f(self):
66 self.assertRaises(RuntimeError,
67 lambda: gr.glfsr_source_f(0))
68 self.assertRaises(RuntimeError,
69 lambda: gr.glfsr_source_f(33))
70 def test_005_correlation_f(self):
71 for degree in range(1,11): # Higher degrees take too long to correlate
72 src = gr.glfsr_source_f(degree, False)
73 dst = gr.vector_sink_f()
74 del self.tb # Discard existing top block
75 self.tb = gr.top_block()
76 self.tb.connect(src, dst)
79 actual_result = dst.data()
80 R = auto_correlate(actual_result)
81 self.assertEqual(R[0], float(len(R))) # Auto-correlation peak at origin
82 for i in range(len(R)-1):
83 self.assertEqual(R[i+1], -1.0) # Auto-correlation minimum everywhere else
85 def auto_correlate(data):
90 R[lag] += data[i]*data[i-lag]
93 if __name__ == '__main__':