Hi,

I have added (de-)scramble methods to the gri_glfsr.h Galois LFSR.

Benefits: 
- Galois type LFSRs are faster then Fibonacci type (the later uses popcount, 
which is not available on all CPUs, and the current code has it implemented 
in C).
- The Fibonacci LFSR have a delay corresponding to the register length (this 
could be changed ...). The Galois LFSR has no delay.
I think several people have been tricked by the second point when using GR for 
packet radio - one has to feed enough data to flush the register.

Both types of LFSRs can be interchanged and mixed arbitrarily, but there are 
some caveats:
- Polynom specification for both types are different.
For Gal', one only has to use the binary representation (e.g. x^4+x^3+1 => 
2^4+2^3+1 = 0x19) and shift it one bit left ( 0x13>>1 = 0x9).
For Fib, the following has to be done: Reverse the binary representation (0x19 
= 11001 => 10011), strip the highest bit. The resulting parameters for the 
lfsr are (mask=0x3, len=(degree-1)=3). (Yes, this is somewhat contrary to the 
comments in gri_lfsr.h. IMHO, the documentation is wrong/misleading. Most 
noteworthy, the len paramater is not the register len, but the bitshift).
- Internal state of the two types differs. For self synchronizing 
applications, this is a non issue, for synchronous operation, either use the 
same type, or calculate the corresponding seed values for both types.

The GR blocks are more or less plain copies of the existing blocks. Might be a 
good idea to drop the (unused) len parameter.

Everything open for discussion.

Regards,

Stefan

-- 
 Stefan Brüns  /  Bergstraße 21  /  52062 Aachen
phone: +49 241 53809034   mobile: +49 151 50412019
>From 31179ebdef99eefa976bc65780d27870a9f15f6c Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Stefan=20Br=C3=BCns?= <stefan.bru...@rwth-aachen.de>
Date: Wed, 16 Dec 2009 18:32:53 +0100
Subject: [PATCH] Add galois lfsr (de-)scrambler

- add next_bit_(de-)scramble method to glfsr class, same signature
  as lfsr class
- add gnuradio blocks and test cases
---
 gnuradio-core/src/lib/general/Makefile.am          |    9 +-
 gnuradio-core/src/lib/general/general.i            |    4 +
 .../src/lib/general/gr_descrambler2_bb.cc          |   56 ++++++++
 gnuradio-core/src/lib/general/gr_descrambler2_bb.h |   59 +++++++++
 gnuradio-core/src/lib/general/gr_descrambler2_bb.i |   31 +++++
 gnuradio-core/src/lib/general/gr_scrambler2_bb.cc  |   56 ++++++++
 gnuradio-core/src/lib/general/gr_scrambler2_bb.h   |   59 +++++++++
 gnuradio-core/src/lib/general/gr_scrambler2_bb.i   |   31 +++++
 gnuradio-core/src/lib/general/gri_glfsr.h          |   19 +++
 gnuradio-core/src/lib/general/qa_general.cc        |    2 +
 gnuradio-core/src/lib/general/qa_gri_glfsr.cc      |  136 ++++++++++++++++++++
 gnuradio-core/src/lib/general/qa_gri_glfsr.h       |   42 ++++++
 12 files changed, 503 insertions(+), 1 deletions(-)
 create mode 100644 gnuradio-core/src/lib/general/gr_descrambler2_bb.cc
 create mode 100644 gnuradio-core/src/lib/general/gr_descrambler2_bb.h
 create mode 100644 gnuradio-core/src/lib/general/gr_descrambler2_bb.i
 create mode 100644 gnuradio-core/src/lib/general/gr_scrambler2_bb.cc
 create mode 100644 gnuradio-core/src/lib/general/gr_scrambler2_bb.h
 create mode 100644 gnuradio-core/src/lib/general/gr_scrambler2_bb.i
 create mode 100644 gnuradio-core/src/lib/general/qa_gri_glfsr.cc
 create mode 100644 gnuradio-core/src/lib/general/qa_gri_glfsr.h

diff --git a/gnuradio-core/src/lib/general/Makefile.am b/gnuradio-core/src/lib/general/Makefile.am
index ff188f7..0ec82f5 100644
--- a/gnuradio-core/src/lib/general/Makefile.am
+++ b/gnuradio-core/src/lib/general/Makefile.am
@@ -171,7 +171,9 @@ libgeneral_la_SOURCES = 		\
 	malloc16.c			\
 	gr_unpack_k_bits_bb.cc		\
 	gr_descrambler_bb.cc		\
