Algorithm Search⌘K

Definitioncore

OVSF codes

Also called orthogonal variable spreading factor codes.

A binary tree of Walsh codes of lengths 1, 2, 4, ... Two codes stay orthogonal at different rates exactly when neither is an ancestor of the other in the tree.

OVSF codes are ±1\pm 1 spreading codes of lengths 1,2,4,8,…1, 2, 4, 8, \ldots, placed on a binary tree. The codes on one level are the rows of a Sylvester Hadamard matrix, so codes of the same length are orthogonal. A code of length SFSF spreads one symbol over SFSF chips, so at a fixed chip rate a short code carries a high symbol rate and a long code carries a low one. Codes of different lengths stay orthogonal for any data if and only if neither code is an ancestor of the other in the tree. This lets a CDMA downlink give users different rates on one chip stream without interference between them. Adachi, Sawahashi and Okawa described the tree in 1997, and WCDMA (UMTS) uses it for its channelization codes.

Definition

Let SFSF be a power of two, the spreading factor, and 0≤k<SF0 \le k < SF. The code CSF,kC_{SF,k} is a vector of SFSF values in {+1,−1}\{+1, -1\}, defined by

C1,0=(1),C2N,2k=(CN,k, CN,k),C2N,2k+1=(CN,k, −CN,k).C_{1,0} = (1), \qquad C_{2N,2k} = (C_{N,k}, \ C_{N,k}), \qquad C_{2N,2k+1} = (C_{N,k}, \ -C_{N,k}).

This is the numbering of 3GPP TS 25.213. The parent of CSF,kC_{SF,k} is CSF/2,⌊k/2⌋C_{SF/2, \lfloor k/2 \rfloor}, and its two children are C2SF,2kC_{2SF, 2k} and C2SF,2k+1C_{2SF, 2k+1}. A code CSF1,k1C_{SF_1,k_1} is an ancestor of CSF2,k2C_{SF_2,k_2} (with SF1≤SF2SF_1 \le SF_2) if it lies on the path from the root to CSF2,k2C_{SF_2,k_2}, that is, if ⌊k2/(SF2/SF1)⌋=k1\lfloor k_2 / (SF_2/SF_1) \rfloor = k_1.

Same length. The codes CSF,0,…,CSF,SF−1C_{SF,0}, \ldots, C_{SF,SF-1} are the rows of the Sylvester-Hadamard matrix of order SFSF, in bit-reversed order: CSF,kC_{SF,k} is the row with index rev(k)\mathrm{rev}(k), the bits of kk reversed. So ⟨CSF,i,CSF,k⟩=SF\langle C_{SF,i}, C_{SF,k}\rangle = SF if i=ki = k and 00 otherwise.

Different lengths. Let SF1<SF2SF_1 < SF_2. Over the time of one symbol of the long code, the short code sends SF2/SF1SF_2/SF_1 symbols d1,d2,…d_1, d_2, \ldots, each spread by CSF1,k1C_{SF_1,k_1}. Split CSF2,k2C_{SF_2,k_2} into SF2/SF1SF_2/SF_1 blocks of length SF1SF_1. By the recursion, each block is ±CSF1,a\pm C_{SF_1,a}, where CSF1,aC_{SF_1,a} is the ancestor of CSF2,k2C_{SF_2,k_2} at level SF1SF_1. The despreader output is

∑idi ⟨CSF1,k1, ±CSF1,a⟩.\sum_i d_i \, \langle C_{SF_1,k_1}, \ \pm C_{SF_1,a} \rangle.

If a≠k1a \ne k_1, every term is 00 by the same-length case, and the codes are orthogonal for every choice of data. If a=k1a = k_1, the code CSF1,k1C_{SF_1,k_1} is an ancestor, every inner product is ±SF1\pm SF_1, and the output depends on the data. Then the two codes interfere. So two codes can be used at the same time if and only if neither is an ancestor of the other.

  1. OVSF(SF, k) ▷ SF a power of two, 0 ≤ k < SF
  2. 1if SF = 1 then return (1)
  3. 2p ← OVSF(SF/2, ⌊k/2⌋)
  4. 3if k is even then return (p, p)
  5. 4return (p, -p)
  6. ORTHOGONAL(SF1, k1, SF2, k2)
  7. 5return neither code is an ancestor of the other

