You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

59 lines
2.7 KiB

#!/usr/bin/env python3
"""Freeze the LDPC base matrices: write ldpc_tables.py (loaded by fec.py) and
c/src/ldpc_tables.h (for the C implementation).
The matrices were drawn from NumPy's PRNG (fec.QCLDPC construction with
fec.SEED_TABLE); from format v1 on the tables themselves are normative, so
nothing depends on a PRNG implementation. Re-running this regenerates the
same tables from the construction (it refuses to overwrite with different
ones unless --force).
"""
import os, sys, argparse
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, ".."))
import numpy as np
import fec
def main():
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("--force", action="store_true")
a = ap.parse_args()
tabs = {}
for Z in fec.Z_SET:
code = fec.QCLDPC(Z, seed=fec.SEED_TABLE.get(Z, fec.DEFAULT_SEED), base=None)
tabs[Z] = code.base.tolist()
py = os.path.join(HERE, "..", "ldpc_tables.py")
if os.path.exists(py) and not a.force:
sys.path.insert(0, os.path.dirname(py))
import ldpc_tables
if ldpc_tables.BASE != tabs:
sys.exit("construction no longer reproduces the frozen tables (use --force to overwrite)")
with open(py, "w") as f:
f.write('"""QC-LDPC base matrices, 12 x 24 per lift size Z (-1 = zero block,\n'
'otherwise the circulant shift). Normative for chirpmodem format v1;\n'
'generated by tools/export_tables.py. Do not edit."""\n')
f.write("BASE = {\n")
for Z, B in tabs.items():
f.write(f" {Z}: [\n" + "".join(f" {row},\n" for row in B) + " ],\n")
f.write("}\n")
h = os.path.join(HERE, "..", "c", "src", "ldpc_tables.h")
with open(h, "w") as f:
f.write("/* SPDX-License-Identifier: MIT */\n")
f.write("/* QC-LDPC base matrices for chirpmodem format v1 (generated by\n"
" * tools/export_tables.py; do not edit). -1 = zero block. */\n")
f.write("#ifndef CM_LDPC_TABLES_H\n#define CM_LDPC_TABLES_H\n#include <stdint.h>\n\n")
f.write(f"#define CM_LDPC_MB {fec.MB}\n#define CM_LDPC_NB {fec.NB}\n"
f"#define CM_LDPC_NZ {len(tabs)}\n\n")
f.write("static const uint16_t cm_ldpc_z[CM_LDPC_NZ] = {" +
", ".join(str(Z) for Z in tabs) + "};\n\n")
f.write("static const int16_t cm_ldpc_base[CM_LDPC_NZ][CM_LDPC_MB][CM_LDPC_NB] = {\n")
for Z, B in tabs.items():
f.write(f" /* Z = {Z} */ {{\n" + "".join(
" {" + ", ".join(f"{v:3d}" for v in row) + "},\n" for row in B) + " },\n")
f.write("};\n\n#endif\n")
print(f"wrote {py} and {h}: {len(tabs)} lift sizes")
if __name__ == "__main__":
main()