WH-16 - Created separate folder for updated POF Protocol. Added C model of binary-Golay encoding/decoding process, reflecting the proposed usage of this code in the updated protocol
This commit is contained in:
parent
4e7514529c
commit
6ef5e61241
7 changed files with 12572 additions and 0 deletions
|
|
@ -0,0 +1,4 @@
|
|||
#!/bin/sh
|
||||
gcc golay23.c -o golay23
|
||||
gcc golay23-gen.c -o golay23-gen
|
||||
gcc golay23_codec.c -o golay23_codec
|
||||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
Binary file not shown.
|
|
@ -0,0 +1,280 @@
|
|||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Title : golay23_codec
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// File : golay_codec.c
|
||||
// Author : Gianmarco Girau
|
||||
// Company : Global Inkjet Systems
|
||||
// Target :
|
||||
// Dependency : none
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Description: Binary Golay ([23,12,7]) encoder/decoder simulator
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Last update: 2019-09-06
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Revisions :
|
||||
// Date Version Author Description
|
||||
// 2019-09-06 1.0 gmg Created
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define USE_ENCODER_LUT 0 // 0 => Manually Encode, 1 => Use LUT to encode data
|
||||
#define GEN_ERROR_PATTERN 1 // 1 => Generate the error pattern list, 0 => fetch the list from file
|
||||
|
||||
#define ENCODE_TABLE_FILE "encoding_table.txt"
|
||||
#define DECODE_TABLE_FILE "decoding_table.txt"
|
||||
#define SYNDROME_TABLE_FILE "syndrome_table.txt"
|
||||
#define ERROR_TABLE_FILE "error_patterns.txt"
|
||||
|
||||
#define GENPOL 0x00000c75 // Golay 23 generator polynomial, g(x)
|
||||
|
||||
int syndrome_lut[4096]; // Golay Syndrome
|
||||
int encode_lut[4096]; // Golay encoded codewords
|
||||
int decode_lut[2048]; // Correction patterns, for every possible correctable error
|
||||
int error_pattern[2048]; // list of correctable error pattern (up to 3 error bits)
|
||||
|
||||
|
||||
// Compute the syndrome (tx version)
|
||||
int gen_syndrome_tx(int pattern)
|
||||
{
|
||||
int i;
|
||||
|
||||
// make sure the pattern is a valid codeword
|
||||
pattern &= 0x7FFFFF;
|
||||
for(i = 0; i < 12; i++)
|
||||
{
|
||||
if(pattern & 0x1)
|
||||
pattern ^= GENPOL;
|
||||
pattern >>=1;
|
||||
}
|
||||
return (0xFFF & pattern);
|
||||
}
|
||||
|
||||
// Compute the syndrome (rx version)
|
||||
int gen_syndrome_rx(int pattern)
|
||||
{
|
||||
int i;
|
||||
|
||||
pattern &= 0x7FFFFF;
|
||||
for(i = 11; i >= 0; i--)
|
||||
{
|
||||
if(pattern & 0x1<<(i+11))
|
||||
pattern ^= GENPOL << i;
|
||||
}
|
||||
return(pattern);
|
||||
}
|
||||
|
||||
// Golay Encoder
|
||||
int golay_encode(int data)
|
||||
{
|
||||
#if USE_ENCODER_LUT == 0
|
||||
return encode_lut[0xFFF & data];
|
||||
#else
|
||||
gen_syndrome_tx(0xFFF & data) + (data << 11);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Golay Decoder
|
||||
void golay_decode(int rx_data, int *decoded_data, int *detected_value, int *syn, int *err_corr)
|
||||
{
|
||||
int rx_syn, rx_error, decoded_value;
|
||||
|
||||
// compute the syndrome for the input data
|
||||
//printf("Decoding rx_data = %06X:\n",rx_data);
|
||||
rx_syn = gen_syndrome_rx(rx_data);
|
||||
//printf("Syndrome for %06X is %03X\n",rx_data, rx_syn);
|
||||
rx_error = decode_lut[rx_syn];
|
||||
//printf("error correction pattern is %06X\n",rx_error);
|
||||
|
||||
decoded_value = (rx_data ^ rx_error);
|
||||
|
||||
*decoded_data = decoded_value >> 11;
|
||||
|
||||
*syn = rx_syn;
|
||||
*err_corr = rx_error;
|
||||
|
||||
//printf("decoded-data = %03X, decoded-value = %06X\n", *decoded_data, decoded_value);
|
||||
}
|
||||
|
||||
// compare the data before the encoding, with the decoded data, return the number of bit errors
|
||||
int check_errors(int data, int dec_data)
|
||||
{
|
||||
int error_num, error_data, n;
|
||||
|
||||
error_data = data ^ dec_data;
|
||||
|
||||
for(n = 0, error_num = 0; n < 12; n++)
|
||||
{
|
||||
if(error_data & 0x1)
|
||||
error_num++;
|
||||
error_data >>= 1;
|
||||
}
|
||||
return error_num;
|
||||
}
|
||||
|
||||
int count_logic_ones(int num, int width)
|
||||
{
|
||||
int n, count;
|
||||
for(n = 0, count = 0; n < width; n++, num>>=1)
|
||||
if(num & 0x1)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
// Generate the list of all possible correctable patterns [2048], through exaustive search (2^23 patterns to be considered)
|
||||
// Note:
|
||||
// 1 pattern with 0-bit errors
|
||||
// 23 patterns with 1-bit errors,
|
||||
// 253 patterns with 2-bits errors,
|
||||
// 1771 patterns with 3-bits errors.
|
||||
// => 1+23+253+1771 =2048 correctable patterns
|
||||
void gen_error_pattern(void)
|
||||
{
|
||||
int n, m;
|
||||
FILE *fp;
|
||||
|
||||
for(n = 0, m = 0; n < 8388608; n++)
|
||||
if(count_logic_ones(n, 24) <= 3)
|
||||
error_pattern[m++] = n;
|
||||
|
||||
// save the list to file
|
||||
// Print the syndrome table
|
||||
if((fp = fopen(ERROR_TABLE_FILE, "w")) == NULL)
|
||||
{
|
||||
printf("ERROR: cannot create the error pattern table file\n");
|
||||
return;
|
||||
}
|
||||
for(m = 0; m < 2048; m++)
|
||||
fprintf(fp, "%06X\n", (unsigned int)(0xFFFFFF & error_pattern[m]));
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int n, data, encoded_data, rx_data, decoded_data, detected_value, rx_syn, err_corr;
|
||||
|
||||
FILE *fp;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Fetch syndrome, encoding and decoding LUTs
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Syndrome LUT:
|
||||
if((fp = fopen(SYNDROME_TABLE_FILE, "r")) == NULL)
|
||||
{
|
||||
printf("ERROR: cannot open the syndrome table file\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for(n = 0; n < 4096; n++)
|
||||
if(fscanf(fp, "%03X", &syndrome_lut[n]) != 1)
|
||||
{
|
||||
printf("ERROR: cannot read syndrome table element %d\n", n);
|
||||
return -1;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
// Encoding LUT:
|
||||
if((fp = fopen(ENCODE_TABLE_FILE, "r")) == NULL)
|
||||
{
|
||||
printf("ERROR: cannot open the encoding table file\n");
|
||||
return -1;
|
||||
}
|
||||
for(n = 0; n < 4096; n++)
|
||||
if(fscanf(fp, "%06X", &encode_lut[n]) != 1)
|
||||
{
|
||||
printf("ERROR: cannot read encoding table element %d\n", n);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
// Decoding LUT:
|
||||
if((fp = fopen(DECODE_TABLE_FILE, "r")) == NULL)
|
||||
{
|
||||
printf("ERROR: cannot open the decoding table file\n");
|
||||
return -1;
|
||||
}
|
||||
for(n = 0; n < 2048; n++)
|
||||
if(fscanf(fp, "%06X", &decode_lut[n]) != 1)
|
||||
{
|
||||
printf("ERROR: cannot read decoding table element %d\n", n);
|
||||
return -1;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Generate or fetch syndrome correctable error patterns
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if GEN_ERROR_PATTERN == 1
|
||||
// generate the list of correctable errors
|
||||
gen_error_pattern();
|
||||
|
||||
#else
|
||||
// fetch the list from list
|
||||
if((fp = fopen(ERROR_TABLE_FILE, "r")) == NULL)
|
||||
{
|
||||
printf("ERROR: cannot open the error patterns table file\n");
|
||||
return -1;
|
||||
}
|
||||
for(n = 0; n < 2048; n++)
|
||||
if(fscanf(fp, "%06X", &error_pattern[n]) != 1)
|
||||
{
|
||||
printf("ERROR: cannot read error pattern element %d\n", n);
|
||||
return -1;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Check Encode/Decode process, without errors
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
printf("Testing Encoding/Decoding process without errors ... ");
|
||||
for(data = 0; data < 4096; data++)
|
||||
{
|
||||
// encode the data
|
||||
encoded_data = encode_lut[data];
|
||||
// decode the data
|
||||
golay_decode(encoded_data, &decoded_data, &detected_value, &rx_syn, &err_corr);
|
||||
if(decoded_data != data)
|
||||
{
|
||||
printf("\nERROR : data = %03X, encoded-data = %06X, decoded-data = %03X\n", data, encoded_data, decoded_data);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
printf("successfully completed. \n");
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Check Encode/Decode process, with errors
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
for(n = 0; n < 2048; n++)
|
||||
printf("decode_table[%04d] = %06X\n", n, decode_lut[n]);
|
||||
*/
|
||||
|
||||
printf("Testing Encoding/Decoding process with errors ... ");
|
||||
for(data = 0; data < 4096; data++)
|
||||
{
|
||||
// encode the data
|
||||
encoded_data = encode_lut[data];
|
||||
for(n = 0; n < 2048; n++)
|
||||
{
|
||||
rx_data = encoded_data ^ error_pattern[n];
|
||||
golay_decode(rx_data, &decoded_data, &detected_value, &rx_syn, &err_corr);
|
||||
if(decoded_data != data)
|
||||
{
|
||||
printf("\nERROR : data = %03X, encoded-data = %06X, rx_data = %06X, rx-syndrome = %06X, err_corr = %06X, decoded-data = %03X\n", data, encoded_data, rx_data, rx_syn, err_corr, decoded_data);
|
||||
//printf("\nERROR : data = %03X, encoded-data = %06X, rx_data = %06X, error_pattern = %06X, rx-syndrome = %d, err_corr = %06X, decoded-data = %03X\n", data, encoded_data, rx_data, error_pattern[n], rx_syn, decode_lut[rx_syn], decoded_data);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("successfully completed. \n");
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue