⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rscodeyanjiu.txt

📁 也是我从网上找到的源码 供大家分享 希望有所新发现
💻 TXT
📖 第 1 页 / 共 2 页
字号:
Reed Solomon Codec C code implement!
//this code is from Internet, not mine. thank for the author
//C code begin
/////////////////////////////////////////////////////////////////////////////
/*
* Reed-Solomon coding and decoding
* This code supports a symbol size from 2 bits to 16 bits,
* implying a block size of 32-bit symbols (6 bits) up to 65535
* 16-bit symbols (1,048,560 bits).
* Note that if symbols larger than 8 bits are used, the type of each
* data array element switches from unsigned char to unsigned int. The
* caller must ensure that elements larger than the symbol range are
* not passed to the encoder or decoder.
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define MM  8  /* RS code over GF(2**MM) - change to suit */
#define KK  239 /* KK = number of information symbols */
//#define MM 4
//#define KK 9
#define NN ((1 << MM) - 1)
#define TT 8
//#define TT 3
#define DEBUG 1
#define PS 1

#if (MM < 8)
typedef unsigned char dtype;
#else
typedef unsigned int dtype;
#endif
#if (KK >= NN)
#error "KK must be less than 2**MM - 1"
#endif
/*primitive polynomials */
#if(MM == 2)
int pp[MM+1] = { 1, 1, 1 };
#elif(MM == 3)
/* 1 + x + x^3 */
int pp[MM+1] = { 1, 1, 0, 1 };
#elif(MM == 4)
/* 1 + x + x^4 */
int pp[MM+1] = { 1, 1, 0, 0, 1 };
#elif(MM == 5)
/* 1 + x^2 + x^5 */
int pp[MM+1] = { 1, 0, 1, 0, 0, 1 };
#elif(MM == 6)
/* 1 + x + x^6 */
int pp[MM+1] = { 1, 1, 0, 0, 0, 0, 1 };
#elif(MM == 7)
/* 1 + x^3 + x^7 */
int pp[MM+1] = { 1, 0, 0, 1, 0, 0, 0, 1 };
#elif(MM == 8)
/* 1+x^2+x^3+x^4+x^8 */
int pp[MM+1] = { 1, 0, 1, 1, 1, 0, 0, 0, 1 };
#elif(MM == 9)
/* 1+x^4+x^9 */
int pp[MM+1] = { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
#elif(MM == 10)
/* 1+x^3+x^10 */
int pp[MM+1] = { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 };
#elif(MM == 11)
/* 1+x^2+x^11 */
int pp[MM+1] = { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
#elif(MM == 12)
/* 1+x+x^4+x^6+x^12 */
int pp[MM+1] = { 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 };
#elif(MM == 13)
/* 1+x+x^3+x^4+x^13 */
int pp[MM+1] = { 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
#elif(MM == 14)
/* 1+x+x^6+x^10+x^14 */
int pp[MM+1] = { 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 };
#elif(MM == 15)
/* 1+x+x^15 */
int pp[MM+1] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
#elif(MM == 16)
/* 1+x+x^3+x^12+x^16 */
int pp[MM+1] = { 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 };
#else
#error "MM must be in range 2-16"
#endif
int alpha_to[NN+1];
int index_of[NN+1];
int gg[NN-KK+1];
int recd[NN];
int err_flag;
dtype data[KK],bb[NN-KK];
//   int elp[NN-KK+2][NN-KK], d[NN-KK+2], l[NN-KK+2], u_lu[NN-KK+2], s[NN-KK+1] ;
//   int count=0, syn_error=0, root[TT], loc[TT], z[TT+1], err[NN], reg[TT+1] ;
//   int i,j,u,q ;
void generate_gf()
/* generate GF(2**MM) from the irreducible polynomial p(X) in pp[0]..pp[MM]
   lookup tables:  index->polynomial form   alpha_to[] contains j=alpha**i;
                   polynomial form -> index form  index_of[j=alpha**i] = i
   alpha=2 is the primitive element of GF(2**MM)
*/
 {
   register int i, mask ;
  mask = 1 ;
  alpha_to[MM] = 0 ;
  for (i=0; i<MM; i++)
   { alpha_to[i] = mask ;
     index_of[alpha_to[i]] = i ;
     if (pp[i]!=0)
       alpha_to[MM] ^= mask ;
     mask <<= 1 ;
   }
  index_of[alpha_to[MM]] = MM ;
  mask >>= 1 ;
  for (i=MM+1; i<NN; i++)
   { if (alpha_to[i-1] >= mask)
        alpha_to[i] = alpha_to[MM] ^ ((alpha_to[i-1]^mask)<<1) ;
     else alpha_to[i] = alpha_to[i-1]<<1 ;
     index_of[alpha_to[i]] = i ;
   }
  index_of[0] = -1 ;
 }
void gen_poly()
/* Obtain the generator polynomial of the TT-error correcting, length
  NN=(2**MM -1) Reed Solomon code  from the product of (X+alpha**i), i=1..2*TT
*/
 {
   register int i,j ;
   gg[0] = 2 ;    /* primitive element alpha = 2  for GF(2**MM)  */
   gg[1] = 1 ;    /* g(x) = (X+alpha) initially */
   for (i=2; i<=NN-KK; i++)
    { gg[i] = 1 ;
      for (j=i-1; j>0; j--)
        if (gg[j] != 0)  gg[j] = gg[j-1]^ alpha_to[(index_of[gg[j]]+i)%NN] ;
        else gg[j] = gg[j-1] ;
      gg[0] = alpha_to[(index_of[gg[0]]+i)%NN] ;     /* gg[0] can never be zero */
    }
   /* convert gg[] to index form for quicker encoding */
   for (i=0; i<=NN-KK; i++)  gg[i] = index_of[gg[i]] ;
 }
void encode_rs()
/* take the string of symbols in data[i], i=0..(k-1) and encode systematically
   to produce 2*TT parity symbols in bb[0]..bb[2*TT-1]
   data[] is input and bb[] is output in polynomial form.
   Encoding is done by using a feedback shift register with appropriate
   coNNections specified by the elements of gg[], which was generated above.
   Codeword is   c(X) = data(X)*X**(NN-KK)+ b(X)          */
 {
   register int i,j ;
   int feedback ;
   for (i=0; i<NN-KK; i++)   bb[i] = 0 ;
   for (i=KK-1; i>=0; i--)
    {  feedback = index_of[data[i]^bb[NN-KK-1]] ;
       if (feedback != -1)
        { for (j=NN-KK-1; j>0; j--)
            if (gg[j] != -1)
              bb[j] = bb[j-1]^alpha_to[(gg[j]+feedback)%NN] ;
            else
              bb[j] = bb[j-1] ;
          bb[0] = alpha_to[(gg[0]+feedback)%NN] ;
        }
       else
        { for (j=NN-KK-1; j>0; j--)
            bb[j] = bb[j-1] ;
          bb[0] = 0 ;
        } ;
    } ;
 } ;
void decode_rs()
/* assume we have received bits grouped into MM-bit symbols in recd[i],
   i=0..(NN-1),  and recd[i] is index form (ie as powers of alpha).
   We first compute the 2*TT syndromes by substituting alpha**i into rec(X) and
   evaluating, storing the syndromes in s[i], i=1..2TT (leave s[0] zero) .
   Then we use the Berlekamp iteration to find the error location polynomial
   elp[i].   If the degree of the elp is >TT, we cannot correct all the errors
   and hence just put out the information symbols uncorrected. If the degree of
   elp is <=TT, we substitute alpha**i , i=1..n into the elp to get the roots,
   hence the inverse roots, the error location numbers. If the number of errors
   located does not equal the degree of the elp, we have more than TT errors
   and cannot correct them.  Otherwise, we then solve for the error value at
   the error location and correct the error.  The procedure is that found in
   Lin and Costello. For the cases where the number of errors is known to be too
   large to correct, the information symbols as received are output (the
   advantage of systematic encoding is that hopefully some of the information
   symbols will be okay and that if we are in luck, the errors are in the
   parity part of the transmiTTed codeword).  Of course, these insoluble cases
   can be returned as error flags to the calling routine if desired.   */
 {
    
   register int i,j,u,q ;
   int elp[NN-KK+2][NN-KK], d[NN-KK+2], l[NN-KK+2], u_lu[NN-KK+2], s[NN-KK+1] ;
   int count=0, syn_error=0, root[TT], loc[TT], z[TT+1], err[NN], reg[TT+1] ;
#ifdef PS
   int syndrome[NN-KK+1];
   int omega[NN-KK+2][NN-KK];
   int zx[TT+1];
#endif
 err_flag=0;
/* first form the syndromes */
   for (i=1; i<=NN-KK; i++)
    { s[i] = 0 ;
      for (j=0; j<NN; j++)
        if (recd[j]!=-1)
          s[i] ^= alpha_to[(recd[j]+i*j)%NN] ;      /* recd[j] in index form */
/* convert syndrome from polynomial form to index form  */
      if (s[i]!=0)  syn_error=1 ;        /* set flag if non-zero syndrome => error */
#ifdef PS
   syndrome[i]=s[i];
#endif
     s[i] = index_of[s[i]] ;
    } ;
   if (syn_error)       /* if errors, try and correct */
    {
/* compute the error location polynomial via the Berlekamp iterative algorithm,
   following the terminology of Lin and Costello :   d[u] is the 'mu'th
   discrepancy, where u='mu'+1 and 'mu' (the Greek leTTer!) is the step number
   ranging from -1 to 2*TT (see L&C),  l[u] is the
   degree of the elp at that step, and u_l[u] is the difference between the
   step number and the degree of the elp.
*/
/* initialise table entries */
    err_flag=1;
      d[0] = 0 ;           /* index form */
      d[1] = s[1] ;        /* index form */
      elp[0][0] = 0 ;      /* index form */
      elp[1][0] = 1 ;      /* polynomial form */
      for (i=1; i<NN-KK; i++)
        { elp[0][i] = -1 ;   /* index form */
          elp[1][i] = 0 ;   /* polynomial form */
        }
      l[0] = 0 ;
      l[1] = 0 ;
      u_lu[0] = -1 ;
      u_lu[1] = 0 ;
      u = 0 ;
      do
      {
        u++ ;
        if (d[u]==-1)
          { l[u+1] = l[u] ;
            for (i=0; i<=l[u]; i++)
             {  elp[u+1][i] = elp[u][i] ;
#ifdef PS
 omega[u][i]=elp[u][i];
#endif
                elp[u][i] = index_of[elp[u][i]] ;
             }
          }
        else
/* search for words with greatest u_lu[q] for which d[q]!=0 */
          { q = u-1 ;
            while ((d[q]==-1) && (q>0)) q-- ;
/* have found first non-zero d[q]  */
            if (q>0)
             { j=q ;
               do
               { j-- ;
                 if ((d[j]!=-1) && (u_lu[q]<u_lu[j]))
                   q = j ;
               }while (j>0) ;
             } ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -