int main()
{
   /* Finite Field Parameters */
   const std::size_t field_descriptor                =  14;
   const std::size_t generator_polynomial_index      = 120;
   const std::size_t generator_polynomial_root_count = 100;

   /* Reed Solomon Code Parameters */
   const std::size_t code_length = 16383; //(2^14 - 1)
   const std::size_t fec_length  =   100;
   const std::size_t data_length = code_length - fec_length;

   /* 14-bit Symbol Mask Parameter */
   const int mask = 0x00003FFF;

   /*
     Note: One must make sure to be using primitive polynomials
           of correct degree for generating elements in the specified
           field ie: a primitive polynomial of degree 14 for GF(2^14).
   */

   /* Instantiate Finite Field and Generator Polynomials */
   schifra::galois::field field(field_descriptor,
                                schifra::galois::primitive_polynomial_size12,
                                schifra::galois::primitive_polynomial12);

   schifra::galois::field_polynomial generator_polynomial(field);

   schifra::sequential_root_generator_polynomial_creator(field,
                                                         generator_polynomial_index,
                                                         generator_polynomial_root_count,
                                                         generator_polynomial);

   /* Instantiate Encoder and Decoder (Codec) */
   schifra::reed_solomon::encoder<code_length,fec_length> encoder(field,generator_polynomial);
   schifra::reed_solomon::decoder<code_length,fec_length> decoder(field,generator_polynomial_index);

   /*
     Note: The data length represents the number of code symbols that will be used.
           The effective data length is then the number of code symbols multipled
           by the number of bits per symbol, in this case it is 14-bits per code
           symbol.
   */

   /* Instantiate RS Block For Codec */
   schifra::reed_solomon::block<code_length,fec_length> block;
   schifra::reed_solomon::block<code_length,fec_length> original_block;

   /* Populate RS Blocks with 14-bit data symbols */
   for (std::size_t i = 0; i < data_length; ++i)
   {
      block[i] = static_cast<int>(i & mask);
      original_block[i] = static_cast<int>(i & mask);
   }

   /* Transform message into Reed-Solomon encoded codeword */
   if (!encoder.encode(block))
   {
      std::cout << "Error - Critical encoding failure!" << std::endl;
      return 1;
   }

   /* Add errors at every 3rd location starting at position zero */
   schifra::corrupt_message_all_errors_wth_mask(block,0,mask,3);

   if (!decoder.decode(block))
   {
      std::cout << "Error - Critical decoding failure!" << std::endl;
      return 1;
   }
   else if (!schifra::are_blocks_equivelent(block,original_block,data_length))
   {
      std::cout << "Error - Error correction failed!" << std::endl;
      return 1;
   }

   std::cout << "Encoder Parameters [" << schifra::reed_solomon::encoder<code_length,fec_length>::trait::code_length << ","
                                       << schifra::reed_solomon::encoder<code_length,fec_length>::trait::data_length << ","
                                       << schifra::reed_solomon::encoder<code_length,fec_length>::trait::fec_length  << "]" << std::endl;

   std::cout << "Decoder Parameters [" << schifra::reed_solomon::decoder<code_length,fec_length>::trait::code_length << ","
                                       << schifra::reed_solomon::decoder<code_length,fec_length>::trait::data_length << ","
                                       << schifra::reed_solomon::decoder<code_length,fec_length>::trait::fec_length  << "]" << std::endl;

   return 0;
}
Esempio n. 2
0
//template <std::size_t code_length, std::size_t fec_length>
//void double_array_decode(std::list<schifra::reed_solomon::block<code_length, fec_length> >list, double* d) {
double* double_array_decode(MYCONTAINER list, double* d) {
	std::cout << "[decode]# of blocks: " << list.size() << std::endl;
	std::size_t field_descriptor = 8;
	std::size_t generator_poly_index = 120;
	std::size_t generator_poly_root_count = FEC_LENGTH;
	
	schifra::galois::field field(field_descriptor,
				     schifra::galois::primitive_polynomial_size06,
				     schifra::galois::primitive_polynomial06);
	schifra::galois::field_polynomial generator_polynomial(field);
	schifra::sequential_root_generator_polynomial_creator(field,
							      generator_poly_index,
							      generator_poly_root_count,
							      generator_polynomial);
	schifra::reed_solomon::decoder<CODE_LENGTH, FEC_LENGTH> decoder(field, generator_poly_index);
	/* Iterate through the list and decode */

	MYCONTAINER::iterator itr_list; // Can't use templates "code_length" and "fec_length" here.....
	int blksDecoded = 0, message_len=0, pArray=0, arrayLen=0;
	bool isNewArray = false;
	for(itr_list = list.begin(); itr_list != list.end(); itr_list ++) {
//		schifra::reed_solomon::block<code_length, fec_length> blk = *itr_list;
		schifra::reed_solomon::block<CODE_LENGTH, FEC_LENGTH> blk = *itr_list;
		if(!decoder.decode(blk)) {
//			printf("Decode Error.\n");
		} else {
			int ptr=0;
//			printf("Decode OK!\n");
			std::string message = std::string((CODE_LENGTH - FEC_LENGTH), static_cast<unsigned char>(0xC0));
			blk.data_to_string(message);
			const char* c=message.c_str();
//			char* c = strdup(message.c_str());
			message_len = message.length();
//			printf("[decode] String Length: %d\n", message_len);
			if(blksDecoded==0) {
				double k;
				int i; for(i=0; i<(int)(sizeof(double)); i++) {
					unsigned char p = *((unsigned char*)c + ptr);
//					printf("%d ", p);
					p = p ^ MYMASK;
					*(unsigned char*)((unsigned char*)&k+i)=p;
					ptr++;
				}
				printf("[decode]Array Length: %d\n", (int)k);
				arrayLen = (int)k;
				if(d==NULL) { printf("[decode] allocating result. %d elems.\n", arrayLen);
						d=(double*)malloc(sizeof(double)*arrayLen); isNewArray = true;}
			}
			int nElemsRounded = sizeof(double) * (int)((CODE_LENGTH - FEC_LENGTH)/sizeof(double));
			while(ptr < nElemsRounded) {
				double k;
				int i; for(i=0; i<(int)(sizeof(double)); i++) {
					unsigned char p = *((unsigned char*)c + ptr);
//					printf("%d ", p);
					p = p ^ MYMASK;
					*(unsigned char*)((unsigned char*)&k+i)=p;
					ptr++;
				}
//				printf("=%g ", k);
				if(pArray <= arrayLen-1) {
					if(isNewArray==false && d[pArray] != k) { printf("[decode] Detected difference: d[%d]=%g (should be %g)\n}", pArray, d[pArray], k); }
					d[pArray]=k;
					pArray++;
				}
			}
			blksDecoded++;
		}
	}
	return d;
}
Esempio n. 3
0
//template <std::size_t code_length, std::size_t fec_length>
//MYCONTAINER double_array_encode(double* d, int length) {
MYCONTAINER double_array_decode(double* d, int length) {
	std::cout << "[encode]";
	printf("Length: %d\n", length);
	std::size_t field_descriptor = 8;
	std::size_t generator_poly_index = 120;
	std::size_t generator_poly_root_count = FEC_LENGTH;
	
	schifra::galois::field field(field_descriptor,
				     schifra::galois::primitive_polynomial_size06,
				     schifra::galois::primitive_polynomial06);
	schifra::galois::field_polynomial generator_polynomial(field);
	schifra::sequential_root_generator_polynomial_creator(field,
							      generator_poly_index,
							      generator_poly_root_count,
							      generator_polynomial);
	schifra::reed_solomon::encoder<CODE_LENGTH, FEC_LENGTH> encoder(field, generator_polynomial);
	MYCONTAINER ret;
	printf("1");
	int i, j, k=0;
	int nElem = (int)((CODE_LENGTH - FEC_LENGTH)/sizeof(double));
	std::stringstream sstream;
	int elemsDone = 0;

	double sz2 = (double)length;
	for(j=0; j<(int)sizeof(double); j++) {
		unsigned char c = *((unsigned char*)&sz2 + j);
		c = c ^ MYMASK;
		sstream << c;
	}
	elemsDone = elemsDone + 1;
	printf("2");
	for(i=0; i<length; i++) {
		elemsDone = elemsDone + 1;
		double dd = d[i];
//		printf(" %g=", dd);
		for(j=0; j<(int)sizeof(double); j++) {
			unsigned char c = *(unsigned char*)((unsigned char*)&dd + j);
			c = c ^ MYMASK;
//			printf("%d ", c);
			sstream << c;
		}
		if((elemsDone>0 && elemsDone%nElem==0) || (i==length-1)) {
//			printf("[encode]elemsDone=%d\n", (unsigned int)elemsDone);
			sstream.flush();
			std::string s = sstream.str();
//			std::cout<<"Encoding block #" << k <<" (length=" << s.length() << ")";
			k = k + 1;
//			schifra::reed_solomon::block<code_length, fec_length> block;
			schifra::reed_solomon::block<CODE_LENGTH, FEC_LENGTH> block;
			if(!encoder.encode(s, block)) {
				std::cout << "ERROR: Critical encoding failure!!!" << std::endl;
			} else {
//				std::cout << "ok." << std::endl;
//				schifra::reed_solomon::block<code_length, fec_length> *block2 = new schifra::reed_solomon::block<code_length, fec_length>(block);

		//04-08-2012
				schifra::reed_solomon::block<CODE_LENGTH, FEC_LENGTH> *block2 = new schifra::reed_solomon::block<CODE_LENGTH, FEC_LENGTH>(block);
//				MYCONTAINER *block2 = new schifra::reed_solomon::block<CODE_LENGTH, FEC_LENGTH>(block);
				ret.push_back(*block2);
				sstream.str("");
			}
		}
	}
	printf("[encode]len=%d, elemsDone=%d\n", (unsigned int)length, (unsigned int)elemsDone);
	return ret;
}
int main()
{
    /* Finite Field Parameters */
    const std::size_t field_descriptor                =   8;
    const std::size_t generator_polynomial_index      = 120;
    const std::size_t generator_polynomial_root_count =  32;

    /* Reed Solomon Code Parameters */
    const std::size_t code_length = 255;
    const std::size_t fec_length  =  32;
    const std::size_t data_length = code_length - fec_length;

    /* Instantiate Finite Field and Generator Polynomials */
    schifra::galois::field field(field_descriptor,
                                 schifra::galois::primitive_polynomial_size06,
                                 schifra::galois::primitive_polynomial06);

    schifra::galois::field_polynomial generator_polynomial(field);

    if (
        !schifra::make_sequential_root_generator_polynomial(field,
                generator_polynomial_index,
                generator_polynomial_root_count,
                generator_polynomial)
    )
    {
        std::cout << "Error - Failed to create sequential root generator!" << std::endl;
        return 1;
    }

    /* Instantiate Encoder and Decoder (Codec) */
    typedef schifra::reed_solomon::encoder<code_length,fec_length,data_length> encoder_t;
    typedef schifra::reed_solomon::decoder<code_length,fec_length,data_length> decoder_t;

    encoder_t encoder(field,generator_polynomial);
    decoder_t decoder(field,generator_polynomial_index);

    std::string message = "A professional is a person who knows more and more about less "
                          "and less until they know everything about nothing";

    /* Pad message with nulls up until the code-word length */
    message.resize(code_length,0x00);

    std::cout << "Original Message:   [" << message << "]" << std::endl;

    /* Instantiate RS Block For Codec */
    schifra::reed_solomon::block<code_length,fec_length> block;

    /* Transform message into Reed-Solomon encoded codeword */
    if (!encoder.encode(message,block))
    {
        std::cout << "Error - Critical encoding failure!" << std::endl;
        return 1;
    }

    schifra::reed_solomon::erasure_locations_t erasure_location_list;

    /* Add a burst of errors then a burst of erasures consecutively */
    schifra::corrupt_message_errors_erasures(block,
            schifra::error_mode::errors_erasures,
            0,
            10,
            erasure_location_list);

    std::cout << "Corrupted Codeword: [" << block << "]" << std::endl;

    if (!decoder.decode(block,erasure_location_list))
    {
        std::cout << "Error - Critical decoding failure!" << std::endl;
        return 1;
    }
    else if (!schifra::is_block_equivelent(block,message))
    {
        std::cout << "Error - Error correction failed!" << std::endl;
        return 1;
    }

    block.data_to_string(message);

    std::cout << "Corrected Message:  [" << message << "]" << std::endl;

    std::cout << "Encoder Parameters [" << encoder_t::trait::code_length << ","
              << encoder_t::trait::data_length << ","
              << encoder_t::trait::fec_length  << "]" << std::endl;

    std::cout << "Decoder Parameters [" << decoder_t::trait::code_length << ","
              << decoder_t::trait::data_length << ","
              << decoder_t::trait::fec_length  << "]" << std::endl;

    return 0;
}
Esempio n. 5
0
int main()
{
   const std::size_t code_length         = 255;
   const std::size_t fec_length          =   2;
   const std::size_t data_length         = code_length - fec_length;
   const std::size_t field_descriptor    =   8;
   const std::size_t gen_poly_index      = 120;
   const std::size_t gen_poly_root_count = fec_length;
   const std::size_t stack_size          = 255;

   const std::string input_file_name                = "input.dat";
   const std::string rsencoded_output_file_name     = "output.rsenc";
   const std::string interleaved_output_file_name   = "output.intr";
   const std::string deinterleaved_output_file_name = "output.deintr";
   const std::string rsdecoded_file_name            = "output.rsdec";

   schifra::galois::field field(field_descriptor,
                                schifra::galois::primitive_polynomial_size06,
                                schifra::galois::primitive_polynomial06);

   schifra::galois::field_polynomial generator_polynomial(field);

   if (
        !schifra::make_sequential_root_generator_polynomial(field,
                                                            gen_poly_index,
                                                            gen_poly_root_count,
                                                            generator_polynomial)
      )
   {
      std::cout << "Error - Failed to create sequential root generator!" << std::endl;
      return 1;
   }

   schifra::reed_solomon::encoder<code_length,fec_length> encoder(field,generator_polynomial);
   schifra::reed_solomon::decoder<code_length,fec_length> decoder(field,gen_poly_index);

   create_file(input_file_name,data_length * stack_size - 3);

   schifra::reed_solomon::file_encoder<code_length,fec_length>(encoder,
                                                               input_file_name,
                                                               rsencoded_output_file_name);

   schifra::reed_solomon::file_interleaver<code_length,stack_size>(rsencoded_output_file_name,
                                                                   interleaved_output_file_name);

   schifra::corrupt_file_with_burst_errors(interleaved_output_file_name,10,code_length * (fec_length >> 1));

   schifra::reed_solomon::file_deinterleaver<code_length,stack_size>(interleaved_output_file_name,
                                                                     deinterleaved_output_file_name);

   schifra::reed_solomon::file_decoder<code_length,fec_length>(decoder,
                                                               deinterleaved_output_file_name,
                                                               rsdecoded_file_name);

   if (!schifra::fileio::files_identical(input_file_name,rsdecoded_file_name))
   {
      std::cout << "ERROR - Input file and decoded deinterleaved files are not equivelent!" << std::endl;
      return 1;
   }

   return 0;
}
int main()
{
   /* Finite Field Parameters */
   const std::size_t field_descriptor                =   8;
   const std::size_t generator_polynomial_index      = 120;
   const std::size_t generator_polynomial_root_count = 128;

   /* Reed Solomon Code Parameters */
   const std::size_t code_length = 255;
   const std::size_t fec_length  = 128;
   const std::size_t data_length = code_length - fec_length;

   /* Instantiate Finite Field and Generator Polynomials */
   schifra::galois::field field(field_descriptor,
                                schifra::galois::primitive_polynomial_size06,
                                schifra::galois::primitive_polynomial06);

   schifra::galois::field_polynomial generator_polynomial(field);

   schifra::sequential_root_generator_polynomial_creator(field,
                                                         generator_polynomial_index,
                                                         generator_polynomial_root_count,
                                                         generator_polynomial);

   typedef schifra::reed_solomon::encoder<code_length,fec_length>              encoder_type;
   typedef schifra::reed_solomon::erasure_code_decoder<code_length,fec_length> decoder_type;

   typedef erasure_process<encoder_type,decoder_type> erasure_process_type;
   typedef boost::shared_ptr<erasure_process_type>    erasure_process_ptr_type;

   /* Instantiate Encoder and Decoder (Codec) */
   encoder_type encoder(field,generator_polynomial);
   decoder_type decoder(field,generator_polynomial_index);

   const unsigned int max_thread_count = 4; // number of functional cores.

   std::vector<erasure_process_ptr_type> erasure_process_list;

   boost::thread_group threads;

   for (unsigned int i = 0; i < max_thread_count; ++i)
   {
      erasure_process_list.push_back(erasure_process_ptr_type(new erasure_process_type(i,encoder,decoder)));
      threads.create_thread(boost::bind(&erasure_process_type::execute,erasure_process_list[i]));
   }

   threads.join_all();

   double time = -1.0;

   /* Determine the process with the longest running time. */
   for (std::size_t i = 0; i < erasure_process_list.size(); ++i)
   {
      time = ((time < erasure_process_list[i]->time()) ? erasure_process_list[i]->time() : time);
   }

   double mbps = (max_thread_count * round_count * 8.0 * code_length * data_length) / (1048576.0 * time);

   std::cout << "Blocks decoded: " << max_thread_count * round_count * code_length << "\tTime: " << time <<"sec\tRate: " << mbps << "Mbps" << std::endl;

   return 0;
}