Thursday, December 11, 2008

Maximal length sequences (MLS or m-sequences) are useful for system identification and digital communication. For example if a MLS is used to excite the input to a linear time-invariant (LTI) system, the system's impulse response can be determined from the cross correlation of the input and output. The impulse response can be useful in its own right or can be windowed to obtain a quasi-anechoic frequency response.

This code is written for an AVR ATMEGA8, which is an easy-to-use, low-cost microcontroller, but should work with others. It outputs the MLS on pin 18 (PB4). A clock signal is output on pin 19 (PB5), transitioning high on each value. Pin 17 (PB3) is a synchronizing signal, going high for one sample at the start of each loop of the sequence.

The MLS is generated by a 16-bit linear feedback shift register. The "feedback" variable can be changed to other values to generate other sequences. See http://www.ece.cmu.edu/~koopman/lfsr/index.html for other values.

The code is written for gcc with -O3 optimization and the loop runs in a minimum 19 clock cycles if the delay function is removed. The "NOP" (No OPeration) lines must be tuned if you use a different compiler, to ensure that each branch of each "if" statement takes the same number of cycles.

"N_delay" can be used to change the loop's speed. It is currently set for a loop of 73 cycles or approx 219 kHz for a 16 MHz clock.

Download from here. You have to sign up (free), but if you're interested in AVR microcontrollers you should be a member anyway.