From b86e88413d4c6ec428aaedb147f7675f28882fe4 Mon Sep 17 00:00:00 2001 From: drowe67 Date: Fri, 14 Jul 2023 12:36:50 +0930 Subject: clang-format -i applied to src unittest misc --- src/ldpc_dec_test.c | 586 +++++++++++++++++++++++++++------------------------- 1 file changed, 302 insertions(+), 284 deletions(-) (limited to 'src/ldpc_dec_test.c') diff --git a/src/ldpc_dec_test.c b/src/ldpc_dec_test.c index d26fd52..9ba63b0 100644 --- a/src/ldpc_dec_test.c +++ b/src/ldpc_dec_test.c @@ -1,4 +1,4 @@ -/* +/* FILE...: ldpc_dec.c AUTHOR.: Matthew C. Valenti, Rohit Iyer Seshadri, David Rowe CREATED: Sep 2016 @@ -26,9 +26,10 @@ #include #include #include +#include #include #include -#include + #include "mpdecode_core_test.h" #include "ofdm_internal.h" @@ -37,320 +38,337 @@ /* TODO: Better octave/C support for multuple codes */ -#include "H2064_516_sparse_test.h" -#include "HRA_112_112_test.h" +#include "H2064_516_sparse_test.h" +#include "HRA_112_112_test.h" int opt_exists(char *argv[], int argc, char opt[]) { - int i; - for (i=0; i 0) { + ldpc.max_iter = atoi(argv[i + 1]); + fprintf(stderr, "max_iter: %d\n", ldpc.max_iter); + } - if ((codename = opt_exists(argv, argc, "--listcodes")) != 0) { - fprintf(stderr,"\n"); - fprintf(stderr,"H2064_516_sparse\n"); - fprintf(stderr,"HRA_112_112\n"); - fprintf(stderr,"\n"); - exit(0); - } + CodeLength = ldpc.CodeLength; /* length of entire codeword */ + NumberParityBits = ldpc.NumberParityBits; + data_bits_per_frame = ldpc.NumberRowsHcols; + unsigned char ibits[data_bits_per_frame]; + unsigned char pbits[NumberParityBits]; + char out_char[CodeLength]; - /* default Wenet High Alitiude Balloon rate 0.8 code */ - - ldpc.max_iter = MAX_ITER; - ldpc.dec_type = 0; - ldpc.q_scale_factor = 1; - ldpc.r_scale_factor = 1; - ldpc.CodeLength = CODELENGTH; - ldpc.NumberParityBits = NUMBERPARITYBITS; - ldpc.NumberRowsHcols = NUMBERROWSHCOLS; - ldpc.max_row_weight = MAX_ROW_WEIGHT; - ldpc.max_col_weight = MAX_COL_WEIGHT; - ldpc.H_rows = H_rows; - ldpc.H_cols = H_cols; - ainput = input; - adetected_data = detected_data; - - if ((codename = opt_exists(argv, argc, "--code")) != 0) { - - /* short rate 1/2 code for FreeDV HF digital voice */ - - if (strcmp(argv[codename+1], "HRA_112_112") == 0) { - fprintf(stderr, "code: %s\n", argv[codename+1]); - ldpc.max_iter = HRA_112_112_MAX_ITER; - ldpc.dec_type = 0; - ldpc.q_scale_factor = 1; - ldpc.r_scale_factor = 1; - ldpc.CodeLength = HRA_112_112_CODELENGTH; - ldpc.NumberParityBits = HRA_112_112_NUMBERPARITYBITS; - ldpc.NumberRowsHcols = HRA_112_112_NUMBERROWSHCOLS; - ldpc.max_row_weight = HRA_112_112_MAX_ROW_WEIGHT; - ldpc.max_col_weight = HRA_112_112_MAX_COL_WEIGHT; - ldpc.H_rows = HRA_112_112_H_rows; - ldpc.H_cols = HRA_112_112_H_cols; - ainput = HRA_112_112_input; - adetected_data = HRA_112_112_detected_data; - } - } - - if ((i = opt_exists(argv, argc, "--max_iter")) > 0) { - ldpc.max_iter = atoi(argv[i+1]); - fprintf(stderr, "max_iter: %d\n", ldpc.max_iter); - } + testframes = 0; + total_iters = 0; + Tbits = Terrs = Tbits_raw = Terrs_raw = 0; - CodeLength = ldpc.CodeLength; /* length of entire codeword */ - NumberParityBits = ldpc.NumberParityBits; - data_bits_per_frame = ldpc.NumberRowsHcols; - unsigned char ibits[data_bits_per_frame]; - unsigned char pbits[NumberParityBits]; - char out_char[CodeLength]; + if (!strcmp(argv[1], "--test")) { + /* test mode --------------------------------------------------------*/ - testframes = 0; - total_iters = 0; - Tbits = Terrs = Tbits_raw = Terrs_raw = 0; + fprintf(stderr, "Starting test using pre-compiled test data .....\n"); + fprintf(stderr, "Codeword length: %d\n", CodeLength); + fprintf(stderr, "Parity Bits....: %d\n", NumberParityBits); - if (!strcmp(argv[1],"--test")) { + num_runs = 1; + num_ok = 0; - /* test mode --------------------------------------------------------*/ + for (r = 0; r < num_runs; r++) { + iter = run_ldpc_decoder(&ldpc, out_char, ainput, &parityCheckCount); + // fprintf(stderr, "iter: %d\n", iter); + total_iters += iter; - fprintf(stderr, "Starting test using pre-compiled test data .....\n"); - fprintf(stderr, "Codeword length: %d\n", CodeLength); - fprintf(stderr, "Parity Bits....: %d\n", NumberParityBits); + int ok = 0; + for (i = 0; i < CodeLength; i++) { + if (out_char[i] == adetected_data[i]) ok++; + } - num_runs = 1; num_ok = 0; - - for(r=0; r 16384; + } + encode(&ldpc, ibits, pbits); } - else { - FILE *fin, *fout; - int sdinput, readhalfframe, nread, offset; - /* File I/O mode ------------------------------------------------*/ + double *input_double = calloc(CodeLength, sizeof(double)); + float *input_float = calloc(CodeLength, sizeof(float)); + + nread = CodeLength; + offset = 0; + if (readhalfframe) { + nread = CodeLength / 2; + offset = CodeLength / 2; + for (i = 0; i < offset; i++) { + input_double[i] = 0.0; + } + } - if (strcmp(argv[1], "-") == 0) fin = stdin; - else if ( (fin = fopen(argv[1],"rb")) == NULL ) { - fprintf(stderr, "Error opening input SD file: %s: %s.\n", - argv[1], strerror(errno)); - exit(1); + fprintf(stderr, "Codeword length: %d\n", CodeLength); + fprintf(stderr, "Parity Bits....: %d\n", NumberParityBits); + + // frame = 0; + while (fread(&input_double[offset], sizeof(double), nread, fin) == nread) { + // fprintf(stderr, "Frame %d\n", frame); + if (sdinput) { + if (testframes) { + char in_char; + for (i = 0; i < data_bits_per_frame; i++) { + in_char = input_double[i] < 0; + if (in_char != ibits[i]) { + Terrs_raw++; + } + Tbits_raw++; + } + for (i = 0; i < NumberParityBits; i++) { + in_char = input_double[i + data_bits_per_frame] < 0; + if (in_char != pbits[i]) { + Terrs_raw++; + } + Tbits_raw++; + } } - - if (strcmp(argv[2], "-") == 0) fout = stdout; - else if ( (fout = fopen(argv[2],"wb")) == NULL ) { - fprintf(stderr, "Error opening output bit file: %s: %s.\n", - argv[2], strerror(errno)); - exit(1); + sd_to_llr(input_float, input_double, CodeLength); + } + + iter = run_ldpc_decoder(&ldpc, out_char, input_float, &parityCheckCount); + // fprintf(stderr, "iter: %d\n", iter); + total_iters += iter; + + if (mute) { + // Output data bits if decoder converged, or was + // within 10% of all parity checks converging (10% est + // BER). useful for real world operation as it can + // resync and won't send crappy packets to the decoder + + float ber_est = (float)(ldpc.NumberParityBits - parityCheckCount) / + ldpc.NumberParityBits; + // fprintf(stderr, "iter: %4d parityCheckErrors: %4d ber: %3.2f\n", + // iter, ldpc.NumberParityBits - parityCheckCount, ber_est); + if (ber_est < 0.1) { + fwrite(out_char, sizeof(char), ldpc.NumberRowsHcols, fout); } - sdinput = 0; - readhalfframe = 0; - mute = 0; state = 0; hframe = 0; - if (opt_exists(argv, argc, "--sd")) { - sdinput = 1; + } else { + if (readhalfframe) { + // Establish which half hframe we want to sync on, + // used for testing with cohpsk_put_bits, as it + // maintains sync with test bits state machine. + + next_state = state; + switch (state) { + case 0: + if (iter < ldpc.max_iter) { + /* OK we've found which hframe to sync on */ + next_state = 1; + hframe = 0; + } + break; + case 1: + hframe++; + if ((hframe % 2) == 0) { + /* write decoded packets every second input hframe */ + fwrite(out_char, sizeof(char), ldpc.NumberRowsHcols, fout); + } + break; + } + state = next_state; + // fprintf(stderr, "state: %d iter: %d\n", state, iter); } - if (opt_exists(argv, argc, "--half")) { - readhalfframe = 1; - } - if (opt_exists(argv, argc, "--mute")) { - mute = 1; - } - if (opt_exists(argv, argc, "--testframes")) { - testframes = 1; - uint16_t r[data_bits_per_frame]; - ofdm_rand(r, data_bits_per_frame); - for(i=0; i 16384; - } - encode(&ldpc, ibits, pbits); - } - - double *input_double = calloc(CodeLength, sizeof(double)); - float *input_float = calloc(CodeLength, sizeof(float)); - - nread = CodeLength; - offset = 0; - if (readhalfframe) { - nread = CodeLength/2; - offset = CodeLength/2; - for(i=0; i