+	gr_descrambler2_bb.cc		\
 	gr_scrambler_bb.cc		\
+	gr_scrambler2_bb.cc		\
 	gr_probe_mpsk_snr_c.cc		\
 	gr_probe_density_b.cc
 
@@ -183,7 +185,8 @@ libgeneral_qa_la_SOURCES = 		\
 	qa_gr_fxpt_nco.cc		\
 	qa_gr_fxpt_vco.cc		\
 	qa_gr_math.cc			\
-	qa_gri_lfsr.cc
+	qa_gri_lfsr.cc			\
+	qa_gri_glfsr.cc
 
 grinclude_HEADERS = 			\
 	gr_agc_cc.h                 	\
@@ -338,7 +341,9 @@ grinclude_HEADERS = 			\
 	random.h			\
 	gr_unpack_k_bits_bb.h		\
 	gr_descrambler_bb.h		\
+	gr_descrambler2_bb.h		\
 	gr_scrambler_bb.h		\
+	gr_scrambler2_bb.h		\
 	gr_probe_mpsk_snr_c.h		\
 	gr_probe_density_b.h
 
@@ -475,6 +480,8 @@ swiginclude_HEADERS =			\
 	gri_agc2_cc.i			\
 	gri_agc2_ff.i			\
 	gr_descrambler_bb.i		\
+	gr_descrambler2_bb.i		\
 	gr_scrambler_bb.i		\
+	gr_scrambler2_bb.i		\
 	gr_probe_mpsk_snr_c.i		\
 	gr_probe_density_b.i
diff --git a/gnuradio-core/src/lib/general/general.i b/gnuradio-core/src/lib/general/general.i
index 0684e63..d13bd92 100644
--- a/gnuradio-core/src/lib/general/general.i
+++ b/gnuradio-core/src/lib/general/general.i
@@ -131,7 +131,9 @@
 #include <gr_encode_ccsds_27_bb.h>
 #include <gr_decode_ccsds_27_fb.h>
 #include <gr_descrambler_bb.h>
+#include <gr_descrambler2_bb.h>
 #include <gr_scrambler_bb.h>
+#include <gr_scrambler2_bb.h>
 #include <gr_probe_mpsk_snr_c.h>
 #include <gr_probe_density_b.h>
 #include <gr_rail_ff.h>
@@ -251,7 +253,9 @@
 %include "gr_encode_ccsds_27_bb.i"
 %include "gr_decode_ccsds_27_fb.i"
 %include "gr_descrambler_bb.i"
+%include "gr_descrambler2_bb.i"
 %include "gr_scrambler_bb.i"
+%include "gr_scrambler2_bb.i"
 %include "gr_probe_mpsk_snr_c.i"
 %include "gr_probe_density_b.i"
 %include "gr_rail_ff.i"
