Algorithm Search⌘K

Techniqueintro

Direct-sequence spread spectrum

Also called DSSS, direct-sequence spreading.

Multiply each data symbol by a fast pseudorandom chip code, so that the signal fills a wide band. Correlating with the same code restores the symbol and spreads out interference.

Direct-sequence spread spectrum (DSSS) sends each data symbol as a block of NN short pulses, the chips, whose signs follow a pseudorandom code known to the receiver. The chip rate is NN times the symbol rate, so the signal takes about NN times the bandwidth that the data needs. The receiver multiplies the received chips by the same code and adds them up over each symbol. This despreading collapses the wanted signal back into one strong value, while noise, narrowband interference and the signals of other codes stay spread out and add up only weakly. The factor by which the wanted signal gains over the interference is the processing gain, about NN. DSSS is the basis of CDMA, of GPS and of the first Wi-Fi rates.

In plain words

Each data bit is sent as a long, fast pattern of plus and minus pulses, chosen to look random. The receiver knows the pattern. It multiplies what it hears by the pattern, pulse by pulse, and adds up the results.

For the wanted signal every product has the same sign, so the sum grows large. For noise, for a steady tone, or for a user with another pattern, the products have mixed signs and mostly cancel. So the wanted bit stands out even when each single pulse is buried under the interference.

The cost is bandwidth: the fast pattern takes up a much wider band than the data alone. The receiver must also line up its copy of the pattern with the received one to within a fraction of a pulse, before it can decode anything.

When it applies

  • There is spare bandwidth, and a strong narrowband interferer, a jammer, or many other users must share the band.
  • The receiver can find and follow the code timing: an acquisition step finds it to within about one chip, and a tracking loop keeps it there.
  • Multipath delays are longer than one chip, so a rake receiver can separate the paths and add them.
  • Several users share the band at the same time, each with its own code, and their powers can be controlled. Without power control, a near user can swamp a far one; see near-far problem.
  • The signal must be hard to detect or to read without the code: its power density can be below the noise floor.

The template

Let c=(c0,…,cN−1)c = (c_0, \ldots, c_{N-1}) be a code of ±1\pm 1 chips, for example one period of an m-sequence, and let di∈{+1,−1}d_i \in \{+1, -1\} be the data symbols. The transmitter sends the chips x[iN+n]=dicnx[iN + n] = d_i c_n. The receiver gets $r = x + $ interference and computes, for each symbol, the correlation with the code, which is a matched filter for one symbol:

zi=∑n=0N−1r[iN+n] cn=Ndi+∑n=0N−1e[iN+n] cn,z_i = \sum_{n=0}^{N-1} r[iN + n] \, c_n = N d_i + \sum_{n=0}^{N-1} e[iN + n] \, c_n,

where ee is the interference. With white noise of variance σ2\sigma^2 per chip, the signal-to-noise ratio is 1/σ21/\sigma^2 per chip and N2/(Nσ2)=N/σ2N^2 / (N \sigma^2) = N/\sigma^2 after despreading, a gain of NN.

  1. SPREAD(d, c) ▷ d data symbols ±1, c code of N chips ±1
  2. 1for each symbol d_i do
  3. 2send d_i c_0, d_i c_1, …, d_i c_{N-1}
  4. DESPREAD(r, c)
  5. 3for each block i do z_i ← Σ_{n=0}^{N-1} r[iN + n] c_n
  6. 4return d̂_i ← sign(z_i) for each i

Spreading and despreading cost O(N)O(N) operations per symbol. On a real link the chips are shaped pulses on a carrier, and the receiver must also find the code phase and the carrier before L3; see serial-search code acquisition, parallel code phase search and the delay-locked loop.

