root / gnuradio-core / src / lib / general / qa_gri_lfsr.cc @ bdcae56a
History | View | Annotate | Download (4.4 kB)
| 1 | /*
|
|---|---|
| 2 | * Copyright 2008 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This file is part of GNU Radio |
| 5 | * |
| 6 | * GNU Radio is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * GNU Radio is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with GNU Radio; see the file COPYING. If not, write to |
| 18 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 19 | * Boston, MA 02110-1301, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <gri_lfsr.h> |
| 23 | #include <qa_gri_lfsr.h> |
| 24 | #include <cppunit/TestAssert.h> |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | void
|
| 28 | qa_gri_lfsr::test_lfsr () |
| 29 | {
|
| 30 | int mask = 0x19; |
| 31 | int seed = 0x01; |
| 32 | int length = 5; |
| 33 | |
| 34 | gri_lfsr lfsr1(mask,seed,length); |
| 35 | gri_lfsr lfsr2(mask,seed,length); |
| 36 | |
| 37 | unsigned char expected[] = {1, 0, 1, 1, 0, 1, 0, 1, 0, 0}; |
| 38 | |
| 39 | for(unsigned int i=0; i<31; i++){ |
| 40 | lfsr1.next_bit(); |
| 41 | } |
| 42 | |
| 43 | // test that after one lfsr cycle we still match out uncycled lfsr
|
| 44 | for (unsigned int i = 0; i < 41; i++) { |
| 45 | CPPUNIT_ASSERT_EQUAL((int) lfsr1.next_bit(), (int) lfsr2.next_bit()); |
| 46 | } |
| 47 | |
| 48 | // test the known correct values at the given shift offset
|
| 49 | for(unsigned int i=0; i<10; i++){ |
| 50 | CPPUNIT_ASSERT_EQUAL((int) lfsr1.next_bit(), (int) expected[i]); |
| 51 | } |
| 52 | |
| 53 | // test for register length too long
|
| 54 | CPPUNIT_ASSERT_THROW(gri_lfsr(mask, seed, 32), std::invalid_argument);
|
| 55 | } |
| 56 | |
| 57 | void
|
| 58 | qa_gri_lfsr::test_scrambler() |
| 59 | {
|
| 60 | // CCSDS 7-bit scrambler
|
| 61 | int mask = 0x8A; |
| 62 | int seed = 0x7F; |
| 63 | int length = 7; |
| 64 | |
| 65 | gri_lfsr scrambler(mask, seed, length); |
| 66 | |
| 67 | // Impulse (1 and 126 more zeroes)
|
| 68 | unsigned char src[] = |
| 69 | { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
| 70 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 71 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 72 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 73 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 74 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 75 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 76 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 77 | 0, 0, 0, 0, 0, 0, 0 }; // flush bits |
| 78 | |
| 79 | // Impulse response (including leading bits)
|
| 80 | unsigned char expected[] = |
| 81 | { 1, 1, 1, 1, 1, 1, 1,
|
| 82 | 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, |
| 83 | 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, |
| 84 | 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, |
| 85 | 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, |
| 86 | 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, |
| 87 | 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, |
| 88 | 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, |
| 89 | 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, }; |
| 90 | |
| 91 | int len = sizeof(src); |
| 92 | unsigned char actual[len]; |
| 93 | |
| 94 | for (int i = 0; i < len; i++) |
| 95 | actual[i] = scrambler.next_bit_scramble(src[i]); |
| 96 | |
| 97 | CPPUNIT_ASSERT(memcmp(expected, actual, len) == 0);
|
| 98 | } |
| 99 | |
| 100 | void
|
| 101 | qa_gri_lfsr::test_descrambler() |
| 102 | {
|
| 103 | // CCSDS 7-bit scrambler
|
| 104 | int mask = 0x8A; |
| 105 | int seed = 0x7F; |
| 106 | int length = 7; |
| 107 | |
| 108 | gri_lfsr descrambler(mask, seed, length); |
| 109 | |
| 110 | // Scrambled sequence (impulse response)
|
| 111 | unsigned char src[] = |
| 112 | { 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0,
|
| 113 | 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, |
| 114 | 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, |
| 115 | 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, |
| 116 | 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, |
| 117 | 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, |
| 118 | 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, |
| 119 | 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0 }; |
| 120 | |
| 121 | // Original (garbage while synchronizing, them impulse)
|
| 122 | unsigned char expected[] = |
| 123 | { 0, 1, 0, 0, 1, 0,
|
| 124 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 125 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 126 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 127 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 128 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 129 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 130 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 131 | 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 132 | |
| 133 | int len = sizeof(src); |
| 134 | unsigned char actual[len]; |
| 135 | |
| 136 | for (int i = 0; i < len; i++) |
| 137 | actual[i] = descrambler.next_bit_descramble(src[i]); |
| 138 | |
| 139 | CPPUNIT_ASSERT(memcmp(expected, actual, len) == 0);
|
| 140 | } |