Algorithm Search⌘K

Definitioncore

Gold sequences

Also called Gold codes.

The 2^n + 1 binary sequences made from a preferred pair of m-sequences u and v: u, v and u plus every shift of v. All their periodic correlations take only the values -1, -t(n) and t(n) - 2.

Gold sequences are a family of 2n+12^n + 1 binary sequences of period N=2n−1N = 2^n - 1 whose periodic autocorrelations (off the peak) and cross-correlations all lie in the set {−1,−t(n),t(n)−2}\{-1, -t(n), t(n) - 2\}, with t(n)=1+2⌊(n+2)/2⌋t(n) = 1 + 2^{\lfloor (n+2)/2 \rfloor}. Robert Gold built them in 1967 by adding, bit by bit, one m-sequence uu to every cyclic shift of a second m-sequence vv of the same degree. The pair (u,v)(u, v) must be a preferred pair, one whose own cross-correlation is three-valued. A single m-sequence has a perfect two-level autocorrelation, but there are few m-sequences of each degree and two of them can correlate badly. Gold sequences give up the perfect autocorrelation and get about NN sequences whose largest correlation is near 2N\sqrt{2N} for odd nn. That is why they are the spreading codes of many CDMA systems and the C/A codes of GPS.

Definition

Write a binary sequence of period NN as s=(s0,…,sN−1)s = (s_0, \ldots, s_{N-1}), its cyclic shift by τ\tau as Tτs=(sτ,sτ+1,…)T^\tau s = (s_\tau, s_{\tau+1}, \ldots), with indices mod NN, and its decimation by qq as s[q]=(s0,sq,s2q,…)s[q] = (s_0, s_q, s_{2q}, \ldots). The periodic correlation of two bit sequences at shift τ\tau is

θa,b(τ)=∑k=0N−1(−1)ak⊕bk+τ=N−2 #{k:ak≠bk+τ},\theta_{a,b}(\tau) = \sum_{k=0}^{N-1} (-1)^{a_k \oplus b_{k+\tau}} = N - 2 \, \#\{k : a_k \ne b_{k+\tau}\},

the correlation of their ±1\pm 1 forms (bit 00 to +1+1, bit 11 to −1-1). For a family F\mathcal{F}, the figure of merit is

θmax⁡(F)=max⁡{∣θa,b(τ)∣:a,b∈F, (a,τ)≠(b,0)},\theta_{\max}(\mathcal{F}) = \max\{|\theta_{a,b}(\tau)| : a, b \in \mathcal{F}, \ (a, \tau) \ne (b, 0)\},

which covers the off-peak autocorrelations and all cross-correlations.

Preferred pair. Let n≢0(mod4)n \not\equiv 0 \pmod 4 and t(n)=1+2⌊(n+2)/2⌋t(n) = 1 + 2^{\lfloor (n+2)/2 \rfloor}, so t(n)=1+2(n+1)/2t(n) = 1 + 2^{(n+1)/2} for odd nn and 1+2(n+2)/21 + 2^{(n+2)/2} for even nn. Two m-sequences uu and vv of degree nn are a preferred pair if θu,v(τ)\theta_{u,v}(\tau) takes exactly the three values −1-1, −t(n)-t(n) and t(n)−2t(n) - 2. A standard way to get one: take v=u[q]v = u[q] with q=2k+1q = 2^k + 1 or q=22k−2k+1q = 2^{2k} - 2^k + 1, where gcd⁡(n,k)=1\gcd(n, k) = 1 for odd nn and gcd⁡(n,k)=2\gcd(n, k) = 2 for n≡2(mod4)n \equiv 2 \pmod 4 (Sarwate and Pursley 1980, the section on m-sequence cross-correlation). For nn divisible by 44 no preferred pair exists.

Gold family. For a preferred pair (u,v)(u, v) the Gold family is

G(u,v)={u, v}∪{u⊕Tτv:τ=0,1,…,N−1},\mathcal{G}(u, v) = \{u, \ v\} \cup \{u \oplus T^\tau v : \tau = 0, 1, \ldots, N - 1\},

with N+2=2n+1N + 2 = 2^n + 1 sequences of period NN. Gold's theorem (Gold 1967): every off-peak autocorrelation and every cross-correlation in G(u,v)\mathcal{G}(u, v) takes a value in {−1,−t(n),t(n)−2}\{-1, -t(n), t(n) - 2\}, so θmax⁡=t(n)\theta_{\max} = t(n). The reason is the shift-and-add property of m-sequences: the correlation of two members reduces to the cross-correlation of uu and vv at some shift, or to an autocorrelation value −1-1. Each member is a sum of two sequences with linear recurrences of degree nn, so it satisfies the recurrence of the product polynomial of degree 2n2n.

