Revision b0d95cf8

b/gnuradio-core/src/lib/general/Makefile.am
176 176
	qa_gr_fxpt.cc			\
177 177
	qa_gr_fxpt_nco.cc		\
178 178
	qa_gr_fxpt_vco.cc		\
179
	qa_gr_math.cc
179
	qa_gr_math.cc			\
180
	qa_gri_lfsr.cc
180 181

181 182
grinclude_HEADERS = 			\
182 183
	gr_agc_cc.h                 	\
b/gnuradio-core/src/lib/general/gri_lfsr.h
1
/* -*- c++ -*- */
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 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 <stdexcept>
27
#include <stdint.h>
28

29
/*!
30
 * \brief Fibonacci Linear Feedback Shift Register using specified polynomial mask
31
 * \ingroup math
32
 *
33
 * Generates a maximal length pseudo-random sequence of length 2^degree-1
34
 * 
35
 * Constructor: gri_lfsr(int mask, int seed, int reg_len);
36
 *  
37
 *      mask - polynomial coefficients representing the locations
38
 *             of feedback taps from a shift register which are xor'ed
39
 *             together to form the new high order bit.
40
 *
41
 *             Some common masks might be:
42
 *              x^4 + x^3 + x^0 = 0x19
43
 *              x^5 + x^3 + x^0 = 0x29
44
 *              x^6 + x^5 + x^0 = 0x61
45
 *
46
 *      seed - the initialization vector placed into the register
47
 *             durring initialization.   Low order bit corresponds
48
 *             to x^0 coefficient -- the first to be shifted as output.
49
 *
50
 *   reg_len - specifies the length of the feedback shift register 
51
 *             to be used.   Durring each iteration, the register
52
 *             is rightshifted one and the new bit is placed in bit reg_len.
53
 *             reg_len should generally be at least order(mask) + 1
54
 *
55
 *
56
 * see http://en.wikipedia.org/wiki/Linear_feedback_shift_register 
57
 * for more explanation.
58
 *
59
 *
60
 *
61
 *  next_bit() - performs once cycle of the lfsr,
62
 *               generating the new high order bit from the mask
63
 *               and returning the low order bit which has been
64
 *               shifted out of the register.
65
 *
66
 *
67
 */
68

69

70
class gri_lfsr
71
{
72
 private:
73
  uint32_t d_shift_register;
74
  uint32_t d_mask;
75
  uint32_t d_shift_register_length;	// less than 32
76

77
  static uint32_t
78
  popCount(uint32_t x)
79
  {
80
    uint32_t r = x - ((x >> 1) & 033333333333)
81
                   - ((x >> 2) & 011111111111);
82
    return ((r + (r >> 3)) & 030707070707) % 63;
83
  }
84

85
 public:
86

87
  gri_lfsr(uint32_t mask, uint32_t seed, uint32_t reg_len)
88
    : d_shift_register(seed), d_mask(mask), d_shift_register_length(reg_len)
89
  {
90
    if (reg_len > 31)
91
      throw std::invalid_argument("reg_len must be <= 31");
92
  }
93

94
  unsigned char next_bit() {
95
    unsigned char bit = d_shift_register & 1;
96
    unsigned char newbit = popCount( d_shift_register & d_mask )%2;
97
    d_shift_register = ((d_shift_register>>1) | (newbit<<d_shift_register_length));
98
    return bit;
99
  }
100

101
  int mask() const { return d_mask; }
102
};
103

104
#endif /* INCLUDED_GRI_LFSR_H */
b/gnuradio-core/src/lib/general/qa_general.cc
32 32
#include <qa_gr_fxpt_nco.h>
33 33
#include <qa_gr_fxpt_vco.h>
34 34
#include <qa_gr_math.h>
35
#include <qa_gri_lfsr.h>
35 36

36 37
CppUnit::TestSuite *
37 38
qa_general::suite ()
......
44 45
  s->addTest (qa_gr_fxpt_nco::suite ());
45 46
  s->addTest (qa_gr_fxpt_vco::suite ());
46 47
  s->addTest (qa_gr_math::suite ());
48
  s->addTest (qa_gri_lfsr::suite ());
47 49
  
48 50
  return s;
49 51
}
b/gnuradio-core/src/lib/general/qa_gri_lfsr.cc
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

b/gnuradio-core/src/lib/general/qa_gri_lfsr.h
1
/* -*- c++ -*- */
2
/*
3
 * Copyright 2008 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
#ifndef _QA_GRI_LFSR_H_
23
#define _QA_GRI_LFSR_H_
24

25
#include <cppunit/extensions/HelperMacros.h>
26
#include <cppunit/TestCase.h>
27

28
class qa_gri_lfsr : public CppUnit::TestCase {
29

30
  CPPUNIT_TEST_SUITE(qa_gri_lfsr);
31
  CPPUNIT_TEST(test_lfsr);
32
  CPPUNIT_TEST_SUITE_END();
33

34
 private:
35
  void test_lfsr();
36
};
37

38
#endif /* _QA_GRI_LFSR_H_ */

Also available in: Unified diff