Skip to main content

Simulate LDPC codes, both encoding and decoding

Project description

License: MIT Build Status - GitHub codecov Sourcery

LDPC

My implementation of LDPC codes. My notes regarding theory and implementation appears on GitHub Pages: https://yairmz.github.io/LDPC/
To install:

pip install sim-ldpc

To run tests simply clone, cd into the cloned repo, and run:

python -m pytest

or

python -m pytest --cov-report=html

to run also coverage tests, or

python -m pytest  -n auto --cov-report=html

to run tests in parallel (with number of CPU's dictated by machine) to speed up tests.

Verify static typing with

mypy --strict --config-file .mypy.ini .

Included modules

  • Utilities: implementing various utility operations to assist with encoding, decoding and simulations.
  • Encoder: implementing a generator based encoder, and encoders for IEEE802.11 (WiFi) LDPC codes.
  • Decoder: implementing a Log-SPA based BP decoder.

Basic Example

import numpy as np
from bitstring import BitArray, Bits
from ldpc.decoder import DecoderWiFi, bsc_llr
from ldpc.encoder import EncoderWiFi
from ldpc.wifi_spec_codes import WiFiSpecCode
from ldpc.utils import QCFile

# create information bearing bits
rng = np.random.default_rng()
info_bits = Bits(bytes=rng.bytes(41))[:648//2]
# create encoder with frame of 648 bits, and rate 1/2. Possible rates and frame sizes are per the ieee802.11n spec.
enc = EncoderWiFi(WiFiSpecCode.N648_R12)
# encode bits
encoded = enc.encode(info_bits)

# verify validity of codeword
h = enc.h
np.dot(h, np.array(encoded)) % 2  # creates an all zero vector as required.

# create a decoder which assumes a probability of p=0.05 for bit flips by the channel
# allow up to 20 iterations for the bp decoder.
p = 0.05
decoder = DecoderWiFi(spec=WiFiSpecCode.N648_R12, max_iter=20, channel_model=bsc_llr(p=p))

# create a corrupted version of encoded codeword with error rate p
corrupted = BitArray(encoded)
no_errors = int(len(corrupted)*p)
error_idx = rng.choice(len(corrupted), size=no_errors, replace=False)
for idx in error_idx:
    corrupted[idx] = not corrupted[idx]
decoded, llr, decode_success, num_of_iterations, syndrome, vnode_validity  = decoder.decode(corrupted)
# Verify correct decoding
print(Bits(decoded) == encoded)  # true
info = decoder.info_bits(decoded)

# a decoder can also be instantiated without a channel model, in which case llr is expected to be sent for decoding instead of
# hard channel outputs.
channel = bsc_llr(p=p)
channel_llr = channel(np.array(corrupted, dtype=np.int_))
decoder = DecoderWiFi(spec=WiFiSpecCode.N648_R12, max_iter=20)
decoded, llr2, decode_success, num_of_iterations, syndrome, vnode_validity  = decoder.decode(channel_llr)
print(Bits(decoded) == encoded)  # true
info = decoder.info_bits(decoded)

The example is also included as a jupyter notebook. Note however, that you need to launch the notebook from the correct path for it to be able to access installed packages. To run the notebook:

  1. create a new virtualenv
python3 -m venv ~/.virtualenv/LDPC_env
  1. activate it and install sim-ldpc, and notebook:
source ~/.virtualenv/LDPC_env/bin/activate
pip install sim-ldpc
pip install notebook
  1. run jupyter from within the virtual env for it to have access to all requirements:
~/.virtualenv/LDPC_env/bin/jupyter-notebook
  1. run the notebook

Sources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sim-ldpc-0.3.1.tar.gz (2.1 MB view details)

Uploaded Source

Built Distribution

sim_ldpc-0.3.1-py3-none-any.whl (2.3 MB view details)

Uploaded Python 3

File details

Details for the file sim-ldpc-0.3.1.tar.gz.

File metadata

  • Download URL: sim-ldpc-0.3.1.tar.gz
  • Upload date:
  • Size: 2.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for sim-ldpc-0.3.1.tar.gz
Algorithm Hash digest
SHA256 a7d9b82f0281bd3d9bc4f58e21df4d085c74469b3f0e8edb83a9f7d9cedb2922
MD5 ec2565e670d75b87bb17327b5122aaff
BLAKE2b-256 fa55d6bd4e54de78a106e5ef9fb0cf771d01228424cf3613e34c042faf2fa4df

See more details on using hashes here.

File details

Details for the file sim_ldpc-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: sim_ldpc-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for sim_ldpc-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b95fecd91c0577480e69060441816d9190a9186c120cf8467da8dbab4fe27e10
MD5 436d9fe8ed539a68cde53b2acd8efcee
BLAKE2b-256 bf0ce08ae4759ee458761480ff48023a1a53bc35417b37a1d86b3f8f256e1349

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page