The construction and the computation of θmax⁡\theta_{\max} are below. Building the family costs O(N2)O(N^2) bit operations in the word RAM model; checking θmax⁡\theta_{\max} by direct sums over all pairs and shifts costs O(N4)O(N^4), which is fine for n≤6n \le 6.

  1. GOLD-FAMILY(u, v) ▷ u, v a preferred pair of period N
  2. 1F ← [u, v]
  3. 2for τ ← 0 to N - 1 do
  4. 3append u ⊕ T^τ v to F ▷ bitwise XOR with the shifted v
  5. return F
  6. THETA-MAX(F)
  7. for each pair a, b in F and each shift τ, except a = b with τ = 0 do
  8. 4θ ← N - 2 · #{k : a[k] ≠ b[(k + τ) mod N]}
  9. 5best ← max(best, |θ|)
  10. return best

Against the Welch bound. The Welch bound for MM sequences of length NN and energy NN gives θmax⁡≥N(M−1)/(MN−1)\theta_{\max} \ge N \sqrt{(M - 1)/(MN - 1)}, which is close to N\sqrt N for M=N+2M = N + 2. For odd nn, t(n)=1+2(N+1)t(n) = 1 + \sqrt{2(N+1)} is about 2\sqrt 2 times the bound; for n≡2(mod4)n \equiv 2 \pmod 4 it is about 22 times. The small set of Kasami sequences has a smaller θmax⁡\theta_{\max}, about N\sqrt N, but only N+1\sqrt{N + 1} sequences.

Examples and non-examples

Degree 5 (the check value). The m-sequences of uu: x5+x2+1x^5 + x^2 + 1 and vv: x5+x4+x3+x2+1x^5 + x^4 + x^3 + x^2 + 1, both started from 1000010000, are a preferred pair. In fact v=u[3]v = u[3], the decimation by q=21+1q = 2^1 + 1. Over the 3131 shifts, θu,v(τ)\theta_{u,v}(\tau) takes the value −9-9 six times, −1-1 fifteen times and 77 ten times. Here t(5)=1+23=9t(5) = 1 + 2^3 = 9, so the values are −t-t, −1-1 and t−2t - 2 as stated. The Gold family has 3333 distinct sequences of period 3131 and θmax⁡=9\theta_{\max} = 9. The Welch bound for M=33M = 33 and N=31N = 31 is 3132/1022=5.4931 \sqrt{32/1022} = 5.49, so the family is within a factor of 1.641.64 of it. In dB, the largest correlation is 20log⁡10(9/31)=−10.720 \log_{10}(9/31) = -10.7 dB below the peak.

Degree 6. uu from x6+x+1x^6 + x + 1 and v=u[5]v = u[5], which is the sequence of x6+x5+x2+x+1x^6 + x^5 + x^2 + x + 1, are a preferred pair with values −17-17, −1-1 and 1515, and t(6)=17t(6) = 17. Here gcd⁡(6,2)=2\gcd(6, 2) = 2 and q=22+1=5q = 2^2 + 1 = 5. The decimation by 33 does not work for n=6n = 6: gcd⁡(3,63)=3\gcd(3, 63) = 3, so u[3]u[3] is not an m-sequence of degree 6.

GPS C/A codes. The C/A code of each GPS satellite is a Gold code of period 10231023 from the two 10-cell registers with polynomials x10+x3+1x^{10} + x^3 + 1 and x10+x9+x8+x6+x3+x2+1x^{10} + x^9 + x^8 + x^6 + x^3 + x^2 + 1. Their cross-correlation over the 10231023 shifts takes the value −65-65 at 120120 shifts, −1-1 at 767767 shifts and 6363 at 136136 shifts, which matches t(10)=65t(10) = 65. Each satellite uses a different shift of the second register. The largest correlation is 20log⁡10(65/1023)=−23.920 \log_{10}(65/1023) = -23.9 dB below the peak, which is the margin that lets a receiver separate satellites with parallel code phase search.

Non-example: degree 4. Degree 4 has two primitive polynomials, x4+x+1x^4 + x + 1 and x4+x3+1x^4 + x^3 + 1. The cross-correlation of their m-sequences takes four values, −5-5, −1-1, 33 and 77, so they are not a preferred pair, and no other pair of degree 4 is.