diff --git a/gnuradio-core/src/lib/general/gr_descrambler2_bb.cc b/gnuradio-core/src/lib/general/gr_descrambler2_bb.cc
new file mode 100644
index 0000000..d0e3f1f
--- /dev/null
+++ b/gnuradio-core/src/lib/general/gr_descrambler2_bb.cc
@@ -0,0 +1,56 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2008 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <gr_descrambler2_bb.h>
+#include <gr_io_signature.h>
+
+gr_descrambler2_bb_sptr
+gr_make_descrambler2_bb(int mask, int seed, int len)
+{
+  return gr_descrambler2_bb_sptr(new gr_descrambler2_bb(mask, seed, len));
+}
+
+gr_descrambler2_bb::gr_descrambler2_bb(int mask, int seed, int len)
+  : gr_sync_block("descrambler2_bb",
+		  gr_make_io_signature (1, 1, sizeof (unsigned char)),
+		  gr_make_io_signature (1, 1, sizeof (unsigned char))),
+    d_lfsr(mask, seed)
+{
+}
+
+int
+gr_descrambler2_bb::work(int noutput_items,
+		      gr_vector_const_void_star &input_items,
+		      gr_vector_void_star &output_items)
+{
+  const unsigned char *in = (const unsigned char *) input_items[0];
+  unsigned char *out = (unsigned char *) output_items[0];
+
+  for (int i = 0; i < noutput_items; i++)
+    out[i] = d_lfsr.next_bit_descramble(in[i]);
+
+  return noutput_items;
+}
diff --git a/gnuradio-core/src/lib/general/gr_descrambler2_bb.h b/gnuradio-core/src/lib/general/gr_descrambler2_bb.h
new file mode 100644
index 0000000..18c06c6
--- /dev/null
+++ b/gnuradio-core/src/lib/general/gr_descrambler2_bb.h
@@ -0,0 +1,59 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2008 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+#ifndef INCLUDED_GR_DESCRAMBLER2_BB_H
+#define INCLUDED_GR_DESCRAMBLER2_BB_H
+
+#include <gr_sync_block.h>
+#include "gri_glfsr.h"
+
+class gr_descrambler2_bb;
+typedef boost::shared_ptr<gr_descrambler2_bb> gr_descrambler2_bb_sptr;
+
+gr_descrambler2_bb_sptr gr_make_descrambler2_bb(int mask, int seed, int len);
+
+/*!
+ * Descramble an input stream using an LFSR.  This block works on the LSB only
+ * of the input data stream, i.e., on an "unpacked binary" stream, and
+ * produces the same format on its output.
+ *
+ * \param mask     Polynomial mask for LFSR
+ * \param seed     Initial shift register contents
+ * \param len      Shift register length
+ *
+ * \ingroup misc
+ */
+
+class gr_descrambler2_bb : public gr_sync_block
+{
+  friend gr_descrambler2_bb_sptr gr_make_descrambler2_bb(int mask, int seed, int len);
+
+  gri_glfsr d_lfsr;
+
+  gr_descrambler2_bb(int mask, int seed, int len);
+
+public:
+  int work(int noutput_items,
+	   gr_vector_const_void_star &input_items,
+	   gr_vector_void_star &output_items);
+};
+
+#endif /* INCLUDED_GR_DESCRAMBLER2_BB_H */
diff --git a/gnuradio-core/src/lib/general/gr_descrambler2_bb.i b/gnuradio-core/src/lib/general/gr_descrambler2_bb.i
new file mode 100644
index 0000000..3b500e4
--- /dev/null
+++ b/gnuradio-core/src/lib/general/gr_descrambler2_bb.i
@@ -0,0 +1,31 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2008 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+GR_SWIG_BLOCK_MAGIC(gr,descrambler2_bb);
+
+gr_descrambler2_bb_sptr gr_make_descrambler2_bb(int mask, int seed, int len);
+
+class gr_descrambler2_bb : public gr_sync_block
+{
+private:
+  gr_descrambler2_bb(int mask, int seed, int len);
+};
diff --git a/gnuradio-core/src/lib/general/gr_scrambler2_bb.cc b/gnuradio-core/src/lib/general/gr_scrambler2_bb.cc
new file mode 100644
index 0000000..c874f5e
--- /dev/null
+++ b/gnuradio-core/src/lib/general/gr_scrambler2_bb.cc
@@ -0,0 +1,56 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2008 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <gr_scrambler2_bb.h>
+#include <gr_io_signature.h>
+
+gr_scrambler2_bb_sptr
+gr_make_scrambler2_bb(int mask, int seed, int len)
+{
+  return gr_scrambler2_bb_sptr(new gr_scrambler2_bb(mask, seed, len));
+}
+
+gr_scrambler2_bb::gr_scrambler2_bb(int mask, int seed, int len)
+  : gr_sync_block("scrambler2_bb",
+		  gr_make_io_signature (1, 1, sizeof (unsigned char)),
+		  gr_make_io_signature (1, 1, sizeof (unsigned char))),
+    d_lfsr(mask, seed)
+{
+}
+
+int
+gr_scrambler2_bb::work(int noutput_items,
+		      gr_vector_const_void_star &input_items,
+		      gr_vector_void_star &output_items)
+{
+  const unsigned char *in = (const unsigned char *) input_items[0];
+  unsigned char *out = (unsigned char *) output_items[0];
+
+  for (int i = 0; i < noutput_items; i++)
+    out[i] = d_lfsr.next_bit_scramble(in[i]);
+  
+  return noutput_items;
+}
diff --git a/gnuradio-core/src/lib/general/gr_scrambler2_bb.h b/gnuradio-core/src/lib/general/gr_scrambler2_bb.h
new file mode 100644
index 0000000..a2e294a
--- /dev/null
+++ b/gnuradio-core/src/lib/general/gr_scrambler2_bb.h
@@ -0,0 +1,59 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2008 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+#ifndef INCLUDED_GR_SCRAMBLER2_BB_H
+#define INCLUDED_GR_SCRAMBLER2_BB_H
+
+#include <gr_sync_block.h>
+#include "gri_glfsr.h"
+
+class gr_scrambler2_bb;
+typedef boost::shared_ptr<gr_scrambler2_bb> gr_scrambler2_bb_sptr;
+
+gr_scrambler2_bb_sptr gr_make_scrambler2_bb(int mask, int seed, int len);
+
+/*!
+ * Scramble an input stream using an LFSR.  This block works on the LSB only
+ * of the input data stream, i.e., on an "unpacked binary" stream, and 
+ * produces the same format on its output.
+ * 
+ * \param mask     Polynomial mask for LFSR
+ * \param seed     Initial shift register contents
+ * \param len      Shift register length
+ *
+ * \ingroup misc
+ */
+
+class gr_scrambler2_bb : public gr_sync_block
+{
+  friend gr_scrambler2_bb_sptr gr_make_scrambler2_bb(int mask, int seed, int len);
+
+  gri_glfsr d_lfsr;
+
+  gr_scrambler2_bb(int mask, int seed, int len);
+
+public:
+  int work(int noutput_items,
+	   gr_vector_const_void_star &input_items,
+	   gr_vector_void_star &output_items);
+};
+
+#endif /* INCLUDED_GR_SCRAMBLER2_BB_H */
diff --git a/gnuradio-core/src/lib/general/gr_scrambler2_bb.i b/gnuradio-core/src/lib/general/gr_scrambler2_bb.i
new file mode 100644
index 0000000..861d410
--- /dev/null
+++ b/gnuradio-core/src/lib/general/gr_scrambler2_bb.i
@@ -0,0 +1,31 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2008 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+GR_SWIG_BLOCK_MAGIC(gr,scrambler2_bb);
+
+gr_scrambler2_bb_sptr gr_make_scrambler2_bb(int mask, int seed, int len);
+
+class gr_scrambler2_bb : public gr_sync_block
+{
+private:
+  gr_scrambler2_bb(int mask, int seed, int len);
+};
diff --git a/gnuradio-core/src/lib/general/gri_glfsr.h b/gnuradio-core/src/lib/general/gri_glfsr.h
index 7dd5f86..eb09515 100644
--- a/gnuradio-core/src/lib/general/gri_glfsr.h
+++ b/gnuradio-core/src/lib/general/gri_glfsr.h
@@ -49,7 +49,26 @@ class gri_glfsr
     return bit;
   }
 
