View Single Post
  #9 (permalink)  
Old 07-05-2009, 11:16 PM
robert bristow-johnson
Guest
 
Posts: n/a
Default Re: Generating Maximum Length Sequence using Galois LFSR


On Jul 4, 8:02 pm, spop...@speedymail.org (Steve Pope) wrote:
> robert bristow-johnson <r...@audioimagination.com> wrote:
>
> >On Jul 4, 12:55 am, Nicholas Kinar <n.ki...@usask.ca> wrote:
> >> for(int i = 0; i < L; i++)
> >> lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & taps);

> >i dunno what the minus sign is for. i don't think it belongs there.

>
> Looks right to me. Read the code again.


yeah, you're right. the code is correct. i almost went and changed
it at Wikipedia, but thought better.



On Jul 5, 10:26 am, Nicholas Kinar <n.ki...@usask.ca> wrote:
>
>
> > your L "elements" are single bits. the "S" of MLS is a sequence of
> > *bits*, not of words.

>
> > pick any bit of the shift register (your "lfsr") and use it. i
> > usually pick the same bit that falls off the edge


i forgot to mention that to get a virtually zero-mean MLS signal for
the purposes of measuring impulse response (or just for some noise
that is guaranteed to be "white" in some sense of the word), then you
take that bit, (we'll call it a[n] where n is discrete time) which is
0 or 1 and create this signed MLS signal:

x[n] = (-1)^a[n]

so, it's nearly zero mean. there are L/2 ones and L/2-1 zeros in a[n]
(because the zero state in the shift register is the only state
missing and if it were included, there would be an equal L/2 ones and
zeros, but it's not). then there is one more -1 than +1 in x[n] over
L samples. the mean is -1/L

this way, when you autocorrelate, you get


Rx[k] = MEAN{ x[n] * x[n+k] }

( "*" means simple multiplication.)

which is

Rx[k] = MEAN{ (-1)^a[n] * (-1)^a[n+k] }

= MEAN{ (-1)^(a[n] + a[n+k]) }

which happens to be the same as

Rx[k] = MEAN{ (-1)^(a[n] XOR a[n+k]) }


now it turns out that if you take that periodic MLS bit sequence, copy
it, circularly rotate one of the copies by k samples, and XOR the two
MLSes together, it turns out that you can show that the same recursive
generating algorithm that produces the MLS also governs that XOR
product of two with a lag in between. except, we know for MLS, the
zero state gets mapped back to the zero state. for an MLS, those are
the two loops; zero goes straight to zero, but any non-zero state of
the shift register goes through the whole MLS, which are all the other
non-zero states, before it gets back to your original non-zero state.
so that MEAN above for k not a multiple of L (nor zero), is -1/L. but
the mean for k=0 or any other integer multiple of L is the mean of a
sequence of ones.


> > you need to select your taps word correctly. first of all, for m=12,
> > then taps > 2048, but there is more to it. somewhere on the web,
> > there is a resource of "primitive polynomials". find that and use one
> > of them (ignore the least significant bit, which is always 1, the
> > other m bits of the primitive polynomial are the m bits of your taps
> > word). for m=12, you could write a program that guesses at a 12-bit
> > word, try it out (see if it goes through all 2^12 - 1 non-zero
> > states), and if it fails, try another word for taps.

>
> There are a few resources on "primitive polynomials" currently on the
> web, but perhaps you are thinking about the following paper?
>
> Stahnke, W. 1973. "Primitive Binary Polynomials," Mathematics of
> Computation 27(124): 977-980.
>
> The paper gives a table of exponents of terms of primitive binary
> polynomial terms up to m = 168, where the length of the sequence is L =
> (2^m) - 1. I've tested this table up to m = 30, and it seems to
> generate an MLS.


right, and you will see m+1 "coefficients" (that are either 0 or 1) in
the primitive polynomial. but the two end bits are always 1 and the
bit-reverse sequence can be shown to also be a primitive polynomial.
so, depending on which direction you're shifting, you leave off one of
the bits. you were shifting right so the "1" bit in the left is kept,
and you use the m most-significant bits on your word "taps" (right
justified).


> >> The loop should repeat [after] 4095 times.

>
> > yes. that's how you confirm it's MLS. start with a state of 0x0001
> > and count how many iterations it takes to get back to 0x0001. if it's
> > 4095 and your "taps" word has zeros in the bits other than the least-
> > significant 12 bits, then it's an MLS

>
> You know, that's an interesting way of checking whether something is an MLS.


it's similar to a recent experience i had where i missed that the
simplest and obvious way to display the step response of a filter (IIR
or FIR), assuming you know the coefficients, is to simple bang a step
input to a simulated filter with the same coefficients. somebody at
music-dsp had to point that out to me, where i was thinking of doing
Heaviside partial fraction expansion of the transfer function times 1/
(z-1), inverse Z transform, and getting it from there.


> Thanks, Robert.


FWIW

BTW, if you Google "Maximum Length Sequence" and look at the link
right below the one you referred to, there is a Little MLS Tutorial
that explains the math behind most of this. it doesn't derive
primitive polynomials, but does prove how MLS can get your time-
aliased impulse response from where you can get your frequency
response.



--

r b-j [email protected]

"Imagination is more important than knowledge."




Reply With Quote