Classic examples

  • Length 7. With the m-sequence 10010111001011 of x3+x+1x^3 + x + 1 and the mapping 0→+10 \to +1, 1→−11 \to -1, the code is (−1,1,1,−1,1,−1,−1)(-1, 1, 1, -1, 1, -1, -1). The symbols (+1,−1)(+1, -1) become 1414 chips, and despreading gives exactly 77 and −7-7.
  • A constant jammer. The chips of an m-sequence of length NN add up to −1-1, since the sequence has one more 1 than 0. A constant interferer of amplitude AA therefore adds exactly −A-A to each correlator output, against ±N\pm N for the symbol. With N=127N = 127 and A=40A = 40, the jammer is 32.0432.04 dB stronger than the signal on each chip, but the outputs are 8787 and −167-167, and every decision is right.
  • White noise. With N=31N = 31 and noise variance 44 per chip, the signal-to-noise ratio per chip is −6.02-6.02 dB. After despreading it is 31/431/4, or 8.898.89 dB.
  • Two users. User 1 uses an m-sequence of length 3131, and user 2 uses a cyclic shift of it. Two different shifts of an m-sequence have periodic cross-correlation −1-1, so user 1 sees exactly 31d1−d231 d_1 - d_2. With Gold sequences the cross-talk is larger but bounded, and there are many more codes.
  • GPS C/A code. Each satellite spreads 5050 bit/s of navigation data with its own Gold code of 10231023 chips at 1.0231.023 Mchip/s, so one bit spans 2020 code periods and 2046020460 chips.
  • IS-95 and WCDMA. IS-95 spreads at 1.22881.2288 Mchip/s and separates the downlink users with Walsh codes of length 6464. WCDMA spreads at 3.843.84 Mchip/s with OVSF codes and a long scrambling code.

Variations

  • Short and long codes. A short code repeats every symbol, which makes acquisition fast and allows multiuser detection. A long code spans many symbols and makes the interference look more like noise.
  • Spreading with complex codes. Two independent codes on the in-phase and quadrature branches (QPSK spreading) make the interference less dependent on carrier phase.
  • Code-shift keying. Each group of kk bits selects one of 2k2^k codes; the receiver correlates with all of them and picks the largest. IEEE 802.11b at 5.5 and 11 Mbit/s uses a related scheme, complementary code keying.
  • Orthogonal codes in a synchronous downlink. When all signals leave one transmitter at the same time, orthogonal codes such as OVSF codes remove the cross-talk on a single path.
  • Frequency hopping. The other main way to spread is to hop the carrier; see frequency-hopping spread spectrum.

Pitfalls

  • The gain is not free against white noise. Spreading does not change the energy per bit to noise density ratio, so the bit error rate on an AWGN channel is the same as without spreading. The gain is against interference whose power is fixed and does not grow with the bandwidth.
  • The code timing must be known. An error of one chip or more makes the correlation fall to the off-peak level. Despreading only works after acquisition and with tracking.
  • Near-far. The cross-talk from another user scales with its amplitude. A user near the base station can swamp a far user, unless the transmit powers are controlled.
  • Partial correlations. In an asynchronous system, one symbol of the other user may change sign in the middle of the correlation window. The cross-talk is then an odd or partial correlation, not the periodic correlation, and it can be larger.
  • Bandwidth. The signal needs NN times the bandwidth of the data. If the band is the limit and there is no interference to fight, DSSS lowers the spectral efficiency of a single link.

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.

direct_sequence_spread_spectrum.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/direct-sequence-spread-spectrum/. The authors are listed in prov/5PQ8PD.jsonl.

"""Direct-sequence spreading and despreading with a +1/-1 chip code.

Each symbol d[i] in {+1, -1} is sent as d[i] c[0], ..., d[i] c[N - 1], where c
is a chip code of length N (here usually one period of an m-sequence). The
receiver correlates each block of N chips with c and takes the sign.
"""


