t
This commit is contained in:
53
main.typ
53
main.typ
@ -1,5 +1,5 @@
|
|||||||
#import "composants.typ": *
|
#import "composants.typ": *
|
||||||
#import "@preview/touying:0.5.2": *
|
// #import "@preview/touying:0.5.2": *
|
||||||
#import "code_slides.typ": generer_slides_code
|
#import "code_slides.typ": generer_slides_code
|
||||||
|
|
||||||
#set page(
|
#set page(
|
||||||
@ -26,6 +26,7 @@
|
|||||||
#text(size: 3em, weight: "bold", fill: black)[#titre]
|
#text(size: 3em, weight: "bold", fill: black)[#titre]
|
||||||
|
|
||||||
#text(size: 1.2em, weight: "bold", fill: black)[#auteur]
|
#text(size: 1.2em, weight: "bold", fill: black)[#auteur]
|
||||||
|
t
|
||||||
|
|
||||||
#h(0.5em)
|
#h(0.5em)
|
||||||
|
|
||||||
@ -909,29 +910,29 @@
|
|||||||
#set text(1em)
|
#set text(1em)
|
||||||
- Si $v_j$ participe à *trop d'équations non satisfaites* $=>$ on l'inverse.
|
- Si $v_j$ participe à *trop d'équations non satisfaites* $=>$ on l'inverse.
|
||||||
|
|
||||||
#uncover(2)[
|
#uncover(2)[
|
||||||
#grid(
|
#grid(
|
||||||
columns: (1fr, 1fr, 1fr),
|
columns: (1fr, 1fr, 1fr),
|
||||||
column-gutter: 10pt,
|
column-gutter: 10pt,
|
||||||
// On aligne tout par le bas (bottom) pour que les CN soient sur la même ligne
|
// On aligne tout par le bas (bottom) pour que les CN soient sur la même ligne
|
||||||
align: center + bottom,
|
align: center + bottom,
|
||||||
[
|
[
|
||||||
#bf_step1_sending()
|
#bf_step1_sending()
|
||||||
#v(-0.5em)
|
#v(-0.5em)
|
||||||
#text(style: "italic", fill: gray.darken(40%), size: 0.8em)[VN $arrow$ CN]
|
#text(style: "italic", fill: gray.darken(40%), size: 0.8em)[VN $arrow$ CN]
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
#bf_step2_verdict()
|
#bf_step2_verdict()
|
||||||
#v(-0.5em)
|
#v(-0.5em)
|
||||||
#text(style: "italic", fill: gray.darken(40%), size: 0.8em)[CN $arrow$ VN]
|
#text(style: "italic", fill: gray.darken(40%), size: 0.8em)[CN $arrow$ VN]
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
#bf_step3_flip()
|
#bf_step3_flip()
|
||||||
#v(-0.5em)
|
#v(-0.5em)
|
||||||
#text(style: "italic", fill: gray.darken(40%), size: 0.8em)[Correction]
|
#text(style: "italic", fill: gray.darken(40%), size: 0.8em)[Correction]
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
// #align(center + horizon)[
|
// #align(center + horizon)[
|
||||||
// #scale(100%)[#bp_hard_diagram()]
|
// #scale(100%)[#bp_hard_diagram()]
|
||||||
// #v(0.4em)
|
// #v(0.4em)
|
||||||
@ -2161,7 +2162,11 @@
|
|||||||
"src/rs/decoder.rs",
|
"src/rs/decoder.rs",
|
||||||
"src/rs/generator.rs",
|
"src/rs/generator.rs",
|
||||||
"src/rs/channel.rs",
|
"src/rs/channel.rs",
|
||||||
|
"src/rs/graph.rs",
|
||||||
"src/rs/matrix.rs",
|
"src/rs/matrix.rs",
|
||||||
|
"src/rs/benchmark.rs",
|
||||||
|
"src/rs/benchmark2.rs",
|
||||||
|
"src/rs/image_sim.rs",
|
||||||
"src/rs/lib.rs",
|
"src/rs/lib.rs",
|
||||||
),
|
),
|
||||||
myslide,
|
myslide,
|
||||||
|
|||||||
290
src/rs/benchmark.rs
Normal file
290
src/rs/benchmark.rs
Normal file
@ -0,0 +1,290 @@
|
|||||||
|
use crate::channel::{AwgnChannel, Channel};
|
||||||
|
use crate::code::{CodeTopology, GenerationMethod, LdpcCode, LdpcParams};
|
||||||
|
use crate::decoder::{build_decoder, DecoderConfig, DecoderMethod};
|
||||||
|
use crate::encoder::{build_encoder, EncodingMethod};
|
||||||
|
use crate::Result as LdpcResult;
|
||||||
|
use rand::Rng;
|
||||||
|
use rayon::prelude::*;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
pub fn run_simulation(mut code: LdpcCode) -> LdpcResult<()> {
|
||||||
|
println!("[*] Étape 1 : Construction Mathématique et Graphe de Tanner");
|
||||||
|
|
||||||
|
println!(
|
||||||
|
" - Dimensions : n={}, k={}, m={} (Taux R={:.3})",
|
||||||
|
code.n(),
|
||||||
|
code.k(),
|
||||||
|
code.m(),
|
||||||
|
code.rate()
|
||||||
|
);
|
||||||
|
|
||||||
|
let methode_nom = match code.params.generation {
|
||||||
|
GenerationMethod::MacKayNeal { .. } => "MacKay-Neal",
|
||||||
|
GenerationMethod::Gallager => "Gallager",
|
||||||
|
};
|
||||||
|
|
||||||
|
println!(" - Topologie : Régulier via {}", methode_nom);
|
||||||
|
println!(" - Densité H : {:.2}%", code.h.density() * 100.0);
|
||||||
|
println!(" - Girth : {}", code.girth());
|
||||||
|
println!(
|
||||||
|
" - Cycles-4 : {}",
|
||||||
|
if code.graph.has_4_cycle() {
|
||||||
|
"Présents (Problématique)"
|
||||||
|
} else {
|
||||||
|
"Aucun (Optimal)"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
println!("\n[*] Étape 2 : Instanciation de l'Encodeur (SIMD Bit-Packing)");
|
||||||
|
let start_enc = Instant::now();
|
||||||
|
let encoder = build_encoder(&mut code, EncodingMethod::Systematic)?;
|
||||||
|
println!(" - Encodeur prêt en {:.2?}", start_enc.elapsed());
|
||||||
|
|
||||||
|
println!("\n[*] Étape 3 : Instanciation des Décodeurs sur Graphe");
|
||||||
|
let config = DecoderConfig {
|
||||||
|
max_iterations: 50,
|
||||||
|
early_stopping: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
let dec_sp = build_decoder(&code, DecoderMethod::SumProduct, config.clone());
|
||||||
|
let dec_ms = build_decoder(
|
||||||
|
&code,
|
||||||
|
DecoderMethod::MinSum {
|
||||||
|
scaling_factor: 0.8,
|
||||||
|
},
|
||||||
|
config.clone(),
|
||||||
|
);
|
||||||
|
let dec_bf = build_decoder(&code, DecoderMethod::BitFlipping, config.clone());
|
||||||
|
println!(" - Moteurs prêts : Sum-Product, Min-Sum (α=0.8), Bit-Flipping");
|
||||||
|
|
||||||
|
let snr_range = [1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 3.0, 3.5, 4.0];
|
||||||
|
let n_trials = 1000;
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"\n[*] Étape 4 : Simulation sur Canal AWGN ({} trames par SNR, Multi-threadé)",
|
||||||
|
n_trials
|
||||||
|
);
|
||||||
|
println!("{:-<115}", "");
|
||||||
|
println!(
|
||||||
|
"{:>8} | {:>9} || {:>10} | {:>10} || {:>10} | {:>10} || {:>10} | {:>10} |",
|
||||||
|
"SNR (dB)",
|
||||||
|
"Capacité",
|
||||||
|
"FER (SP)",
|
||||||
|
"BER (SP)",
|
||||||
|
"FER (MS)",
|
||||||
|
"BER (MS)",
|
||||||
|
"FER (BF)",
|
||||||
|
"BER (BF)"
|
||||||
|
);
|
||||||
|
println!("{:-<115}", "");
|
||||||
|
|
||||||
|
for &snr in &snr_range {
|
||||||
|
let channel = AwgnChannel::new(snr, code.rate())?;
|
||||||
|
|
||||||
|
let results: Vec<_> = (0..n_trials)
|
||||||
|
.into_par_iter()
|
||||||
|
.map(|_| {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
|
let message: Vec<u8> = (0..code.k()).map(|_| rng.gen::<u8>() & 1).collect();
|
||||||
|
let codeword = encoder.encode(&message).unwrap();
|
||||||
|
let received_llr = channel.transmit(&codeword, &mut rng);
|
||||||
|
|
||||||
|
let mut t_sp_f = 0;
|
||||||
|
let mut t_sp_b = 0;
|
||||||
|
let mut t_ms_f = 0;
|
||||||
|
let mut t_ms_b = 0;
|
||||||
|
let mut t_bf_f = 0;
|
||||||
|
let mut t_bf_b = 0;
|
||||||
|
|
||||||
|
// SP
|
||||||
|
let res_sp = dec_sp.decode(&received_llr);
|
||||||
|
if let Some(decoded) = res_sp.codeword() {
|
||||||
|
let errs = count_bit_errors(&codeword, decoded);
|
||||||
|
if errs > 0 {
|
||||||
|
t_sp_f = 1;
|
||||||
|
t_sp_b = errs;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t_sp_f = 1;
|
||||||
|
t_sp_b = code.n();
|
||||||
|
}
|
||||||
|
|
||||||
|
// MS
|
||||||
|
let res_ms = dec_ms.decode(&received_llr);
|
||||||
|
if let Some(decoded) = res_ms.codeword() {
|
||||||
|
let errs = count_bit_errors(&codeword, decoded);
|
||||||
|
if errs > 0 {
|
||||||
|
t_ms_f = 1;
|
||||||
|
t_ms_b = errs;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t_ms_f = 1;
|
||||||
|
t_ms_b = code.n();
|
||||||
|
}
|
||||||
|
|
||||||
|
// BF
|
||||||
|
let res_bf = dec_bf.decode(&received_llr);
|
||||||
|
if let Some(decoded) = res_bf.codeword() {
|
||||||
|
let errs = count_bit_errors(&codeword, decoded);
|
||||||
|
if errs > 0 {
|
||||||
|
t_bf_f = 1;
|
||||||
|
t_bf_b = errs;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t_bf_f = 1;
|
||||||
|
t_bf_b = code.n();
|
||||||
|
}
|
||||||
|
|
||||||
|
(t_sp_f, t_sp_b, t_ms_f, t_ms_b, t_bf_f, t_bf_b)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// Agrégation
|
||||||
|
let mut err_sp_frames = 0;
|
||||||
|
let mut err_sp_bits = 0;
|
||||||
|
let mut err_ms_frames = 0;
|
||||||
|
let mut err_ms_bits = 0;
|
||||||
|
let mut err_bf_frames = 0;
|
||||||
|
let mut err_bf_bits = 0;
|
||||||
|
|
||||||
|
for res in results {
|
||||||
|
err_sp_frames += res.0;
|
||||||
|
err_sp_bits += res.1;
|
||||||
|
err_ms_frames += res.2;
|
||||||
|
err_ms_bits += res.3;
|
||||||
|
err_bf_frames += res.4;
|
||||||
|
err_bf_bits += res.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
let total_bits = (n_trials * code.n()) as f64;
|
||||||
|
let total_frames = n_trials as f64;
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"{:>8.2} | {:>9.4} || {:>9.2}% | {:>9.2}% || {:>9.2}% | {:>9.2}% || {:>9.2}% | {:>9.2}% |",
|
||||||
|
snr, channel.capacity(),
|
||||||
|
(err_sp_frames as f64 / total_frames) * 100.0, (err_sp_bits as f64 / total_bits) * 100.0,
|
||||||
|
(err_ms_frames as f64 / total_frames) * 100.0, (err_ms_bits as f64 / total_bits) * 100.0,
|
||||||
|
(err_bf_frames as f64 / total_frames) * 100.0, (err_bf_bits as f64 / total_bits) * 100.0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
println!("{:-<115}", "");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn count_bit_errors(transmitted: &[u8], decoded: &[u8]) -> usize {
|
||||||
|
transmitted
|
||||||
|
.iter()
|
||||||
|
.zip(decoded.iter())
|
||||||
|
.filter(|(a, b)| a != b)
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// pub fn generate_valid_code(
|
||||||
|
// n: usize,
|
||||||
|
// k: usize,
|
||||||
|
// wc: usize,
|
||||||
|
// wr: usize,
|
||||||
|
// generation_method: GenerationMethod,
|
||||||
|
// ) -> LdpcResult<LdpcCode> {
|
||||||
|
// let mut attempt = 0;
|
||||||
|
// let start_gen = Instant::now();
|
||||||
|
// loop {
|
||||||
|
// attempt += 1;
|
||||||
|
// let params = LdpcParams {
|
||||||
|
// n,
|
||||||
|
// k,
|
||||||
|
// topology: CodeTopology::Regular { wc, wr },
|
||||||
|
// generation: generation_method.clone(),
|
||||||
|
// seed: Some(rand::random()),
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// if let Ok(mut code) = LdpcCode::new(params) {
|
||||||
|
// if code.compute_systematic_form().is_ok() {
|
||||||
|
// if attempt > 1 {
|
||||||
|
// println!(
|
||||||
|
// " -> Matrice inversible obtenue après {} tentatives.",
|
||||||
|
// attempt
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// println!(" - Génération : Terminée en {:.2?}", start_gen.elapsed());
|
||||||
|
// return Ok(code);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
pub fn get_or_generate_cached_code(
|
||||||
|
n: usize,
|
||||||
|
k: usize,
|
||||||
|
wc: usize,
|
||||||
|
wr: usize,
|
||||||
|
generation_method: GenerationMethod,
|
||||||
|
) -> LdpcResult<LdpcCode> {
|
||||||
|
let method_str = match generation_method {
|
||||||
|
GenerationMethod::MacKayNeal { .. } => "MN",
|
||||||
|
GenerationMethod::Gallager => "GAL",
|
||||||
|
};
|
||||||
|
let cache_filename = format!("cache_ldpc_{}_n{}_k{}.bin", method_str, n, k);
|
||||||
|
let path = Path::new(&cache_filename);
|
||||||
|
|
||||||
|
// Chargement
|
||||||
|
if path.exists() {
|
||||||
|
let start_load = Instant::now();
|
||||||
|
let mut file = File::open(&path).expect("Impossible d'ouvrir le fichier de cache");
|
||||||
|
let mut buffer = Vec::new();
|
||||||
|
file.read_to_end(&mut buffer).unwrap();
|
||||||
|
|
||||||
|
let mut code: LdpcCode = bincode::deserialize(&buffer).expect(
|
||||||
|
"Erreur de désérialisation du cache LDPC. Supprimez le fichier .bin et réessayez.",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Reconstruction du Graphe de Tanner
|
||||||
|
code.graph = crate::graph::TannerGraph::from_matrix(&code.h);
|
||||||
|
|
||||||
|
println!(
|
||||||
|
" -> Matrice chargée depuis le cache en {:.2?}",
|
||||||
|
start_load.elapsed()
|
||||||
|
);
|
||||||
|
return Ok(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Génération
|
||||||
|
println!(" -> Aucun cache trouvé. Génération en cours ...");
|
||||||
|
let mut attempt = 0;
|
||||||
|
let start_gen = Instant::now();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
attempt += 1;
|
||||||
|
let params = LdpcParams {
|
||||||
|
n,
|
||||||
|
k,
|
||||||
|
topology: CodeTopology::Regular { wc, wr },
|
||||||
|
generation: generation_method.clone(),
|
||||||
|
seed: Some(rand::random()),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Ok(mut code) = LdpcCode::new(params) {
|
||||||
|
if code.compute_systematic_form().is_ok() {
|
||||||
|
println!(
|
||||||
|
" - Génération et Pivot de Gauss terminés en {:.2?}",
|
||||||
|
start_gen.elapsed()
|
||||||
|
);
|
||||||
|
|
||||||
|
// sauvegarde cache
|
||||||
|
let encoded = bincode::serialize(&code).expect("Échec de la sérialisation");
|
||||||
|
let mut file =
|
||||||
|
File::create(&path).expect("Impossible de créer le fichier de cache");
|
||||||
|
file.write_all(&encoded)
|
||||||
|
.expect("Impossible d'écrire sur le disque");
|
||||||
|
println!(" -> Matrice sauvegardée ({})", cache_filename);
|
||||||
|
|
||||||
|
return Ok(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
228
src/rs/benchmark2.rs
Normal file
228
src/rs/benchmark2.rs
Normal file
@ -0,0 +1,228 @@
|
|||||||
|
use crate::benchmark::get_or_generate_cached_code;
|
||||||
|
use crate::channel::{AwgnChannel, Channel};
|
||||||
|
use crate::code::GenerationMethod;
|
||||||
|
use crate::decoder::{build_decoder, DecoderConfig, DecoderMethod};
|
||||||
|
use crate::encoder::{build_encoder, EncodingMethod};
|
||||||
|
use crate::Result as LdpcResult;
|
||||||
|
|
||||||
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
|
use rand::Rng;
|
||||||
|
use rayon::prelude::*;
|
||||||
|
use std::fs::OpenOptions;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct CodeScenario {
|
||||||
|
pub n: usize,
|
||||||
|
pub k: usize,
|
||||||
|
pub wc: usize,
|
||||||
|
pub wr: usize,
|
||||||
|
pub method: GenerationMethod,
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CampaignConfig {
|
||||||
|
pub scenarios: Vec<CodeScenario>,
|
||||||
|
pub snr_range: Vec<f64>,
|
||||||
|
pub n_trials: usize,
|
||||||
|
pub max_iterations: usize,
|
||||||
|
pub output_csv: String,
|
||||||
|
pub export_graph: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_massive_campaign(config: CampaignConfig) -> LdpcResult<()> {
|
||||||
|
println!("TEST)");
|
||||||
|
println!("Fichier de sortie : {}", config.output_csv);
|
||||||
|
println!("Scenarios a tester: {}", config.scenarios.len());
|
||||||
|
println!("SNRs par scenario : {}", config.snr_range.len());
|
||||||
|
println!("Trames par point : {}\n", config.n_trials);
|
||||||
|
|
||||||
|
let file_exists = std::path::Path::new(&config.output_csv).exists();
|
||||||
|
let mut file = OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.append(true)
|
||||||
|
.open(&config.output_csv)
|
||||||
|
.expect("Impossible d'ouvrir le fichier CSV");
|
||||||
|
|
||||||
|
if !file_exists {
|
||||||
|
writeln!(
|
||||||
|
file,
|
||||||
|
"Scenario,N,K,Rate,Wc,Wr,Method,Girth,Density_pct,SNR_dB,Capacity,Decoder,FER_pct,BER_pct,Frames,Time_ms"
|
||||||
|
).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
let total_steps = (config.scenarios.len() * config.snr_range.len()) as u64;
|
||||||
|
let pb = ProgressBar::new(total_steps);
|
||||||
|
pb.set_style(
|
||||||
|
ProgressStyle::default_bar()
|
||||||
|
.template("{spinner:.cyan} [{elapsed_precise}] [{bar:40.green/blue}] {pos}/{len} ({eta}) - {msg}")
|
||||||
|
.unwrap()
|
||||||
|
.progress_chars("=>-"),
|
||||||
|
);
|
||||||
|
|
||||||
|
for scenario in config.scenarios {
|
||||||
|
pb.set_message(format!("Generation Matrice: {}", scenario.name));
|
||||||
|
|
||||||
|
let mut code = get_or_generate_cached_code(
|
||||||
|
scenario.n,
|
||||||
|
scenario.k,
|
||||||
|
scenario.wc,
|
||||||
|
scenario.wr,
|
||||||
|
scenario.method.clone(),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
if config.export_graph {
|
||||||
|
let dot_filename = format!("{}_tanner.dot", scenario.name);
|
||||||
|
let mut dot_file = std::fs::File::create(&dot_filename).expect("Erreur creation .dot");
|
||||||
|
|
||||||
|
writeln!(dot_file, "graph TannerGraph {{").unwrap();
|
||||||
|
writeln!(dot_file, " rankdir=TB;").unwrap();
|
||||||
|
writeln!(dot_file, " nodesep=0.5;").unwrap();
|
||||||
|
writeln!(dot_file, " ranksep=2.0;").unwrap();
|
||||||
|
|
||||||
|
writeln!(dot_file, " node [style=filled, fontname=\"Arial\"];").unwrap();
|
||||||
|
|
||||||
|
writeln!(dot_file, " {{ rank=same;").unwrap();
|
||||||
|
for v in 0..code.n() {
|
||||||
|
writeln!(
|
||||||
|
dot_file,
|
||||||
|
" v{} [shape=circle, fillcolor=lightblue, label=\"v{}\"];",
|
||||||
|
v, v
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
writeln!(dot_file, " }}").unwrap();
|
||||||
|
|
||||||
|
writeln!(dot_file, " {{ rank=same;").unwrap();
|
||||||
|
for c in 0..code.m() {
|
||||||
|
writeln!(
|
||||||
|
dot_file,
|
||||||
|
" c{} [shape=square, fillcolor=lightcoral, label=\"c{}\"];",
|
||||||
|
c, c
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
writeln!(dot_file, " }}").unwrap();
|
||||||
|
|
||||||
|
for c in 0..code.m() {
|
||||||
|
for &v in code.graph.chk_neighbors(c) {
|
||||||
|
writeln!(dot_file, " v{} -- c{};", v, c).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeln!(dot_file, "}}").unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
let rate = code.rate();
|
||||||
|
let girth = code.girth();
|
||||||
|
let density = code.h.density() * 100.0;
|
||||||
|
let method_str = match scenario.method {
|
||||||
|
GenerationMethod::Gallager => "Gallager",
|
||||||
|
GenerationMethod::MacKayNeal { .. } => "MacKayNeal",
|
||||||
|
};
|
||||||
|
|
||||||
|
pb.set_message(format!("Initialisation DSP: {}", scenario.name));
|
||||||
|
let encoder = build_encoder(&mut code, EncodingMethod::Systematic)?;
|
||||||
|
|
||||||
|
let dec_config = DecoderConfig {
|
||||||
|
max_iterations: config.max_iterations,
|
||||||
|
early_stopping: true,
|
||||||
|
};
|
||||||
|
let dec_sp = build_decoder(&code, DecoderMethod::SumProduct, dec_config.clone());
|
||||||
|
let dec_ms = build_decoder(
|
||||||
|
&code,
|
||||||
|
DecoderMethod::MinSum {
|
||||||
|
scaling_factor: 0.8,
|
||||||
|
},
|
||||||
|
dec_config.clone(),
|
||||||
|
);
|
||||||
|
let dec_bf = build_decoder(&code, DecoderMethod::BitFlipping, dec_config);
|
||||||
|
|
||||||
|
for &snr in &config.snr_range {
|
||||||
|
pb.set_message(format!("Test: {} | SNR: {:.2} dB", scenario.name, snr));
|
||||||
|
let channel = AwgnChannel::new(snr, rate)?;
|
||||||
|
let cap = channel.capacity();
|
||||||
|
|
||||||
|
let start_snr_time = Instant::now();
|
||||||
|
|
||||||
|
let results: Vec<_> = (0..config.n_trials)
|
||||||
|
.into_par_iter()
|
||||||
|
.map(|_| {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let message: Vec<u8> = (0..scenario.k).map(|_| rng.gen::<u8>() & 1).collect();
|
||||||
|
let codeword = encoder.encode(&message).unwrap();
|
||||||
|
let rx_llr = channel.transmit(&codeword, &mut rng);
|
||||||
|
|
||||||
|
let eval_decoder = |decoder: &dyn crate::decoder::Decoder| -> (usize, usize) {
|
||||||
|
if let Some(decoded) = decoder.decode(&rx_llr).codeword() {
|
||||||
|
let errs = codeword
|
||||||
|
.iter()
|
||||||
|
.zip(decoded.iter())
|
||||||
|
.filter(|(a, b)| a != b)
|
||||||
|
.count();
|
||||||
|
(if errs > 0 { 1 } else { 0 }, errs)
|
||||||
|
} else {
|
||||||
|
(1, scenario.n)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let (sp_f, sp_b) = eval_decoder(&*dec_sp);
|
||||||
|
let (ms_f, ms_b) = eval_decoder(&*dec_ms);
|
||||||
|
let (bf_f, bf_b) = eval_decoder(&*dec_bf);
|
||||||
|
|
||||||
|
(sp_f, sp_b, ms_f, ms_b, bf_f, bf_b)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let mut sp_err = (0, 0);
|
||||||
|
let mut ms_err = (0, 0);
|
||||||
|
let mut bf_err = (0, 0);
|
||||||
|
for r in results {
|
||||||
|
sp_err.0 += r.0;
|
||||||
|
sp_err.1 += r.1;
|
||||||
|
ms_err.0 += r.2;
|
||||||
|
ms_err.1 += r.3;
|
||||||
|
bf_err.0 += r.4;
|
||||||
|
bf_err.1 += r.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
let elapsed_ms = start_snr_time.elapsed().as_millis();
|
||||||
|
let total_f = config.n_trials as f64;
|
||||||
|
let total_b = (config.n_trials * scenario.n) as f64;
|
||||||
|
|
||||||
|
let mut write_csv_line = |dec_name: &str, f_err: usize, b_err: usize| {
|
||||||
|
writeln!(
|
||||||
|
file,
|
||||||
|
"{},{},{},{:.3},{},{},{},{},{:.4},{:.2},{:.4},{},{:.4},{:.4e},{},{}",
|
||||||
|
scenario.name,
|
||||||
|
scenario.n,
|
||||||
|
scenario.k,
|
||||||
|
rate,
|
||||||
|
scenario.wc,
|
||||||
|
scenario.wr,
|
||||||
|
method_str,
|
||||||
|
girth,
|
||||||
|
density,
|
||||||
|
snr,
|
||||||
|
cap,
|
||||||
|
dec_name,
|
||||||
|
(f_err as f64 / total_f) * 100.0,
|
||||||
|
(b_err as f64 / total_b) * 100.0,
|
||||||
|
config.n_trials,
|
||||||
|
elapsed_ms
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
};
|
||||||
|
|
||||||
|
write_csv_line("SumProduct", sp_err.0, sp_err.1);
|
||||||
|
write_csv_line("MinSum_0.8", ms_err.0, ms_err.1);
|
||||||
|
write_csv_line("BitFlipping", bf_err.0, bf_err.1);
|
||||||
|
|
||||||
|
file.flush().unwrap();
|
||||||
|
pb.inc(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pb.finish_with_message("Campagne terminee avec succes !");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
142
src/rs/graph.rs
Normal file
142
src/rs/graph.rs
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
use crate::matrix::SparseMatrixGF2;
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
|
// Graphe de Tanner
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct TannerGraph {
|
||||||
|
pub n_var: usize,
|
||||||
|
pub n_chk: usize,
|
||||||
|
var_to_chk: Vec<Vec<usize>>,
|
||||||
|
chk_to_var: Vec<Vec<usize>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TannerGraph {
|
||||||
|
pub fn from_matrix(h: &SparseMatrixGF2) -> Self {
|
||||||
|
let n_var = h.cols;
|
||||||
|
let n_chk = h.rows;
|
||||||
|
let chk_to_var: Vec<Vec<usize>> = (0..n_chk).map(|c| h.row_neighbors(c).to_vec()).collect();
|
||||||
|
let mut var_to_chk = vec![vec![]; n_var];
|
||||||
|
for c in 0..n_chk {
|
||||||
|
for &v in &chk_to_var[c] {
|
||||||
|
var_to_chk[v].push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Self {
|
||||||
|
n_var,
|
||||||
|
n_chk,
|
||||||
|
var_to_chk,
|
||||||
|
chk_to_var,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn var_neighbors(&self, v: usize) -> &[usize] {
|
||||||
|
&self.var_to_chk[v]
|
||||||
|
}
|
||||||
|
pub fn chk_neighbors(&self, c: usize) -> &[usize] {
|
||||||
|
&self.chk_to_var[c]
|
||||||
|
}
|
||||||
|
pub fn var_degree(&self, v: usize) -> usize {
|
||||||
|
self.var_to_chk[v].len()
|
||||||
|
}
|
||||||
|
pub fn chk_degree(&self, c: usize) -> usize {
|
||||||
|
self.chk_to_var[c].len()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcule le girth par BFS depuis chaque noeud de variable
|
||||||
|
pub fn girth(&self) -> usize {
|
||||||
|
let mut min_girth = usize::MAX;
|
||||||
|
for start in 0..self.n_var {
|
||||||
|
if let Some(g) = self.bfs_girth_from_var(start) {
|
||||||
|
min_girth = min_girth.min(g);
|
||||||
|
if min_girth == 4 {
|
||||||
|
return 4;
|
||||||
|
} // minimum
|
||||||
|
}
|
||||||
|
}
|
||||||
|
min_girth
|
||||||
|
}
|
||||||
|
|
||||||
|
// Détection cycles-4, 2 varnodes partagent >= check-nodes
|
||||||
|
pub fn has_4_cycle(&self) -> bool {
|
||||||
|
for v1 in 0..self.n_var {
|
||||||
|
for v2 in (v1 + 1)..self.n_var {
|
||||||
|
let common = self.var_to_chk[v1]
|
||||||
|
.iter()
|
||||||
|
.filter(|c| self.var_to_chk[v2].contains(c))
|
||||||
|
.count();
|
||||||
|
if common >= 2 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn local_girth_from_var(&self, v: usize) -> usize {
|
||||||
|
self.bfs_girth_from_var(v).unwrap_or(usize::MAX)
|
||||||
|
}
|
||||||
|
|
||||||
|
// retourne la longueur du court cycle passant par ce noeud (None si pas cycle)
|
||||||
|
fn bfs_girth_from_var(&self, start: usize) -> Option<usize> {
|
||||||
|
let mut dist_var = vec![usize::MAX; self.n_var];
|
||||||
|
let mut dist_chk = vec![usize::MAX; self.n_chk];
|
||||||
|
dist_var[start] = 0;
|
||||||
|
|
||||||
|
// File (is_var, index, distance, parent_index)
|
||||||
|
let mut queue: VecDeque<(bool, usize, usize, usize)> = VecDeque::new();
|
||||||
|
queue.push_back((true, start, 0, usize::MAX));
|
||||||
|
let mut shortest = None;
|
||||||
|
|
||||||
|
while let Some((is_var, node, dist, parent)) = queue.pop_front() {
|
||||||
|
if is_var {
|
||||||
|
for &c in self.var_neighbors(node) {
|
||||||
|
if c == parent {
|
||||||
|
continue;
|
||||||
|
} // aller retour immédiat impossible
|
||||||
|
if dist_chk[c] == usize::MAX {
|
||||||
|
dist_chk[c] = dist + 1;
|
||||||
|
queue.push_back((false, c, dist + 1, node));
|
||||||
|
} else {
|
||||||
|
let cycle_len = dist + 1 + dist_chk[c];
|
||||||
|
shortest = Some(shortest.map_or(cycle_len, |s: usize| s.min(cycle_len)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for &v in self.chk_neighbors(node) {
|
||||||
|
if v == parent {
|
||||||
|
continue;
|
||||||
|
} // aller retour immédiat impossible
|
||||||
|
if v == start {
|
||||||
|
let cycle_len = dist + 1;
|
||||||
|
shortest = Some(shortest.map_or(cycle_len, |s: usize| s.min(cycle_len)));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if dist_var[v] == usize::MAX {
|
||||||
|
dist_var[v] = dist + 1;
|
||||||
|
queue.push_back((true, v, dist + 1, node));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
shortest
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn var_degree_distribution(&self) -> Vec<f64> {
|
||||||
|
let max_deg = self.var_to_chk.iter().map(|v| v.len()).max().unwrap_or(0);
|
||||||
|
let mut counts = vec![0usize; max_deg + 1];
|
||||||
|
for v in 0..self.n_var {
|
||||||
|
counts[self.var_degree(v)] += 1;
|
||||||
|
}
|
||||||
|
counts
|
||||||
|
.iter()
|
||||||
|
.map(|&c| c as f64 / self.n_var as f64)
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_regular(&self) -> bool {
|
||||||
|
let d0 = self.var_degree(0);
|
||||||
|
let c0 = self.chk_degree(0);
|
||||||
|
self.var_to_chk.iter().all(|v| v.len() == d0)
|
||||||
|
&& self.chk_to_var.iter().all(|c| c.len() == c0)
|
||||||
|
}
|
||||||
|
}
|
||||||
237
src/rs/image_sim.rs
Normal file
237
src/rs/image_sim.rs
Normal file
@ -0,0 +1,237 @@
|
|||||||
|
use crate::{
|
||||||
|
channel::{AwgnChannel, Channel},
|
||||||
|
decoder::Decoder,
|
||||||
|
encoder::Encoder,
|
||||||
|
Gf2, Result,
|
||||||
|
};
|
||||||
|
use image::{ImageBuffer, Rgb};
|
||||||
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
|
use rayon::prelude::*;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
// Convertit un tableau d'octets en un flux de bits
|
||||||
|
pub fn bytes_to_bits(bytes: &[u8]) -> Vec<Gf2> {
|
||||||
|
let mut bits = Vec::with_capacity(bytes.len() * 8);
|
||||||
|
for &byte in bytes {
|
||||||
|
for i in (0..8).rev() {
|
||||||
|
bits.push((byte >> i) & 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bits
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convertit un flux de bits en tableau d'octets
|
||||||
|
pub fn bits_to_bytes(bits: &[Gf2]) -> Vec<u8> {
|
||||||
|
let mut bytes = Vec::with_capacity(bits.len() / 8);
|
||||||
|
for chunk in bits.chunks(8) {
|
||||||
|
let mut byte = 0u8;
|
||||||
|
for (i, &bit) in chunk.iter().enumerate() {
|
||||||
|
byte |= bit << (7 - i);
|
||||||
|
}
|
||||||
|
bytes.push(byte);
|
||||||
|
}
|
||||||
|
bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transmet une image à travers le canal avec codage LDPC
|
||||||
|
// pub fn transmit_image(
|
||||||
|
// input_path: &str,
|
||||||
|
// noisy_out_path: &str,
|
||||||
|
// decoded_out_path: &str,
|
||||||
|
// encoder: &dyn Encoder,
|
||||||
|
// decoder: &dyn Decoder,
|
||||||
|
// channel: &AwgnChannel,
|
||||||
|
// ) -> Result<()> {
|
||||||
|
// println!("[*] Chargement de l'image : {}", input_path);
|
||||||
|
// let img = image::open(input_path)
|
||||||
|
// .expect("Erreur de chargement de l'image")
|
||||||
|
// .to_rgb8();
|
||||||
|
// let (width, height) = img.dimensions();
|
||||||
|
// let raw_bytes = img.into_raw();
|
||||||
|
//
|
||||||
|
// let mut bits = bytes_to_bits(&raw_bytes);
|
||||||
|
// let original_bit_len = bits.len();
|
||||||
|
//
|
||||||
|
// // Padding
|
||||||
|
// let k = encoder.message_len();
|
||||||
|
// let remainder = bits.len() % k;
|
||||||
|
// if remainder != 0 {
|
||||||
|
// bits.resize(bits.len() + (k - remainder), 0);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// let num_blocks = bits.len() / k;
|
||||||
|
// println!(" - Taille: {}x{} pixels", width, height);
|
||||||
|
// println!(" - Blocs à transmettre (k={}): {}", k, num_blocks);
|
||||||
|
//
|
||||||
|
// let mut rng = rand::rngs::StdRng::seed_from_u64(42);
|
||||||
|
//
|
||||||
|
// let mut noisy_bits = Vec::with_capacity(num_blocks * k);
|
||||||
|
// let mut decoded_bits = Vec::with_capacity(num_blocks * k);
|
||||||
|
//
|
||||||
|
// let mut frame_errors = 0;
|
||||||
|
//
|
||||||
|
// println!("[*] Transmission et Décodage en cours...");
|
||||||
|
//
|
||||||
|
// for (i, block) in bits.chunks(k).enumerate() {
|
||||||
|
// if i % 100 == 0 && i > 0 {
|
||||||
|
// println!(" - Progession: {} / {} blocs...", i, num_blocks);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// let codeword = encoder.encode(block)?;
|
||||||
|
//
|
||||||
|
// let rx_llr = channel.transmit(&codeword, &mut rng);
|
||||||
|
//
|
||||||
|
// // Sans correction LDPC
|
||||||
|
// let hard_codeword: Vec<Gf2> = rx_llr
|
||||||
|
// .iter()
|
||||||
|
// .map(|&l| if l < 0.0 { 1 } else { 0 })
|
||||||
|
// .collect();
|
||||||
|
// let noisy_block = encoder.extract_message(&hard_codeword);
|
||||||
|
// noisy_bits.extend_from_slice(&noisy_block);
|
||||||
|
//
|
||||||
|
// // Décodage LDPC
|
||||||
|
// let res = decoder.decode(&rx_llr);
|
||||||
|
// if let Some(decoded_codeword) = res.codeword() {
|
||||||
|
// let decoded_msg = encoder.extract_message(decoded_codeword);
|
||||||
|
// decoded_bits.extend_from_slice(&decoded_msg);
|
||||||
|
//
|
||||||
|
// if decoded_msg != block {
|
||||||
|
// frame_errors += 1;
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// decoded_bits.extend_from_slice(&noisy_block);
|
||||||
|
// frame_errors += 1;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// println!(
|
||||||
|
// "[*] Transmission terminée. FER : {:.2}%",
|
||||||
|
// (frame_errors as f64 / num_blocks as f64) * 100.0
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// // Suppression du padding
|
||||||
|
// noisy_bits.truncate(original_bit_len);
|
||||||
|
// decoded_bits.truncate(original_bit_len);
|
||||||
|
//
|
||||||
|
// // Reconstitution des images
|
||||||
|
// let noisy_bytes = bits_to_bytes(&noisy_bits);
|
||||||
|
// let decoded_bytes = bits_to_bytes(&decoded_bits);
|
||||||
|
//
|
||||||
|
// let noisy_img = ImageBuffer::<Rgb<u8>, _>::from_raw(width, height, noisy_bytes)
|
||||||
|
// .expect("Erreur de reconstruction de l'image bruitée");
|
||||||
|
// noisy_img.save(noisy_out_path).unwrap();
|
||||||
|
//
|
||||||
|
// let decoded_img = ImageBuffer::<Rgb<u8>, _>::from_raw(width, height, decoded_bytes)
|
||||||
|
// .expect("Erreur de reconstruction de l'image décodée");
|
||||||
|
// decoded_img.save(decoded_out_path).unwrap();
|
||||||
|
//
|
||||||
|
// println!(
|
||||||
|
// "[*] Images sauvegardées : {} et {}",
|
||||||
|
// noisy_out_path, decoded_out_path
|
||||||
|
// );
|
||||||
|
// Ok(())
|
||||||
|
// }
|
||||||
|
pub fn transmit_image(
|
||||||
|
input_path: &str,
|
||||||
|
noisy_out_path: &str,
|
||||||
|
decoded_out_path: &str,
|
||||||
|
encoder: &dyn Encoder,
|
||||||
|
decoder: &dyn Decoder,
|
||||||
|
channel: &AwgnChannel,
|
||||||
|
) -> Result<()> {
|
||||||
|
println!("\n[*] Chargement de l'image : {}", input_path);
|
||||||
|
let img = image::open(input_path)
|
||||||
|
.expect("Erreur : Impossible de trouver ou lire l'image")
|
||||||
|
.to_rgb8();
|
||||||
|
let (width, height) = img.dimensions();
|
||||||
|
let raw_bytes = img.into_raw();
|
||||||
|
|
||||||
|
let mut bits = bytes_to_bits(&raw_bytes);
|
||||||
|
let original_bit_len = bits.len();
|
||||||
|
|
||||||
|
let k = encoder.message_len();
|
||||||
|
let remainder = bits.len() % k;
|
||||||
|
if remainder != 0 {
|
||||||
|
bits.resize(bits.len() + (k - remainder), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
let num_blocks = bits.len() / k;
|
||||||
|
println!(" - Résolution : {}x{} pixels", width, height);
|
||||||
|
println!(
|
||||||
|
" - Poids total : {:.2} Mo",
|
||||||
|
raw_bytes.len() as f64 / 1_048_576.0
|
||||||
|
);
|
||||||
|
println!(" - Découpage en : {} blocs de {} bits\n", num_blocks, k);
|
||||||
|
|
||||||
|
let start_time = Instant::now();
|
||||||
|
|
||||||
|
let pb = ProgressBar::new(num_blocks as u64);
|
||||||
|
pb.set_style(
|
||||||
|
ProgressStyle::default_bar()
|
||||||
|
.template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} blocs ({eta})")
|
||||||
|
.unwrap()
|
||||||
|
.progress_chars("=>-")
|
||||||
|
);
|
||||||
|
|
||||||
|
let results: Vec<_> = bits
|
||||||
|
.par_chunks(k)
|
||||||
|
.map(|block| {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let codeword = encoder.encode(block).unwrap();
|
||||||
|
let rx_llr = channel.transmit(&codeword, &mut rng);
|
||||||
|
let hard_codeword: Vec<Gf2> = rx_llr
|
||||||
|
.iter()
|
||||||
|
.map(|&l| if l < 0.0 { 1 } else { 0 })
|
||||||
|
.collect();
|
||||||
|
let noisy_block = encoder.extract_message(&hard_codeword);
|
||||||
|
let res = decoder.decode(&rx_llr);
|
||||||
|
|
||||||
|
let (decoded_block, has_error) = if let Some(decoded_codeword) = res.codeword() {
|
||||||
|
let decoded_msg = encoder.extract_message(decoded_codeword);
|
||||||
|
let err = if decoded_msg != block { 1 } else { 0 };
|
||||||
|
(decoded_msg, err)
|
||||||
|
} else {
|
||||||
|
(noisy_block.clone(), 1)
|
||||||
|
};
|
||||||
|
pb.inc(1);
|
||||||
|
(noisy_block, decoded_block, has_error)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
pb.finish_with_message("Décodage terminé !");
|
||||||
|
|
||||||
|
let mut noisy_bits = Vec::with_capacity(num_blocks * k);
|
||||||
|
let mut decoded_bits = Vec::with_capacity(num_blocks * k);
|
||||||
|
let mut frame_errors = 0;
|
||||||
|
|
||||||
|
for (n_block, d_block, err) in results {
|
||||||
|
noisy_bits.extend_from_slice(&n_block);
|
||||||
|
decoded_bits.extend_from_slice(&d_block);
|
||||||
|
frame_errors += err;
|
||||||
|
}
|
||||||
|
|
||||||
|
let duration = start_time.elapsed();
|
||||||
|
let fer = (frame_errors as f64 / num_blocks as f64) * 100.0;
|
||||||
|
println!("[*] Transmission terminée en {:.2?}", duration);
|
||||||
|
println!(" - Taux d'Erreur Trame (FER) : {:.2}%", fer);
|
||||||
|
|
||||||
|
noisy_bits.truncate(original_bit_len);
|
||||||
|
decoded_bits.truncate(original_bit_len);
|
||||||
|
|
||||||
|
let noisy_bytes = bits_to_bytes(&noisy_bits);
|
||||||
|
let decoded_bytes = bits_to_bytes(&decoded_bits);
|
||||||
|
|
||||||
|
let noisy_img = ImageBuffer::<Rgb<u8>, _>::from_raw(width, height, noisy_bytes)
|
||||||
|
.expect("Erreur de reconstruction de l'image bruitée");
|
||||||
|
noisy_img.save(noisy_out_path).unwrap();
|
||||||
|
|
||||||
|
let decoded_img = ImageBuffer::<Rgb<u8>, _>::from_raw(width, height, decoded_bytes)
|
||||||
|
.expect("Erreur de reconstruction de l'image décodée");
|
||||||
|
decoded_img.save(decoded_out_path).unwrap();
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"[*] Succès ! Images sauvegardées : {} et {}",
|
||||||
|
noisy_out_path, decoded_out_path
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@ -1,6 +1,5 @@
|
|||||||
pub mod benchmark;
|
pub mod benchmark;
|
||||||
pub mod benchmark2;
|
pub mod benchmark2;
|
||||||
pub mod benchmark3;
|
|
||||||
pub mod channel;
|
pub mod channel;
|
||||||
pub mod code;
|
pub mod code;
|
||||||
pub mod decoder;
|
pub mod decoder;
|
||||||
|
|||||||
144
src/rs/main.rs
Normal file
144
src/rs/main.rs
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
use ldpc::benchmark::get_or_generate_cached_code;
|
||||||
|
use ldpc::channel::AwgnChannel;
|
||||||
|
use ldpc::code::GenerationMethod;
|
||||||
|
use ldpc::decoder::{build_decoder, DecoderConfig, DecoderMethod};
|
||||||
|
use ldpc::encoder::{build_encoder, EncodingMethod};
|
||||||
|
use ldpc::image_sim::transmit_image;
|
||||||
|
|
||||||
|
// fn main() -> ldpc::Result<()> {
|
||||||
|
// let n = 1944;
|
||||||
|
// let k = 972;
|
||||||
|
// let wc = 3;
|
||||||
|
// let wr = 6;
|
||||||
|
//
|
||||||
|
// println!("Transmission d'image via code LDPC");
|
||||||
|
//
|
||||||
|
// let code_mn = get_or_generate_cached_code(
|
||||||
|
// n,
|
||||||
|
// k,
|
||||||
|
// wc,
|
||||||
|
// wr,
|
||||||
|
// GenerationMethod::MacKayNeal { max_attempts: 5000 },
|
||||||
|
// )?;
|
||||||
|
//
|
||||||
|
// let mut code = code_mn;
|
||||||
|
// let encoder = build_encoder(&mut code, EncodingMethod::Systematic)?;
|
||||||
|
//
|
||||||
|
// let config = DecoderConfig {
|
||||||
|
// max_iterations: 50,
|
||||||
|
// early_stopping: true,
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// // Sum-Product
|
||||||
|
// let decoder = build_decoder(&code, DecoderMethod::SumProduct, config);
|
||||||
|
//
|
||||||
|
// let channel = AwgnChannel::new(2.0, code.rate())?;
|
||||||
|
//
|
||||||
|
// transmit_image(
|
||||||
|
// "test.png",
|
||||||
|
// "noisy_out.png",
|
||||||
|
// "decoded_out.png",
|
||||||
|
// &*encoder,
|
||||||
|
// &*decoder,
|
||||||
|
// &channel,
|
||||||
|
// )?;
|
||||||
|
//
|
||||||
|
// Ok(())
|
||||||
|
// }
|
||||||
|
|
||||||
|
use clap::Parser;
|
||||||
|
use ldpc::benchmark2::{run_massive_campaign, CampaignConfig, CodeScenario};
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
#[command(author, version, about = "Laboratoire de test LDPC - TIPE")]
|
||||||
|
struct Args {
|
||||||
|
#[arg(short, long, default_value_t = 1000)]
|
||||||
|
trials: usize,
|
||||||
|
|
||||||
|
/// Générer les fichiers .dot pour visualiser les graphes de Tanner
|
||||||
|
#[arg(long, default_value_t = false)]
|
||||||
|
export_graph: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> ldpc::Result<()> {
|
||||||
|
let args = Args::parse();
|
||||||
|
let snrs = vec![1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.5, 4.0];
|
||||||
|
let mut scenarios = Vec::new();
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// SCÉNARIO "TOY MODEL" : Spécial pour ton document Typst
|
||||||
|
// Un code minuscule R=1/2 avec n=24, k=12, m=12.
|
||||||
|
// Lisible sur une feuille A4.
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
if args.export_graph {
|
||||||
|
scenarios.push(CodeScenario {
|
||||||
|
name: "ToyModel_Typst_N24".into(),
|
||||||
|
n: 24,
|
||||||
|
k: 12,
|
||||||
|
wc: 3,
|
||||||
|
wr: 6,
|
||||||
|
method: GenerationMethod::Gallager,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Les vrais tests pour tes courbes de performances
|
||||||
|
scenarios.push(CodeScenario {
|
||||||
|
name: "Gallager_N1296_R05".into(),
|
||||||
|
n: 1296,
|
||||||
|
k: 648,
|
||||||
|
wc: 3,
|
||||||
|
wr: 6,
|
||||||
|
method: GenerationMethod::Gallager,
|
||||||
|
});
|
||||||
|
|
||||||
|
scenarios.push(CodeScenario {
|
||||||
|
name: "MacKay_N1296_R05".into(),
|
||||||
|
n: 1296,
|
||||||
|
k: 648,
|
||||||
|
wc: 3,
|
||||||
|
wr: 6,
|
||||||
|
method: GenerationMethod::MacKayNeal { max_attempts: 1000 },
|
||||||
|
});
|
||||||
|
|
||||||
|
let config = CampaignConfig {
|
||||||
|
scenarios,
|
||||||
|
snr_range: snrs,
|
||||||
|
n_trials: args.trials,
|
||||||
|
max_iterations: 50,
|
||||||
|
output_csv: "tipe_results.csv".into(),
|
||||||
|
export_graph: args.export_graph,
|
||||||
|
};
|
||||||
|
|
||||||
|
run_massive_campaign(config)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// use ldpc::benchmark::run_simulation;
|
||||||
|
//
|
||||||
|
// fn main() -> ldpc::Result<()> {
|
||||||
|
// let n = 1944;
|
||||||
|
// let k = 972;
|
||||||
|
// let wc = 3;
|
||||||
|
// let wr = 6;
|
||||||
|
//
|
||||||
|
// println!("Benchmark: MacKayNeal vs Gallager");
|
||||||
|
// println!();
|
||||||
|
//
|
||||||
|
// println!("Test 1: Génération MacKayNeal\n");
|
||||||
|
// let code_mn = get_or_generate_cached_code(
|
||||||
|
// n,
|
||||||
|
// k,
|
||||||
|
// wc,
|
||||||
|
// wr,
|
||||||
|
// GenerationMethod::MacKayNeal { max_attempts: 5000 },
|
||||||
|
// )?;
|
||||||
|
//
|
||||||
|
// run_simulation(code_mn)?;
|
||||||
|
//
|
||||||
|
// println!("\nTest 2 : Génération Gallager\n");
|
||||||
|
// let code_gal = get_or_generate_cached_code(n, k, wc, wr, GenerationMethod::Gallager)?;
|
||||||
|
// run_simulation(code_gal)?;
|
||||||
|
//
|
||||||
|
// Ok(())
|
||||||
|
// }
|
||||||
Reference in New Issue
Block a user