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 spreading codes of lengths , 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 spreads one symbol over 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 be a power of two, the spreading factor, and . The code is a vector of values in , defined by
This is the numbering of 3GPP TS 25.213. The parent of is , and its two children are and . A code is an ancestor of (with ) if it lies on the path from the root to , that is, if .
Same length. The codes are the rows of the Sylvester-Hadamard matrix of order , in bit-reversed order: is the row with index , the bits of reversed. So if and otherwise.
Different lengths. Let . Over the time of one symbol of the long code, the short code sends symbols , each spread by . Split into blocks of length . By the recursion, each block is , where is the ancestor of at level . The despreader output is
If , every term is by the same-length case, and the codes are orthogonal for every choice of data. If , the code is an ancestor, every inner product is , 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.
OVSF(SF, k) ▷ SF a power of two, 0 ≤ k < SF- 1
if SF = 1 then return (1) - 2
p ← OVSF(SF/2, ⌊k/2⌋) - 3
if k is even then return (p, p) - 4
return (p, -p) ORTHOGONAL(SF1, k1, SF2, k2)- 5
return neither code is an ancestor of the other
Making costs operations in the word RAM model, and the ancestor test costs word operations.
Examples and non-examples
The first levels. and . On the next level, , , and . The four codes are orthogonal. The Sylvester matrix of order 4 has the rows , , and ; the codes are its rows .
Two users at different rates. User A spreads with and user B spreads with , whose ancestors are , and . Neither code is an ancestor of the other. If B is sent with amplitude , despreading the sum of the two chip streams with gives exactly for each symbol of A, and despreading with gives exactly for each symbol of B. Neither user sees the other.
Non-example: a code and its descendant. and have inner product over one long symbol when the two short symbols are equal, but not when they differ: for short symbols the despreader of gives . The codes are not orthogonal for all data, because is the parent of .
Code blocking. Suppose and are in use. Half of the tree capacity is free, since , but no code of spreading factor can be given: is the parent of and is the parent of . If and are in use instead, is free. A set of codes can be given without conflict whenever : 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 Mchip/s. The uplink data channel uses spreading factors from to , which gives down to ksymbol/s, and the downlink goes up to spreading factor , or ksymbol/s. The downlink common pilot channel uses .
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 at a shift of one chip is , 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.
Related definitions
- The Walsh-Hadamard transform correlates a block of chips with all codes of one level at once, in 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 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.
# 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
- 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)
- 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)
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag MV4CVC Edit this page History Report an error Markdown