def mseq_chips(exps: list[int]) -> list[int]:
    """One period of an m-sequence as chips: bit 0 -> +1 and bit 1 -> -1.

    The polynomial is the list of its exponents, for example [3, 1, 0] for
    x^3 + x + 1, and s[k + n] = XOR over the exponents i < n of s[k + i].
    """
    n = max(exps)
    s = [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 [1 - 2 * b for b in s]


def spread(symbols: list[int], code: list[int]) -> list[int]:
    """The chip stream: each symbol times the whole code."""
    chips = []
    for d in symbols:  # @line L1
        chips.extend(d * c for c in code)  # @line L2
    return chips


def despread(received: list[float], code: list[int]) -> list[float]:
    """The correlator outputs z[i] = sum over n of r[i N + n] c[n]."""
    n = len(code)
    if len(received) % n:
        raise ValueError("length must be a multiple of the code length")
    return [sum(received[i + j] * code[j] for j in range(n)) for i in range(0, len(received), n)]  # @line L3


def decide(z: list[float]) -> list[int]:
    """Hard decisions: +1 if z >= 0, else -1."""
    return [1 if v >= 0 else -1 for v in z]  # @line L4
test_direct_sequence_spread_spectrum.py 66 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/direct-sequence-spread-spectrum/. The authors are listed in prov/5PQ8PD.jsonl.

import random

from direct_sequence_spread_spectrum import decide, despread, mseq_chips, spread

POLYS = {3: [3, 1, 0], 5: [5, 2, 0], 7: [7, 1, 0]}


def test_check_value_degree_three() -> None:
    c = mseq_chips(POLYS[3])
    assert c == [-1, 1, 1, -1, 1, -1, -1]  # bits 1001011
    assert spread([1, -1], c) == c + [-v for v in c]
    assert despread(spread([1, -1], c), c) == [7, -7]


def test_round_trip_without_noise() -> None:
    rng = random.Random(1)
    for poly in POLYS.values():
        c = mseq_chips(poly)
        d = [rng.choice((-1, 1)) for _ in range(50)]
        z = despread(spread(d, c), c)
        assert z == [len(c) * v for v in d]
        assert decide(z) == d


def test_constant_jammer_is_reduced_to_minus_its_amplitude() -> None:
    # The chips of an m-sequence sum to -1, so a constant A adds exactly -A per symbol.
    rng = random.Random(2)
    for poly in POLYS.values():
        c = mseq_chips(poly)
        n = len(c)
        assert sum(c) == -1
        d = [rng.choice((-1, 1)) for _ in range(20)]
        for a in (1, 5, 40):
            r = [x + a for x in spread(d, c)]
            assert despread(r, c) == [n * v - a for v in d]


def test_second_user_on_a_shifted_code() -> None:
    # Two shifts of one m-sequence have periodic cross-correlation -1.
    rng = random.Random(3)
    c = mseq_chips(POLYS[5])
    n = len(c)
    for tau in (1, 7, 30):
        c2 = c[tau:] + c[:tau]
        d1 = [rng.choice((-1, 1)) for _ in range(30)]
        d2 = [rng.choice((-1, 1)) for _ in range(30)]
        r = [x + y for x, y in zip(spread(d1, c), spread(d2, c2))]
        assert despread(r, c) == [n * a - b for a, b in zip(d1, d2)]


def test_white_noise_gain_equals_code_length() -> None:
    # Per chip: signal 1, noise variance s2. After despreading: signal N^2, noise N s2.
    rng = random.Random(4)
    c = mseq_chips(POLYS[5])
    n = len(c)
    s2 = 4.0
    count = 4000
    d = [rng.choice((-1, 1)) for _ in range(count)]
    r = [x + rng.gauss(0.0, s2 ** 0.5) for x in spread(d, c)]
    z = despread(r, c)
    noise = [zi - n * di for zi, di in zip(z, d)]
    var = sum(v * v for v in noise) / count
    assert abs(var / (n * s2) - 1) < 0.1

Exercises

  1. Show that for an m-sequence of length NN and a constant interferer AA, the correlator output is Ndi−AN d_i - A exactly. How large can AA be before a symbol of −1-1 is decided wrongly?
  2. Two users share the band with codes cc and c′c' and periodic cross-correlation θ\theta. Write the output of the correlator for user 1 as a function of the two symbols, and find the largest ∣θ∣|\theta| that still allows error-free decisions without noise.
  3. With white noise of variance σ2\sigma^2 per chip, show that the variance of zi−Ndiz_i - N d_i is Nσ2N \sigma^2, for any ±1\pm 1 code.

References

  1. Raymond L. Pickholtz, Donald L. Schilling and Laurence B. Milstein, Theory of spread-spectrum communications, a tutorial, IEEE Transactions on Communications 30(5), 1982, pp. 855 to 884 (direct-sequence spreading, processing gain, jamming margin, code acquisition and tracking)
  2. Andrew J. Viterbi, CDMA, Principles of Spread Spectrum Communication, Addison-Wesley, 1995, The opening chapters on direct-sequence spreading and its use for multiple access
  3. John G. Proakis and Masoud Salehi, Digital Communications, 5th edition, McGraw-Hill, 2008, The chapter on spread spectrum signals for digital communications

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)
WritingW3 standard
  • five exercises