📄 reedsolomonproto.cpp
字号:
/* ********************************************************************* * * * Galois Field Arithmetic Library * * Prototype: Reed-Solomon Encoder/Decoder Prototype * * Author: Arash Partow - 2000 * * URL: http://www.partow.net/projects/galois/index.html * * * * Copyright Notice: * * Free use of this library is permitted under the guidelines and * * in accordance with the most current version of the Common Public * * License. * * http://www.opensource.org/licenses/cpl.php * * * **********************************************************************/#include <iostream>#include <string>#include <stdlib.h>#include <stdio.h>#include <time.h>#include "GaloisField.h"#include "GaloisFieldElement.h"#include "GaloisFieldPolynomial.h"#include "SequentialRootGeneratorPolynomialCreator.h"#include "ReedSolomonBlock.h"#include "ReedSolomonEncoder.h"#include "ReedSolomonDecoder.h"const unsigned int FIELD_DESCRIPTOR = 8; // Symbol size, also m in GF(2^m)const unsigned int CODE_LENGTH = 255;const unsigned int FEC_LENGTH = 32;const unsigned int GENPOLY_INDEX = 120; // Generator polynomial sequence index in alpha terms.const unsigned int GENPOLY_TERM_CNT = 32; // Number of root terms in generator polynomial/* p(x) = 1x^8+1x^7+0x^6+0x^5+0x^4+0x^3+1x^2+1x^1+1x^0 1 1 0 0 0 0 1 1 1*/const unsigned int poly[9] = {1,1,1,0,0,0,0,1,1};unsigned char add_one_bit_error(unsigned char c){ const unsigned char mask[8] = { 0x01, //00000001 0x02, //00000010 0x04, //00000100 0x08, //00001000 0x10, //00010000 0x20, //00100000 0x40, //01000000 0x80 //10000000 }; unsigned int bit_pos = (unsigned int)(8.0 * rand() / (RAND_MAX + 1.0)); if ((c & mask[bit_pos]) == mask[bit_pos]) return c & (~mask[bit_pos]); else return c |= mask[bit_pos];}unsigned char add_two_bit_error(unsigned char c){ const unsigned char mask[8] = { 0x01, //00000001 0x02, //00000010 0x04, //00000100 0x08, //00001000 0x10, //00010000 0x20, //00100000 0x40, //01000000 0x80 //10000000 }; unsigned int bit_pos = (unsigned int)(4.0 * rand() / (RAND_MAX + 1.0)); if ((c & mask[bit_pos]) == mask[bit_pos]) c &= (~mask[bit_pos]); else c |= mask[bit_pos]; bit_pos = (unsigned int)(4.0 * rand() / (RAND_MAX + 1.0)) + 4; if ((c & mask[bit_pos]) == mask[bit_pos]) return c & (~mask[bit_pos]); else return c |= mask[bit_pos];}void corrupt_message(std::string& message){ /* corrupt data */ srand ((unsigned int)time(NULL)); const unsigned int error_position[] = {3,7,10,15,21,28,33,38,45,53,60,70,80,90,99,107}; for(unsigned int i = 0; i < (unsigned int)(sizeof(error_position) / sizeof(unsigned int)); i++) { //message[error_position[i]] = add_one_bit_error(message[error_position[i]]); message[error_position[i]] = add_two_bit_error(message[error_position[i]]); }}int main(int argc, char *argv[]){ galois::GaloisField gf(FIELD_DESCRIPTOR,poly); galois::SequentialRootGeneratorPolynomialCreator generator(&gf,GENPOLY_INDEX,GENPOLY_TERM_CNT); galois::GaloisFieldPolynomial gen_poly = generator.create(); reedsolomon::ReedSolomonBlock rs_block; reedsolomon::ReedSolomonEncoder rsencoder(&gf,gen_poly,CODE_LENGTH,FEC_LENGTH); reedsolomon::ReedSolomonDecoder rsdecoder(&gf,GENPOLY_INDEX,CODE_LENGTH,FEC_LENGTH); std::string data = "A professional is a person who knows more and more about less and less until they know everything about nothing"; data = data + std::string((CODE_LENGTH - FEC_LENGTH) - data.length(),0x0); std::cout << "Original Message:\t" << data << std::endl; if (!rsencoder.encode(data,rs_block)) { std::cout << "Error during encoding!" << std::endl; exit(1); } corrupt_message(rs_block.data); std::cout << "Corrupted Message:\t" << rs_block.data << std::endl; std::vector <unsigned int> erasures; if(!rsdecoder.decode(rs_block, erasures)) { std::cout << "Error during decoding!" << std::endl; exit(1); } else if (rs_block.data != data) { std::cout << "Error - Correction failed!" << std::endl; for (unsigned int i = 0; i < rs_block.data.length(); i++) { if (rs_block.data[i] != data[i]) std::cout << "Error @ " << i << std::endl; } exit(1); } std::cout << "Decoded Message:\t" << rs_block.data << std::endl; exit(EXIT_SUCCESS);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -