root / gnuradio-core / src / python / gnuradio / gr / qa_glfsr_source.py @ 2104a9d1
History | View | Annotate | Download (3.4 kB)
| 1 | #!/usr/bin/env python
|
|---|---|
| 2 | #
|
| 3 | # Copyright 2007,2010 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 | |
| 25 | class test_glfsr_source(gr_unittest.TestCase): |
| 26 | |
| 27 | def setUp (self): |
| 28 | self.tb = gr.top_block ()
|
| 29 | |
| 30 | def tearDown (self): |
| 31 | self.tb = None |
| 32 | |
| 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) |
| 37 | |
| 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)) |
| 43 | |
| 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)
|
| 52 | self.tb.run()
|
| 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 |
| 59 | |
| 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) |
| 64 | |
| 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)
|
| 77 | self.tb.run()
|
| 78 | |
| 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 |
| 84 | |
| 85 | def auto_correlate(data): |
| 86 | l = len(data)
|
| 87 | R = [0,]*l
|
| 88 | for lag in range(l): |
| 89 | for i in range(l): |
| 90 | R[lag] += data[i]*data[i-lag] |
| 91 | return R
|
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | gr_unittest.run(test_glfsr_source, "test_glfsr_source.xml")
|