diff --git a/Code/ldpc/src/decoder/bit_flip.rs b/Code/ldpc/src/decoder/bit_flip.rs index 5c612a8..a32df2b 100644 --- a/Code/ldpc/src/decoder/bit_flip.rs +++ b/Code/ldpc/src/decoder/bit_flip.rs @@ -9,8 +9,64 @@ impl<'a> BitFlipDecoder<'a> { Self { code } } + // TODO paraléliser (le calcul de chaque check-node) pub fn decode(&self, received: &[u8], max_iter: usize) -> Option> { - let graph = &self.code.tanner_graph; - unimplemented!("Algo de bit-flipp"); + let n = self.code.n; + let m = self.code.h_matrix.rows; + + let mut curr_bits = received.to_vec(); + + for iter in 0..max_iter { + // Syndrome s = v * H^T + let mut s = vec![0; m]; + let mut s_w = 0; + + for (cnid, cb) in self.code.tanner_graph.check.iter().enumerate() { + let mut sum = 0; + // Calcul des equation de parité + for &bid in cb { + sum ^= cb[bid]; + } + s[cnid] = sum; + + // Equation non vérifiée + if sum == 1 { + s_w += 1; + } + } + + // Aucune erreurs + if s_w == 0 { + return Some(curr_bits); + } + + // Calcul du nombre d'équations fausses le bit fait partie + let mut cpt = vec![0; n]; + let mut max_cpt = 0; + + for (cid, &sb) in s.iter().enumerate() { + if sb == 1 { + for &bid in &self.code.tanner_graph.check[cid] { + cpt[bid] += 1; + if cpt[bid] > max_cpt { + max_cpt = cpt[bid]; + } + } + } + } + + if max_cpt == 0 { + break; + } + + // Flip des bit qui causent le plus d'équations fausses + for i in 0..n { + if cpt[i] == max_cpt { + curr_bits[i] ^= 1; // Bit flip + } + } + } + + return Some(curr_bits); } }