+  unsigned char next_bit_scramble(unsigned char input) {
+    unsigned char bit = (input ^ d_shift_register) & 1;
+    d_shift_register >>= 1;
+    if (bit)
+      d_shift_register ^= d_mask;
+    return bit;
+  }
+
+  unsigned char next_bit_descramble(unsigned char input) {
+    unsigned char bit;
+    bit = (input ^ d_shift_register) & 1;
+    d_shift_register >>= 1;
+    if (input & 1)
+      d_shift_register ^= d_mask;
+    return bit;
+  }
+
   int mask() const { return d_mask; }
+
+  void set_seed(int seed) { d_shift_register = seed; }
 };
 
 #endif /* INCLUDED_GRI_GLFSR_H */
diff --git a/gnuradio-core/src/lib/general/qa_general.cc b/gnuradio-core/src/lib/general/qa_general.cc
index 6984d79..8bf6003 100644
--- a/gnuradio-core/src/lib/general/qa_general.cc
+++ b/gnuradio-core/src/lib/general/qa_general.cc
@@ -33,6 +33,7 @@
 #include <qa_gr_fxpt_vco.h>
 #include <qa_gr_math.h>
 #include <qa_gri_lfsr.h>
+#include <qa_gri_glfsr.h>
 
 CppUnit::TestSuite *
 qa_general::suite ()
@@ -46,6 +47,7 @@ qa_general::suite ()
   s->addTest (qa_gr_fxpt_vco::suite ());
   s->addTest (qa_gr_math::suite ());
   s->addTest (qa_gri_lfsr::suite ());
+  s->addTest (qa_gri_glfsr::suite ());
   
   return s;
 }