Making CSF,kC_{SF,k} costs O(SF)O(SF) operations in the word RAM model, and the ancestor test costs O(1)O(1) word operations.

Examples and non-examples

The first levels. C2,0=(1,1)C_{2,0} = (1, 1) and C2,1=(1,−1)C_{2,1} = (1, -1). On the next level, C4,0=(1,1,1,1)C_{4,0} = (1,1,1,1), C4,1=(1,1,−1,−1)C_{4,1} = (1,1,-1,-1), C4,2=(1,−1,1,−1)C_{4,2} = (1,-1,1,-1) and C4,3=(1,−1,−1,1)C_{4,3} = (1,-1,-1,1). The four codes are orthogonal. The Sylvester matrix of order 4 has the rows (1,1,1,1)(1,1,1,1), (1,−1,1,−1)(1,-1,1,-1), (1,1,−1,−1)(1,1,-1,-1) and (1,−1,−1,1)(1,-1,-1,1); the codes are its rows 0,2,1,30, 2, 1, 3.

Two users at different rates. User A spreads with C4,0C_{4,0} and user B spreads with C8,4C_{8,4}, whose ancestors are C4,2C_{4,2}, C2,1C_{2,1} and C1,0C_{1,0}. Neither code is an ancestor of the other. If B is sent with amplitude 33, despreading the sum of the two chip streams with C4,0C_{4,0} gives exactly 4dA4 d_A for each symbol of A, and despreading with C8,4C_{8,4} gives exactly 24dB24 d_B for each symbol of B. Neither user sees the other.

Non-example: a code and its descendant. C2,0=(1,1)C_{2,0} = (1,1) and C4,1=(1,1,−1,−1)C_{4,1} = (1,1,-1,-1) have inner product 00 over one long symbol when the two short symbols are equal, but not when they differ: for short symbols (1,−1)(1, -1) the despreader of C4,1C_{4,1} gives 2+2=42 + 2 = 4. The codes are not orthogonal for all data, because C2,0C_{2,0} is the parent of C4,1C_{4,1}.

Code blocking. Suppose C4,1C_{4,1} and C4,2C_{4,2} are in use. Half of the tree capacity is free, since 1/4+1/4=1/21/4 + 1/4 = 1/2, but no code of spreading factor 22 can be given: C2,0C_{2,0} is the parent of C4,1C_{4,1} and C2,1C_{2,1} is the parent of C4,2C_{4,2}. If C4,0C_{4,0} and C4,1C_{4,1} are in use instead, C2,1C_{2,1} is free. A set of codes can be given without conflict whenever ∑1/SF≤1\sum 1/SF \le 1: assign the requests in order of increasing spreading factor, each to the leftmost free code. When codes come and go in any order, the tree can block, and a system must then move some users to other codes.

WCDMA. The chip rate is 3.843.84 Mchip/s. The uplink data channel uses spreading factors from 44 to 256256, which gives 960960 down to 1515 ksymbol/s, and the downlink goes up to spreading factor 512512, or 7.57.5 ksymbol/s. The downlink common pilot channel uses C256,0C_{256,0}.

Non-example: without synchronization. OVSF codes are orthogonal only when the chip streams are aligned. A delayed copy of a code, for example from a second multipath, is in general not orthogonal to the other codes. The periodic cross-correlation ∑iC4,1[i] C4,3[(i+1) mod 4]\sum_i C_{4,1}[i] \, C_{4,3}[(i+1) \bmod 4] at a shift of one chip is −4-4, as large in magnitude as the peak. This is why WCDMA also multiplies the chips by a long scrambling code and uses a rake receiver, and why multipath reduces the orthogonality of the downlink.

  • The Walsh-Hadamard transform correlates a block of chips with all codes of one level at once, in O(SFlog⁡SF)O(SF \log SF) operations.
  • The rows of a Sylvester-Hadamard matrix are the codewords of the first-order Reed-Muller code, up to sign; see Hadamard code.
  • IS-95 uses fixed-length Walsh codes of length 6464 in the forward link. OVSF codes extend this to several lengths in one tree.
  • Sequence families such as Gold sequences and Kasami sequences keep low correlation at every shift, which OVSF codes do not. They suit asynchronous links, while OVSF codes suit synchronous ones.

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.

