Skip to content

Commit

Permalink
add CCSDS C2 r=7/8 code
Browse files Browse the repository at this point in the history
  • Loading branch information
daniestevez committed Jan 13, 2024
1 parent 210e849 commit 43cbda0
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::error::Error;

pub mod ber;
pub mod ccsds;
pub mod ccsds_c2;
pub mod dvbs2;
pub mod encode;
pub mod mackay_neal;
Expand All @@ -29,7 +30,10 @@ pub enum Args {
BER(ber::Args),
/// ccsds subcommand
CCSDS(ccsds::Args),
/// encode subcommand,
/// ccsds-c2 subcommand
#[allow(non_camel_case_types)]
CCSDS_C2(ccsds_c2::Args),
/// encode subcommand
Encode(encode::Args),
/// dvbs2 subcommand
DVBS2(dvbs2::Args),
Expand All @@ -44,6 +48,7 @@ impl Run for Args {
match self {
Args::BER(x) => x.run(),
Args::CCSDS(x) => x.run(),
Args::CCSDS_C2(x) => x.run(),
Args::DVBS2(x) => x.run(),
Args::Encode(x) => x.run(),
Args::MackayNeal(x) => x.run(),
Expand Down
4 changes: 2 additions & 2 deletions src/cli/ccsds.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! CCSDS CLI subcommand
//!
//! This subcommand can be used to generate the LDPC codes described in the
//! CCSDS TM Synchronization and Channel Coding Blue Book. standard. It will
//! This subcommand can be used to generate the AR4JA LDPC codes described in
//! the CCSDS TM Synchronization and Channel Coding Blue Book standard. It will
//! print the alist of the parity check matrix to `stdout` and optionally
//! compute and print the girth of the Tanner graph. See [`crate::codes::ccsds`]
//! for more information about the CCSDS LDPC codes.
Expand Down
30 changes: 30 additions & 0 deletions src/cli/ccsds_c2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! CCSDS C2 CLI subcommand
//!
//! This subcommand can be used to generate the C2 LDPC code (rate ~7/8)
//! described in the CCSDS TM Synchronization and Channel Coding Blue Book
//! standard. It will print the alist of the parity check matrix to
//! `stdout`. See [`crate::codes::ccsds`] for more information about the CCSDS
//! LDPC codes.
//!
//! # Examples
//! The parity check matrix can be generated with
//! ```shell
//! $ ldpc-toolbox ccsds-c2
//! ```

use crate::cli::*;
use crate::codes::ccsds::C2Code;
use clap::Parser;

/// CCSDS CLI arguments.
#[derive(Debug, Parser)]
#[command(about = "Generates the alist of CCSDS C2 LDPC")]
pub struct Args {}

impl Run for Args {
fn run(&self) -> std::result::Result<(), Box<dyn std::error::Error>> {
let h = C2Code::new().h();
print!("{}", h.alist());
Ok(())
}
}
84 changes: 80 additions & 4 deletions src/codes/ccsds.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! # CCSDS TM Synchronization and Channel Coding LDPC codes
//! CCSDS TM Synchronization and Channel Coding LDPC codes.
//!
//! This module contains the AR4JA LDPC codes described in the TM
//! Synchronization and Channel Coding Blue Book.
//! This module contains the AR4JA LDPC codes and the C2 code described in the
//! TM Synchronization and Channel Coding Blue Book.
//!
//! ## References
//! \[1\] [CCSDS 131.0-B-4 TM Synchronization and Channel Coding Blue Book](https://public.ccsds.org/Pubs/131x0b4.pdf).
//! \[1\] [CCSDS 131.0-B-5 TM Synchronization and Channel Coding Blue Book](https://public.ccsds.org/Pubs/131x0b5.pdf).

use crate::sparse::SparseMatrix;
use enum_iterator::Sequence;
Expand Down Expand Up @@ -335,6 +335,82 @@ static PHI_K: [[[usize; 7]; 26]; 4] = [
],
];

/// C2 code definition.
///
/// This C2 code is the basic (8176, 7156) LDPC code. Expurgation, shortening
/// and extension used to construct the (8160, 7136) code should be handled
/// separately.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Default)]
pub struct C2Code {}

impl C2Code {
/// Creates a C2 code definition.
pub fn new() -> C2Code {
C2Code::default()
}

/// Constructs the parity check matrix for the code.
pub fn h(&self) -> SparseMatrix {
const N: usize = 511;
let mut h = SparseMatrix::new(Self::ROW_BLOCKS * N, Self::COL_BLOCKS * N);
for (row, circs) in C2_CIRCULANTS.iter().enumerate() {
for (col, circs) in circs.iter().enumerate() {
for &circ in circs.iter() {
let circ = usize::from(circ);
for j in 0..N {
h.insert(row * N + j, col * N + (j + circ) % N);
}
}
}
}
h
}

const ROW_BLOCKS: usize = 2;
const COL_BLOCKS: usize = 16;
const BLOCK_WEIGHT: usize = 2;
}

// Table 7-1 in CCSDS 131.0-B-5
static C2_CIRCULANTS: [[[u16; C2Code::BLOCK_WEIGHT]; C2Code::COL_BLOCKS]; C2Code::ROW_BLOCKS] = [
[
[0, 176],
[12, 239],
[0, 352],
[24, 431],
[0, 392],
[151, 409],
[0, 351],
[9, 359],
[0, 307],
[53, 329],
[0, 207],
[18, 281],
[0, 399],
[202, 457],
[0, 247],
[36, 261],
],
[
[99, 471],
[130, 473],
[198, 435],
[260, 478],
[215, 420],
[282, 481],
[48, 396],
[193, 445],
[273, 430],
[302, 451],
[96, 379],
[191, 386],
[244, 467],
[364, 470],
[51, 382],
[192, 414],
],
];

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 43cbda0

Please sign in to comment.