RS编码解码程序源码

所需积分/C币:50 2014-04-30 09:04:40 83KB PDF
28
收藏 收藏
举报

C语言实现,经过调试没有错误,可以放心下载
printf("\n\nRec=") for(i=0; k<length; i++) printf( %4d index ofred[ill; rinf("n "); for(i=0; i<length; i++) printf("%4d",recd [ printf("\n" decode _rs(; //DECODE received codeword recv[*/ // DECODING ERRORS? we compare only the data portion decerror =0 biterror=0 for(i= length-k; i s length; i++) if (data[i-length+k]!= recd[i]) becerro++ biterror + weight(data[i-length+k]-recd[il) void read_ p // Read m, the degree of a primitive polynomial px)used to compute the Galois field GF(2**m). Get precomputed coefficients pl] of p(x). Read the code length //, ninf printf("\sImulation of RS codes n"); printf( "Copyright 2002(c)Robert Morelos-Zaragoza All rights reserved. n\n"); for (i=1; i<m; i++ p[=0 [0]=pl[m]=1 //LSB First if(m==2) p[1]=1; //111 else if (m==3) p[1]=1; //1101 else if (m ==4) p[3]=1; //11001 // else if(m== 4) p1]=1;∥ Commented out to match example p.6〉 else if (m==5) p[2]= //101001 else if (m==6) p[1]=1; //1100001 else if(m==7) p[1]= 10010001 else if (m==8) p[2]=p[3]=p[4]=1;//101110001 else it (m p[4]=1 //1000100001 else if(m==10) p[3]=1; //10010000001 else if (m==11) p[2]= else if (m = 12 p[3]=p[4]=p[7]=1; else if (m==13 p[1=p[3]=p[4]=1; Ise if( 14) p[1]=p[11]=p[12]=1; else if(m==15) p[1]=1; else if (m ==16 p[2]=p[3]=p5]=1; else if (m==17) p[3]=1 else if (m==18) p[=1 else if (m ==19) p[1]=p[5]=p[6]=1; else if (m== 20) p[3]=1 printf(" Primitive polynomial of GF(2 %d),(LSB first) p(x)=",m) //n=1; for(=0;i<=m;i++) printf(%1d pil) //printf("n); //n=n/2-1; void generate gfo //generate GF(2 m) from the irreducible polynomial p(X)in p[o] p[m] // lookup tables: log->vector form alpha tol] contains j=alpha *i vector form->log form index of[j=alpha N alpha=2 is the primitive element of GF(2m register int i, mask, nask=1. alpha to[m]=0; for (i=0; i<m; i++) alpha to[=mask dex oflalpha to] alpha to[m] A= mask; mask <<=1: index oflalpha to[m]]= m mask >>=1: for(i=m+1; k<n; i++) if (alpha to[i-1]>=mask) alpha to alpha tolm(alpha toli-1]mask)<<1) else alpha to[d]= alpha to[i-1]<<1 dex of[alpha to[J] dex of[o]=-1 Hifdef print ge printf("Table of GF(%d): \n"n); printf( n"); printf(" i\tvector \tlog\n"); --n"); printf("%4d\%4d\ t%4d\n,i, alpha to[i], index of[i]); #endif void gen _ poly // Compute the generator polynomial of the t-error correcting length //n=(2m-1)Reed-Solomon code from the product of (X+alpha i), for A/i= init zero, init zero+1,., init zero+length-k-1 register int I,J; g[0]=alpha to[init_ zero]; //<--vector form of alpha init_zero g[1]=1 l/g(x)=(X+alpha init zero for (i=2; i<=length-k; i++) g[]-1; for (j=i-1; j>0; j--) (gj]!=0) g =gli-1 alpha tol(index of[[l+i+init zero-1%n] else g]=g[-1]; g[0]=alpha to[index of[go]+i+init_zero-1)%n // convert gl to log form for quicker encoding for(i=0; i<=length-k; i++g0]= index oflg[i] #ifdef print poly printf("Generator polynomial (independent term first): \ ng(x)="); for(i=0; i<=length-k i ++) printf( %5d" gi]) printf("\n") fendi void encode rs( // Compute the 2t parity symbols in b[o]. b[2*t-11 //data[] is input and bl] is output in polynomial form // Encoding is done by using a feedback shift register with connections // specified by the elements of g[] register int l,l; int feedback for (i=0; iclength-k; i++) b[]=0; for(i=k-1;i>=0;i-) feedback index of[data[i]b[length-k-1: if (feedback =-1) for(=length-k-1; j>0; j--) if(g[]!=-1) []= b[j-1]alpha to[(g[]+feedback %n]; else b]=b[-1] b[O]=alpha tol(glo]+feedback %n else for(j=length-k-1; j>0; j b=b1]; 0]=0; register int i,J, u, nt elp[length-k+2][length-k] d[length-k+2], I[length-k+2]u lu[length-k+] s[length-k+1], forney[length-k+2 int count=0, syn error=0, tau[t], root[t], loc[t] int err[length], reg[t+1], aux[t+1], omega [length-k+1 phi[length-k+1], phiprimellength-k+1] int dephi, ell //Compute the syndromes #ifdef Print syndrome printf("\ns 0"); #endif or(=1;i<=t2;i++) s[i]=0 for〔j=0; j<length;j+) if (redhill=o) s[]A=alpha to(index of[recd[l]+(i+init zero-1 *j%n // convert syndrome from vector form to log form * if(s[i]!=0) syn error //set flag if non-zero syndrome => error // Note: If the code is used only for ERRoR DETECTION then exit program here indicating the presence of errors e= index of[s[eli #ifdef print syndrome printf(%4d",s[i]) #tendif if ( syn error // if syndromes are nonzero then try to correct D]=0;∥S(x)=1+s_1x+… //TO HANDLE ERASURES if (numera) // if erasures are present compute the erasure locator polynomial, tau(x) for(i-0; k<=t 2; i++) {tau[i]=0;aux[j]=0;} x[1]=alpha tolera [oji; aux[0]=1; //(X+era[o]) numera> for (i=l; k<numera; i++ 1]=era[i]; plO for(j=0;j<2;j++) for(el=0; ell<=i; ell++ // Line below added 8/17/2003 if(p[j]!=1)&&(aux[el]!=0) tauj+ell] n =alpha tol(pl]+index of[aux[ell%n] f(i!= numera for(ell=0; elk=(i+1); ell ++ aux[ell]= taulell] aulelll se i [0]=auX[o];tau[1]=aux[1]; // Put in index (log)form for(i=0; i<=numera; i++) tau[]= index of[tau[]; /*tau in log form * #ifdef print syndrome printf("\ntau =) for (i=0; i<=numera; i+ printf(%4d, tau[i) printf("\forney =) #tendif // Compute FORNeY modified syndrome forney(x =[ s(x tau(x)+ 1] mod x(t2] fori=0; k<=n-k; i++forney[]=0; for(i=0; i<=n-k; i++) for (=0; j<=numera; j++) if (i+j<=(n-k))//mod x n-k+1] f(G-1)&&tau[]1=-1) forney[i+j] A=alpha to[(s[i]+tau[]%n]: forney[o]A= 1; for(i=0; i<=n-k; i+ forney[i]= index of forney[il; #ifdef Print syndrome for(i=0;<=n-k;i++) printf( %4d" forney[i) tendif else∥/ No erasures tau[0]=0 for(i=l; k<=n-k; i++)forney=s[i] #ifdef PRiNt syndrome printf("\n"); #endif 7 THE BERLEKAMP-MASSEY ALGORITHM FOR ERRORS AND ERASURES ∥/ // initialize table entries d[o]=0; d[1]= forney[numera +1]; // log form elp[o][o]=0, // log form elp[1[o]=1 // vector form for (i=1: k<t2: i elp[ojd=-1 //log form elp[1[=0;// vector form 0]=0; ]=0; u[1]=0 =0: if (numera <t 2) // If errors can be corrected u+十 f(d[u]==-1 #ifdef Print syndrome printf( d[%d] is zero\n", L fendi [u+1]=u]; for(i=0; k<=llu; i++) elp[u+1][]=elp[uj[]: elpluli= index oflelpulilli elst //search for words with greatest u lu[q] for which d[ql=0 q=u-1 Whle(d[q=-1)&&(q>0)q- // have found first non-zero d[ 」=q; do if(djJ=-1)&&u lula]<u lulD) 9=I I while g>O) #ifdef Print syndrome printf("u=%4d g=%4d, d[g]=%4d d[u]=%4d\n",u, g, dlald[ul) tendif l have now found g such that d[ul =o and u lula] is maximum // store degree of new elp polynomial if ([u]>lalu-g[u+1=lu]:

...展开详情
试读 16P RS编码解码程序源码
立即下载 低至0.43元/次 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
您会向同学/朋友/同事推荐我们的CSDN下载吗?
谢谢参与!您的真实评价是我们改进的动力~
关注 私信
上传资源赚钱or赚积分
最新推荐
RS编码解码程序源码 50积分/C币 立即下载
1/16
RS编码解码程序源码第1页
RS编码解码程序源码第2页
RS编码解码程序源码第3页
RS编码解码程序源码第4页

试读结束, 可继续读1页

50积分/C币 立即下载 >