GNU Radio 3.5.3.2 C++ API
gri_lfsr.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,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 #ifndef INCLUDED_GRI_LFSR_H
24 #define INCLUDED_GRI_LFSR_H
25 
26 #include <gr_core_api.h>
27 #include <stdexcept>
28 #include <stdint.h>
29 
30 /*!
31  * \brief Fibonacci Linear Feedback Shift Register using specified polynomial mask
32  * \ingroup misc
33  *
34  * Generates a maximal length pseudo-random sequence of length 2^degree-1
35  *
36  * Constructor: gri_lfsr(int mask, int seed, int reg_len);
37  *
38  * mask - polynomial coefficients representing the locations
39  * of feedback taps from a shift register which are xor'ed
40  * together to form the new high order bit.
41  *
42  * Some common masks might be:
43  * x^4 + x^3 + x^0 = 0x19
44  * x^5 + x^3 + x^0 = 0x29
45  * x^6 + x^5 + x^0 = 0x61
46  *
47  * seed - the initialization vector placed into the register
48  * durring initialization. Low order bit corresponds
49  * to x^0 coefficient -- the first to be shifted as output.
50  *
51  * reg_len - specifies the length of the feedback shift register
52  * to be used. Durring each iteration, the register
53  * is rightshifted one and the new bit is placed in bit reg_len.
54  * reg_len should generally be at least order(mask) + 1
55  *
56  *
57  * see http://en.wikipedia.org/wiki/Linear_feedback_shift_register
58  * for more explanation.
59  *
60  *
61  *
62  * next_bit() - Standard LFSR operation
63  *
64  * Perform one cycle of the LFSR. The output bit is taken from
65  * the shift register LSB. The shift register MSB is assigned from
66  * the modulo 2 sum of the masked shift register.
67  *
68  * next_bit_scramble(unsigned char input) - Scramble an input stream
69  *
70  * Perform one cycle of the LFSR. The output bit is taken from
71  * the shift register LSB. The shift register MSB is assigned from
72  * the modulo 2 sum of the masked shift register and the input LSB.
73  *
74  * next_bit_descramble(unsigned char input) - Descramble an input stream
75  *
76  * Perform one cycle of the LFSR. The output bit is taken from
77  * the modulo 2 sum of the masked shift register and the input LSB.
78  * The shift register MSB is assigned from the LSB of the input.
79  *
80  * See http://en.wikipedia.org/wiki/Scrambler for operation of these
81  * last two functions (see multiplicative scrambler.)
82  *
83  */
84 
86 {
87  private:
88  uint32_t d_shift_register;
89  uint32_t d_mask;
90  uint32_t d_seed;
91  uint32_t d_shift_register_length; // less than 32
92 
93  static uint32_t
94  popCount(uint32_t x)
95  {
96  uint32_t r = x - ((x >> 1) & 033333333333)
97  - ((x >> 2) & 011111111111);
98  return ((r + (r >> 3)) & 030707070707) % 63;
99  }
100 
101  public:
102 
103  gri_lfsr(uint32_t mask, uint32_t seed, uint32_t reg_len)
104  : d_shift_register(seed),
105  d_mask(mask),
106  d_seed(seed),
107  d_shift_register_length(reg_len)
108  {
109  if (reg_len > 31)
110  throw std::invalid_argument("reg_len must be <= 31");
111  }
112 
113  unsigned char next_bit() {
114  unsigned char output = d_shift_register & 1;
115  unsigned char newbit = popCount( d_shift_register & d_mask )%2;
116  d_shift_register = ((d_shift_register>>1) | (newbit<<d_shift_register_length));
117  return output;
118  }
119 
120  unsigned char next_bit_scramble(unsigned char input) {
121  unsigned char output = d_shift_register & 1;
122  unsigned char newbit = (popCount( d_shift_register & d_mask )%2)^(input & 1);
123  d_shift_register = ((d_shift_register>>1) | (newbit<<d_shift_register_length));
124  return output;
125  }
126 
127  unsigned char next_bit_descramble(unsigned char input) {
128  unsigned char output = (popCount( d_shift_register & d_mask )%2)^(input & 1);
129  unsigned char newbit = input & 1;
130  d_shift_register = ((d_shift_register>>1) | (newbit<<d_shift_register_length));
131  return output;
132  }
133 
134  /*!
135  * Reset shift register to initial seed value
136  */
137  void reset() { d_shift_register = d_seed; }
138 
139  /*!
140  * Rotate the register through x number of bits
141  * where we are just throwing away the results to get queued up correctly
142  */
143  void pre_shift(int num){
144  for(int i=0; i<num; i++){
145  next_bit();
146  }
147  }
148 
149  int mask() const { return d_mask; }
150 };
151 
152 #endif /* INCLUDED_GRI_LFSR_H */