channel add noise
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
pub rand::Rng;
|
||||
use std::usize;
|
||||
|
||||
use rand::{Rng, RngExt};
|
||||
|
||||
pub struct Channel {
|
||||
pub error_prob: f64,
|
||||
@ -7,8 +9,24 @@ pub struct Channel {
|
||||
impl Channel {
|
||||
pub fn new(error_prob: f64) -> Self {
|
||||
assert!(error_prob >= 0.0 && error_prob <= 1.0, "0 <= p <= 1");
|
||||
Self {error_prob}
|
||||
Self { error_prob }
|
||||
}
|
||||
|
||||
}
|
||||
pub fn add_noise(&self, input: &[u8]) -> Vec<u8> {
|
||||
let mut rng = rand::rng();
|
||||
let mut out = input.to_vec();
|
||||
|
||||
for b in out.iter_mut() {
|
||||
let r_v: f64 = rng.random_range(0.0..1.0);
|
||||
if r_v < self.error_prob {
|
||||
*b ^= 1;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub fn count_errors(original: &[u8], other: &[u8]) -> usize {
|
||||
assert_eq!(original.len(), other.len(), "Slice pas de la même taille");
|
||||
original.iter().zip(other).filter(|(a, b)| a != b).count()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user