Non-example: a wrong decimation. For n=5n = 5, u[15]u[15] is an m-sequence, but 15=24−115 = 2^4 - 1 is not of the form above, and θu,u[15]\theta_{u, u[15]} takes six values from −9-9 to 1111. Adding uu to the shifts of this sequence does not give a Gold family.

Non-example: m-sequences alone. A set of distinct m-sequences of one degree is not a good CDMA family in general: the degree 5 pair uu and u[15]u[15] above already reaches a correlation of 1111, worse than the Gold bound 99, and degree 5 has only six m-sequences.

  • Maximal-length sequences are the parts of a Gold family. Two of the N+2N + 2 members, uu and vv, are m-sequences; the others are not.
  • Kasami sequences: the small set has fewer sequences (2n/22^{n/2}) and a smaller θmax⁡\theta_{\max} (1+2n/21 + 2^{n/2}), and the large set contains a Gold family.
  • The Welch bound is the limit that θmax⁡\theta_{\max} is compared with.
  • Cross-correlation is the general signal processing notion; here it is periodic and on ±1\pm 1 sequences.
  • In code-division multiple access, θmax⁡/N\theta_{\max}/N sets the interference that one user leaks into the correlator of another, and multiuser detection removes what is left.

Code

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

gold_sequences.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/gold-sequences/. The authors are listed in prov/BGG18W.jsonl.

"""Gold sequences from a preferred pair of m-sequences, with integer correlations.

A polynomial over GF(2) is the list of its exponents, for example [5, 2, 0]
for x^5 + x^2 + 1. The m-sequence of f satisfies
s[k + n] = XOR over the exponents i < n of s[k + i].
The Gold family of a preferred pair (u, v) of period N = 2^n - 1 is
u, v and u XOR (v shifted by t) for t = 0, ..., N - 1: N + 2 sequences.
"""


def mseq(exps: list[int], state: list[int] | None = None) -> list[int]:
    """One period of the recurrence of f, from the state 1, 0, ..., 0 by default."""
    n = max(exps)
    s = list(state) if state is not None else [1] + [0] * (n - 1)
    taps = [i for i in exps if i < n]
    while len(s) < (1 << n) - 1:
        k = len(s) - n
        bit = 0
        for i in taps:
            bit ^= s[k + i]
        s.append(bit)
    return s


def decimate(s: list[int], q: int) -> list[int]:
    """The sequence s[q k mod N], k = 0, ..., N - 1."""
    n = len(s)
    return [s[(q * k) % n] for k in range(n)]


def shift(s: list[int], t: int) -> list[int]:
    """The cyclic shift s[(k + t) mod N]."""
    n = len(s)
    return [s[(k + t) % n] for k in range(n)]


def gold_family(u: list[int], v: list[int]) -> list[list[int]]:
    """The N + 2 sequences u, v and u XOR shift(v, t) for t = 0, ..., N - 1 (as bits)."""
    n = len(u)
    if len(v) != n:
        raise ValueError("lengths differ")
    fam = [list(u), list(v)]  # @line L1
    for t in range(n):  # @line L2
        fam.append([u[k] ^ v[(k + t) % n] for k in range(n)])  # @line L3
    return fam


def xcorr(a: list[int], b: list[int], t: int) -> int:
    """Periodic correlation sum over k of (-1)^(a[k] XOR b[k + t]) for two bit sequences."""
    n = len(a)
    return n - 2 * sum(a[k] ^ b[(k + t) % n] for k in range(n))  # @line L4


def correlation_values(a: list[int], b: list[int]) -> set[int]:
    """The set of values of the periodic cross-correlation of a and b over all shifts."""
    return {xcorr(a, b, t) for t in range(len(a))}


def max_correlation(fam: list[list[int]]) -> int:
    """theta_max: the largest |correlation| over all pairs and shifts, except the peaks at shift 0 of a with a."""
    best = 0
    for i, a in enumerate(fam):
        for j in range(i, len(fam)):
            b = fam[j]
            for t in range(len(a)):
                if i == j and t == 0:
                    continue
                best = max(best, abs(xcorr(a, b, t)))  # @line L5
    return best


