LDPC: Python Tools for Low Density Parity Check Codes
raw JSON → 2.4.1 verified Fri May 01 auth: no python
A Python library for constructing, encoding, and decoding Low Density Parity Check (LDPC) codes. Current version 2.4.1, active development with periodic releases. Supports a variety of constructions (e.g., random, protograph, QCLDPC) and decoders (belief propagation, min-sum).
pip install ldpc Common errors
error ModuleNotFoundError: No module named 'ldpc.modules' ↓
cause Package restructured in v2.0; old internal modules no longer exist.
fix
Update imports: from ldpc import LdpcCode, etc. Do not import from ldpc.modules.
error AttributeError: module 'ldpc' has no attribute 'decode' ↓
cause Top-level decode function removed in v2.0.
fix
Use code.decoder().decode(...) or LdpcDecoder.from_code(code).decode(...).
Warnings
breaking In version 2.0+, the internal package structure was refactored. Custom parity-check matrices (H) must be passed as numpy arrays or scipy sparse matrices, not as lists. ↓
fix Ensure H is a 2D numpy array. Use np.array(H_list) if you have a list of lists.
gotcha The decoder's decode() method expects a 1D array of received symbols (0/1 for BSC, or soft bits for AWGN). For soft decoding, set decoder_type='bp' and provide log-likelihood ratios. ↓
fix For BSC: received = (codeword + noise) % 2. For AWGN: received = -2*noisey_llr + 1 (or similar).
deprecated The function `ldpc.decode` (top-level) is deprecated; use `LdpcDecoder.decode` instead. ↓
fix Replace `ldpc.decode(...)` with `decoder.decode(...)` where decoder = code.decoder().
Imports
- LDPC code classes
from ldpc import LdpcCode, RandomLdpcCode, ProtographLdpcCode, QCLdpcCode - Encoder
from ldpc import LdpcEncoder - Decoder
from ldpc import LdpcDecoder - Utils
from ldpc.utils import hamming_distance, ...
Quickstart
import numpy as np
from ldpc import RandomLdpcCode, LdpcEncoder, LdpcDecoder
# Create a random LDPC code with rate 0.5, block length 100
code = RandomLdpcCode(block_len=100, rate=0.5)
# Encode: generate random message
msg = np.random.randint(0, 2, code.k)
codeword = code.encode(msg)
# Introduce noise (BSC)
noise = np.random.binomial(1, 0.1, codeword.shape)
received = (codeword + noise) % 2
# Decode
decoder = code.decoder()
decoded = decoder.decode(received, max_iter=50)
print('Decoded correctly:', np.array_equal(decoded, codeword))