X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=gnuradio-core%2Fsrc%2Flib%2Fgeneral%2Fgri_lfsr.h;fp=gnuradio-core%2Fsrc%2Flib%2Fgeneral%2Fgri_lfsr.h;h=715da78a94d8fc0f0aff080a5c26b92bf09c93c0;hb=ea29b08aeb54227e6628f655ccfdb96fe4d8c378;hp=0000000000000000000000000000000000000000;hpb=09a1e803a9e6587c78d20cdf16891e5295874668;p=debian%2Fgnuradio diff --git a/gnuradio-core/src/lib/general/gri_lfsr.h b/gnuradio-core/src/lib/general/gri_lfsr.h new file mode 100644 index 00000000..715da78a --- /dev/null +++ b/gnuradio-core/src/lib/general/gri_lfsr.h @@ -0,0 +1,143 @@ +/* -*- 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_GRI_LFSR_H +#define INCLUDED_GRI_LFSR_H + +#include +#include + +/*! + * \brief Fibonacci Linear Feedback Shift Register using specified polynomial mask + * \ingroup misc + * + * Generates a maximal length pseudo-random sequence of length 2^degree-1 + * + * Constructor: gri_lfsr(int mask, int seed, int reg_len); + * + * mask - polynomial coefficients representing the locations + * of feedback taps from a shift register which are xor'ed + * together to form the new high order bit. + * + * Some common masks might be: + * x^4 + x^3 + x^0 = 0x19 + * x^5 + x^3 + x^0 = 0x29 + * x^6 + x^5 + x^0 = 0x61 + * + * seed - the initialization vector placed into the register + * durring initialization. Low order bit corresponds + * to x^0 coefficient -- the first to be shifted as output. + * + * reg_len - specifies the length of the feedback shift register + * to be used. Durring each iteration, the register + * is rightshifted one and the new bit is placed in bit reg_len. + * reg_len should generally be at least order(mask) + 1 + * + * + * see http://en.wikipedia.org/wiki/Linear_feedback_shift_register + * for more explanation. + * + * + * + * next_bit() - Standard LFSR operation + * + * Perform one cycle of the LFSR. The output bit is taken from + * the shift register LSB. The shift register MSB is assigned from + * the modulo 2 sum of the masked shift register. + * + * next_bit_scramble(unsigned char input) - Scramble an input stream + * + * Perform one cycle of the LFSR. The output bit is taken from + * the shift register LSB. The shift register MSB is assigned from + * the modulo 2 sum of the masked shift register and the input LSB. + * + * next_bit_descramble(unsigned char input) - Descramble an input stream + * + * Perform one cycle of the LFSR. The output bit is taken from + * the modulo 2 sum of the masked shift register and the input LSB. + * The shift register MSB is assigned from the LSB of the input. + * + * See http://en.wikipedia.org/wiki/Scrambler for operation of these + * last two functions (see multiplicative scrambler.) + * + */ + +class gri_lfsr +{ + private: + uint32_t d_shift_register; + uint32_t d_mask; + uint32_t d_shift_register_length; // less than 32 + + static uint32_t + popCount(uint32_t x) + { + uint32_t r = x - ((x >> 1) & 033333333333) + - ((x >> 2) & 011111111111); + return ((r + (r >> 3)) & 030707070707) % 63; + } + + public: + + gri_lfsr(uint32_t mask, uint32_t seed, uint32_t reg_len) + : d_shift_register(seed), d_mask(mask), d_shift_register_length(reg_len) + { + if (reg_len > 31) + throw std::invalid_argument("reg_len must be <= 31"); + } + + unsigned char next_bit() { + unsigned char output = d_shift_register & 1; + unsigned char newbit = popCount( d_shift_register & d_mask )%2; + d_shift_register = ((d_shift_register>>1) | (newbit<>1) | (newbit<>1) | (newbit<