ovsf_codes.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/ovsf-codes/. The authors are listed in prov/MV4CVC.jsonl.

"""Orthogonal variable spreading factor (OVSF) codes, in integers.

The code C(SF, k), for SF a power of two and 0 <= k < SF, is a list of SF
values +1 or -1, with the 3GPP numbering:
    C(1, 0) = (1),
    C(2N, 2k) = (C(N, k), C(N, k)),
    C(2N, 2k + 1) = (C(N, k), -C(N, k)).
The parent of C(SF, k) is C(SF / 2, k // 2).
"""


def _check(sf: int, k: int) -> None:
    if sf < 1 or sf & (sf - 1) or not 0 <= k < sf:
        raise ValueError("need SF a power of two and 0 <= k < SF")


def ovsf(sf: int, k: int) -> list[int]:
    """The code C(SF, k) as a list of +1 and -1."""
    _check(sf, k)
    if sf == 1:  # @line L1
        return [1]
    p = ovsf(sf // 2, k // 2)  # @line L2
    if k % 2 == 0:  # @line L3
        return p + p
    return p + [-v for v in p]  # @line L4


def is_ancestor(sf1: int, k1: int, sf2: int, k2: int) -> bool:
    """True if C(sf1, k1) lies on the path from the root to C(sf2, k2) (a code is its own ancestor)."""
    _check(sf1, k1)
    _check(sf2, k2)
    if sf1 > sf2:
        return False
    return k2 // (sf2 // sf1) == k1


def orthogonal(sf1: int, k1: int, sf2: int, k2: int) -> bool:
    """True if two codes stay orthogonal for any data: neither is an ancestor of the other."""
    return not is_ancestor(sf1, k1, sf2, k2) and not is_ancestor(sf2, k2, sf1, k1)  # @line L5


def spread(symbols: list[int], code: list[int]) -> list[int]:
    """Each symbol times the whole code, one after another."""
    return [s * c for s in symbols for c in code]


def despread(chips: list[int], code: list[int]) -> list[int]:
    """Correlate each block of len(code) chips with the code."""
    n = len(code)
    return [sum(chips[i + j] * code[j] for j in range(n)) for i in range(0, len(chips), n)]


def sylvester_row(sf: int, i: int) -> list[int]:
    """Row i of the Sylvester-Hadamard matrix of order SF: (-1)^popcount(i AND j)."""
    return [1 - 2 * (bin(i & j).count("1") % 2) for j in range(sf)]


def bit_reverse(k: int, bits: int) -> int:
    out = 0
    for _ in range(bits):
        out = (out << 1) | (k & 1)
        k >>= 1
    return out


def valid_assignment(codes: list[tuple[int, int]]) -> bool:
    """True if no assigned code is an ancestor of another."""
    for i, (sa, ka) in enumerate(codes):
        for sb, kb in codes[i + 1:]:
            if not orthogonal(sa, ka, sb, kb):
                return False
    return True


def allocate(used: list[tuple[int, int]], sf: int) -> tuple[int, int] | None:
    """The leftmost code of spreading factor SF that is orthogonal to every used code, or None."""
    for k in range(sf):
        if all(orthogonal(sf, k, su, ku) for su, ku in used):
            return (sf, k)
    return None
test_ovsf_codes.py 99 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/ovsf-codes/. The authors are listed in prov/MV4CVC.jsonl.

import random

from ovsf_codes import allocate, bit_reverse, despread, is_ancestor, orthogonal, ovsf, spread, sylvester_row, valid_assignment

SFS = [1, 2, 4, 8, 16, 32]


def dot(a: list[int], b: list[int]) -> int:
    return sum(x * y for x, y in zip(a, b))


def test_check_values() -> None:
    assert ovsf(1, 0) == [1]
    assert ovsf(2, 0) == [1, 1] and ovsf(2, 1) == [1, -1]
    assert ovsf(4, 0) == [1, 1, 1, 1]
    assert ovsf(4, 1) == [1, 1, -1, -1]
    assert ovsf(4, 2) == [1, -1, 1, -1]
    assert ovsf(4, 3) == [1, -1, -1, 1]


def test_same_spreading_factor_is_orthogonal() -> None:
    for sf in SFS + [64]:
        rows = [ovsf(sf, k) for k in range(sf)]
        for i in range(sf):
            for j in range(sf):
                assert dot(rows[i], rows[j]) == (sf if i == j else 0)


def test_codes_are_bit_reversed_hadamard_rows() -> None:
    for bits in range(7):
        sf = 1 << bits
        for k in range(sf):
            assert ovsf(sf, k) == sylvester_row(sf, bit_reverse(k, bits))


def test_mixed_rates_orthogonal_iff_not_ancestor() -> None:
    rng = random.Random(1)
    for s1 in SFS:
        for s2 in SFS:
            if s1 > s2:
                continue
            for k1 in range(s1):
                short = ovsf(s1, k1)
                data = [rng.choice((-1, 1)) for _ in range(s2 // s1)]
                for k2 in range(s2):
                    out = despread(spread(data, short), ovsf(s2, k2))
                    blocks = [dot(short, ovsf(s2, k2)[i:i + s1]) for i in range(0, s2, s1)]
                    if orthogonal(s1, k1, s2, k2):
                        assert out == [0]
                        assert all(b == 0 for b in blocks)
                    else:
                        assert is_ancestor(s1, k1, s2, k2)
                        assert all(abs(b) == s1 for b in blocks)


def test_two_users_at_different_rates_separate_exactly() -> None:
    rng = random.Random(2)
    a_code, b_code = ovsf(4, 0), ovsf(8, 4)
    assert orthogonal(4, 0, 8, 4)
    a_data = [rng.choice((-1, 1)) for _ in range(40)]
    b_data = [rng.choice((-1, 1)) for _ in range(20)]
    chips = [x + 3 * y for x, y in zip(spread(a_data, a_code), spread(b_data, b_code))]
    assert despread(chips, a_code) == [4 * v for v in a_data]
    assert despread(chips, b_code) == [24 * v for v in b_data]


def test_greedy_allocation_in_order_never_blocks() -> None:
    rng = random.Random(3)
    for _ in range(300):
        reqs = []
        load = 0.0
        while True:
            sf = rng.choice([2, 4, 8, 16, 32, 64])
            if load + 1 / sf > 1:
                break
            reqs.append(sf)
            load += 1 / sf
        used: list[tuple[int, int]] = []
        for sf in sorted(reqs):
            code = allocate(used, sf)
            assert code is not None
            used.append(code)
        assert valid_assignment(used)


def test_shifted_codes_are_not_orthogonal() -> None:
    a, b = ovsf(4, 1), ovsf(4, 3)
    assert sum(a[i] * b[(i + 1) % 4] for i in range(4)) == -4
    assert despread(spread([1, -1], ovsf(2, 0)), ovsf(4, 1)) == [4]


def test_code_blocking_example() -> None:
    used = [(4, 1), (4, 2)]
    assert valid_assignment(used)
    assert allocate(used, 2) is None
    assert allocate([(4, 0), (4, 1)], 2) == (2, 1)

References

  1. F. Adachi, M. Sawahashi and K. Okawa, Tree-structured generation of orthogonal spreading codes with different lengths for forward link of DS-CDMA mobile radio, Electronics Letters 33(1), 1997, pp. 27 to 28 (the code tree and the rule for choosing codes of different lengths)
  2. 3GPP TS 25.213, Spreading and modulation (FDD), The subclauses on channelisation codes (the code tree, the numbering C_ch,SF,k and the spreading factors of the physical channels)
  3. Andrew J. Viterbi, CDMA, Principles of Spread Spectrum Communication, Addison-Wesley, 1995, The material on orthogonal Walsh functions in the forward link

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/MV4CVC/text.yaml