diff --git a/gnuradio-core/src/lib/general/qa_gri_glfsr.cc b/gnuradio-core/src/lib/general/qa_gri_glfsr.cc
new file mode 100644
index 0000000..3e9f1bd
--- /dev/null
+++ b/gnuradio-core/src/lib/general/qa_gri_glfsr.cc
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2008 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include <gri_glfsr.h>
+#include <qa_gri_glfsr.h>
+#include <cppunit/TestAssert.h>
+#include <stdio.h>
+#include <string.h>
+
+void
+qa_gri_glfsr::test_lfsr ()
+{
+  int mask = 0x12;
+  int seed = 0x01;
+
+  gri_glfsr lfsr1(mask,seed);
+  gri_glfsr lfsr2(mask,seed);
+  
+  unsigned char expected[] = {0, 0, 0, 1, 1, 1, 1, 1, 0, 0};
+
+  for(unsigned int i=0; i<31; i++){
+    lfsr1.next_bit();
+  }
+
+  // test that after one lfsr cycle we still match out uncycled lfsr
+  for (unsigned int i = 0; i < 41; i++) {
+    CPPUNIT_ASSERT_EQUAL((int) lfsr1.next_bit(), (int) lfsr2.next_bit());
+  }
+
+  // test the known correct values at the given shift offset
+  for(unsigned int i=0; i<10; i++){
+    CPPUNIT_ASSERT_EQUAL((int) lfsr1.next_bit(), (int) expected[i]);
+  }
+}
+
+void
+qa_gri_glfsr::test_scrambler()
+{
+  // CCSDS 7-bit scrambler
+  int mask = 0xC5;
+  int seed = 0x00;
+
+  gri_glfsr scrambler(mask, seed);
+
+  // Impulse (1 and 126 more zeroes)
+  unsigned char src[] = 
+    { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0 }; // flush bits
+  
+  // Impulse response (including leading bits)
+  unsigned char expected[] =
+	{ 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1,
+      1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0,
+      0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 
+      0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 
+      1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 
+      0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 
+      0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 
+      0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 
+      0, 0, 1, 1, 0, 0 };
+
+  int len = sizeof(src);
+  unsigned char actual[len];
+
+  for (int i = 0; i < len; i++)
+    actual[i] = scrambler.next_bit_scramble(src[i]);
+
+  CPPUNIT_ASSERT(memcmp(expected, actual, len) == 0);
+}
+
+void
+qa_gri_glfsr::test_descrambler()
+{
+  // CCSDS 7-bit scrambler
+  int mask = 0xC5;
+  int seed = 0x00;
+
+  gri_glfsr descrambler(mask, seed);
+
+  // Scrambled sequence (impulse response)
+  unsigned char src[] =
+	{ 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1,
+      1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0,
+      0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 
+      0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 
+      1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 
+      0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 
+      0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 
+      0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 
+      0, 0, 1, 1, 0, 0 };
+
+  // Original (impulse)
+  unsigned char expected[] = 
+    { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+      0, 0, 0, 0, 0, 0 };
+  
+  int len = sizeof(src);
+  unsigned char actual[len];
+
+  for (int i = 0; i < len; i++)
+    actual[i] = descrambler.next_bit_descramble(src[i]);
+
+  CPPUNIT_ASSERT(memcmp(expected, actual, len) == 0);
+}
diff --git a/gnuradio-core/src/lib/general/qa_gri_glfsr.h b/gnuradio-core/src/lib/general/qa_gri_glfsr.h
new file mode 100644
index 0000000..9912d46
--- /dev/null
+++ b/gnuradio-core/src/lib/general/qa_gri_glfsr.h
@@ -0,0 +1,42 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2008 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+#ifndef _QA_GRI_GLFSR_H_
+#define _QA_GRI_GLFSR_H_
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/TestCase.h>
+
+class qa_gri_glfsr : public CppUnit::TestCase {
+
+  CPPUNIT_TEST_SUITE(qa_gri_glfsr);
+  CPPUNIT_TEST(test_lfsr);
+  CPPUNIT_TEST(test_scrambler);
+  CPPUNIT_TEST(test_descrambler);
+  CPPUNIT_TEST_SUITE_END();
+
+ private:
+  void test_lfsr();
+  void test_scrambler();
+  void test_descrambler();
+};
+
+#endif /* _QA_GRI_GLFSR_H_ */
-- 
1.5.6

_______________________________________________
Patch-gnuradio mailing list
Patch-gnuradio@gnu.org
http://lists.gnu.org/mailman/listinfo/patch-gnuradio

Reply via email to