1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Encode CLI subcommand.
//!
//! This command can be used to encode using a systematic LDPC code.

use super::ber::parse_puncturing_pattern;
use crate::{
    cli::Run, encoder::Encoder, gf2::GF2, simulation::puncturing::Puncturer, sparse::SparseMatrix,
};
use clap::Parser;
use ndarray::Array1;
use num_traits::{One, Zero};
use std::{
    error::Error,
    fs::File,
    io::{ErrorKind, Read, Write},
};

/// Encode CLI arguments.
#[derive(Debug, Parser)]
#[command(about = "Performs LDPC encoding")]
pub struct Args {
    /// alist file for the code
    alist: String,
    /// input file (information words as unpacked bits)
    input: String,
    /// input file (punctured words as unpacked bits)
    output: String,
    /// Puncturing pattern (format "1,1,1,0")
    #[structopt(long)]
    puncturing: Option<String>,
}

impl Run for Args {
    fn run(&self) -> Result<(), Box<dyn Error>> {
        let puncturer = if let Some(p) = self.puncturing.as_ref() {
            Some(Puncturer::new(&parse_puncturing_pattern(p)?))
        } else {
            None
        };
        let h = SparseMatrix::from_alist(&std::fs::read_to_string(&self.alist)?)?;
        let mut input = File::open(&self.input)?;
        let mut output = File::create(&self.output)?;
        let encoder = Encoder::from_h(&h)?;
        let n = h.num_cols();
        let k = n - h.num_rows();
        let mut information_word = vec![0; k];
        let mut codeword_buf = vec![0; n];
        loop {
            match input.read_exact(&mut information_word[..]) {
                Err(e) if e.kind() == ErrorKind::UnexpectedEof => break,
                ret => ret?,
            };
            let word = Array1::from_iter(information_word.iter().map(|&b| {
                if b == 1 {
                    GF2::one()
                } else {
                    GF2::zero()
                }
            }));
            let codeword = encoder.encode(&word);
            let codeword = match &puncturer {
                Some(p) => p.puncture(&codeword)?,
                None => codeword,
            };
            for (x, y) in codeword.iter().zip(codeword_buf.iter_mut()) {
                *y = x.is_one().into();
            }
            output.write_all(&codeword_buf)?;
        }
        Ok(())
    }
}