bundles / scipy 1.17.1 / scipy / signal / _max_len_seq / max_len_seq
function
scipy.signal._max_len_seq:max_len_seq
Signature
def max_len_seq ( nbits , state = None , length = None , taps = None ) Summary
Maximum length sequence (MLS) generator.
Parameters
nbits: intNumber of bits to use. Length of the resulting sequence will be
(2**nbits) - 1. Note that generating long sequences (e.g., greater thannbits == 16) can take a long time.state: array_like, optionalIf array, must be of length
nbits, and will be cast to binary (bool) representation. If None, a seed of ones will be used, producing a repeatable representation. Ifstateis all zeros, an error is raised as this is invalid. Default: None.length: int, optionalNumber of samples to compute. If None, the entire length
(2**nbits) - 1is computed.taps: array_like, optionalPolynomial taps to use (e.g.,
[7, 6, 1]for an 8-bit sequence). If None, taps will be automatically selected (for up tonbits == 32).
Returns
seq: arrayResulting MLS sequence of 0's and 1's.
state: arrayThe final state of the shift register.
Notes
The algorithm for MLS generation is generically described in:
https://en.wikipedia.org/wiki/Maximum_length_sequence
The default values for taps are specifically taken from the first option listed for each value of nbits in:
https://web.archive.org/web/20181001062252/http://www.newwaveinstruments.com/resources/articles/m_sequence_linear_feedback_shift_register_lfsr.htm
Array API Standard Support
max_len_seq has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ⛔ PyTorch ⛔ ⛔ JAX ⛔ ⛔ Dask ⛔ n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
MLS uses binary convention:from scipy.signal import max_len_seq max_len_seq(4)[0]✓
import numpy as np import matplotlib.pyplot as plt from numpy.fft import fft, ifft, fftshift, fftfreq seq = max_len_seq(6)[0]*2-1 # +1 and -1 spec = fft(seq) N = len(seq)✓
plt.plot(fftshift(fftfreq(N)), fftshift(np.abs(spec)), '.-')
✗plt.margins(0.1, 0.1) plt.grid(True) plt.show()✓

acorrcirc = ifft(spec * np.conj(spec)).real
✓plt.figure() plt.plot(np.arange(-N/2+1, N/2+1), fftshift(acorrcirc), '.-')✗
plt.margins(0.1, 0.1) plt.grid(True) plt.show()✓

acorr = np.correlate(seq, seq, 'full')
✓plt.figure() plt.plot(np.arange(-N+1, N), acorr, '.-')✗
plt.margins(0.1, 0.1) plt.grid(True) plt.show()✓

Aliases
-
scipy.signal.max_len_seq