use crate::gf2::GF2;
use ndarray::{ArrayBase, Data, Ix1};
use num_traits::{One, Zero};
#[derive(Debug, Clone, Default)]
pub struct BpskModulator {}
impl BpskModulator {
pub fn new() -> BpskModulator {
BpskModulator::default()
}
pub fn modulate<S>(&self, codeword: &ArrayBase<S, Ix1>) -> Vec<f64>
where
S: Data<Elem = GF2>,
{
codeword.iter().cloned().map(Self::modulate_bit).collect()
}
fn modulate_bit(bit: GF2) -> f64 {
if bit.is_zero() {
-1.0
} else if bit.is_one() {
1.0
} else {
panic!("invalid GF2 value")
}
}
}
#[derive(Debug, Clone, Default)]
pub struct BpskDemodulator {
scale: f64,
}
impl BpskDemodulator {
pub fn new(noise_sigma: f64) -> BpskDemodulator {
BpskDemodulator {
scale: -2.0 / (noise_sigma * noise_sigma),
}
}
pub fn demodulate(&self, symbols: &[f64]) -> Vec<f64> {
symbols.iter().map(|&x| self.scale * x).collect()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn bpsk() {
let modulator = BpskModulator::new();
let x = modulator.modulate(&ndarray::arr1(&[GF2::one(), GF2::zero()]));
assert_eq!(&x, &[1.0, -1.0]);
}
#[test]
fn demodulator() {
let demodulator = BpskDemodulator::new(2.0_f64.sqrt());
let x = demodulator.demodulate(&[1.0, -1.0]);
assert_eq!(x.len(), 2);
let tol = 1e-4;
assert!((x[0] + 1.0).abs() < tol);
assert!((x[1] - 1.0).abs() < tol);
}
}