def gold_t(n: int) -> int:
    """t(n) = 1 + 2^floor((n + 2) / 2)."""
    return 1 + (1 << ((n + 2) // 2))


def is_preferred_pair(u: list[int], v: list[int], n: int) -> bool:
    """True if the cross-correlation of u and v takes exactly the values -1, -t(n) and t(n) - 2."""
    t = gold_t(n)
    return correlation_values(u, v) == {-1, -t, t - 2}
test_gold_sequences.py 93 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/gold-sequences/. The authors are listed in prov/BGG18W.jsonl.

from collections import Counter

from gold_sequences import (
    correlation_values,
    decimate,
    gold_family,
    gold_t,
    is_preferred_pair,
    max_correlation,
    mseq,
    shift,
    xcorr,
)


def brute_xcorr(a: list[int], b: list[int], t: int) -> int:
    pa = [1 - 2 * x for x in a]
    pb = [1 - 2 * x for x in b]
    n = len(a)
    return sum(pa[k] * pb[(k + t) % n] for k in range(n))


def test_check_value_degree_five() -> None:
    u = mseq([5, 2, 0])
    v = mseq([5, 4, 3, 2, 0])
    counts = Counter(xcorr(u, v, t) for t in range(31))
    assert counts == {-9: 6, -1: 15, 7: 10}
    assert gold_t(5) == 9
    assert is_preferred_pair(u, v, 5)


def test_xcorr_matches_plus_minus_one_sum() -> None:
    u = mseq([5, 2, 0])
    v = mseq([5, 4, 3, 2, 0])
    for t in range(31):
        assert xcorr(u, v, t) == brute_xcorr(u, v, t)


def test_second_sequence_is_a_decimation() -> None:
    u = mseq([5, 2, 0])
    assert decimate(u, 3) == mseq([5, 4, 3, 2, 0])


def test_family_size_and_bound_degree_five() -> None:
    fam = gold_family(mseq([5, 2, 0]), mseq([5, 4, 3, 2, 0]))
    assert len(fam) == 33
    assert len({tuple(s) for s in fam}) == 33
    assert max_correlation(fam) == gold_t(5)
    # every value of every correlation is -1, -t or t - 2 (except the peaks)
    allowed = {-1, -9, 7}
    for i in (0, 1, 2, 17, 32):
        for j in (0, 1, 5, 20, 31):
            for t in range(31):
                if i == j and t == 0:
                    continue
                assert xcorr(fam[i], fam[j], t) in allowed


def test_family_degree_six() -> None:
    u = mseq([6, 1, 0])
    v = mseq([6, 5, 2, 1, 0])
    assert decimate(u, 5) == v
    assert correlation_values(u, v) == {-17, -1, 15}
    fam = gold_family(u, v)
    assert len(fam) == 65 and max_correlation(fam) == 17 == gold_t(6)


def test_no_preferred_pair_for_degree_four() -> None:
    # the two primitive polynomials of degree 4 give a four-valued cross-correlation
    u = mseq([4, 1, 0])
    v = mseq([4, 3, 0])
    vals = correlation_values(u, v)
    assert vals == {-5, -1, 3, 7}
    assert not is_preferred_pair(u, v, 4)
    for q in (7,):
        assert not is_preferred_pair(u, decimate(u, q), 4)


def test_members_have_full_period_and_three_valued_autocorrelation() -> None:
    u = mseq([5, 2, 0])
    v = mseq([5, 4, 3, 2, 0])
    fam = gold_family(u, v)
    for s in fam[2:]:
        assert all(s != shift(s, t) for t in range(1, 31))
        auto = {xcorr(s, s, t) for t in range(1, 31)}
        assert auto <= {-1, -9, 7}


def test_t_values() -> None:
    assert [gold_t(n) for n in range(3, 11)] == [5, 9, 9, 17, 17, 33, 33, 65]

References

  1. Robert Gold, Optimal binary sequences for spread spectrum multiplexing, IEEE Transactions on Information Theory 13(4), 1967, pp. 619 to 621 (the sum of a preferred pair of maximal-length sequences and the three-valued cross-correlation bound t(n))
  2. Dilip V. Sarwate and Michael B. Pursley, Crosscorrelation properties of pseudorandom and related sequences, Proceedings of the IEEE 68(5), 1980, pp. 593 to 619 (the sections on the cross-correlation of m-sequences, preferred pairs and Gold sequences)
  3. Kai Borre, Dennis M. Akos, Nicolaj Bertelsen, Peter Rinder and Søren Holdt Jensen, A Software-Defined GPS and Galileo Receiver, a Single-Frequency Approach, Birkhäuser, 2007, The chapter on the GPS signal (the C/A codes as Gold codes from two 10-cell shift registers)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/BGG18W/text.yaml