RS編碼解碼程序源碼_第1頁
RS編碼解碼程序源碼_第2頁
RS編碼解碼程序源碼_第3頁
RS編碼解碼程序源碼_第4頁
RS編碼解碼程序源碼_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、/#include#includeinclude#includeinclude#defineMAX_RANDOMLONG_MAX/Maximumvalueofrandom()/#definePRINT_GF/#definePRINT_POLY/#definePRINT_SYNDROME#definem8#definen255#definelength255#definered16#definek239#definet8#definet216#defineinitzero1/lengthred/red/2/2*t/intd;intp10;intalphaoln+l,index_ofn+lzgre

2、d+l;intrecdlength,datak,bred;intnumera,erat2;/charfilename40zname240;voidread_p(void);voidgenerate_gf(void);voidgen_poly(void);voidencode_rs(void);/voidbpsk_awgn(void);voiddecode_rs(void);intweight(intword);main()inti;intbiterror;decerror;/,error;intnumerr;errposft,errvalft;read_p();/Readm*/generate

3、_gf();/ConstructtheGaloisFieldGF(2Am)*/printf(-ThisisanRS(%d,%d,%d)codeoverGF(2A%d),t=%dnM,length,k,red+1,m,t);printf(Hwithzeros:);for(i=0;it2;i+)printf(%d,init_zero+i);printfCW);gen_poly();/ComputethegeneratorpolynomialofRScode*/RandomlygenerateDATA*/for(i=0;ik;i+)datai=(rand()10)%n;encode_rs();/en

4、codedata*/Codewordisc(X)=data(X)*X*(length-k)+b(X)for(i=0;ilengthk;i+)recdi=bi;for(i=0;ik;i+)recdi+length-k=datai;/for(i=0;ilength;i+)recdi=0;/Introduceerrorsanderasuresatwill/*CHANNELNOISE:Readerrorsanderasures*/printf(HNumberoferrors,e:);scanf(”cT:&numerr);if(numerr)for(i=0;knumerr;i+)printffposit

5、ion:);scanf(,%d,/&errposfi);/aspowerofalphaprintf(Hvalue:H);scanf(,%d,/&errvali);/asvectorrecderrposiA=errvali;printf(HNumberoferasures,b:scanf(,%d,/&numera);if(numera)for(i=0;knumera;i+)printffposition:);scanf(,%d,/&erai);/aspowerofalpharecderai=0;printf(nnRec=);for(i=0;klength;i+)printf(H%4d,index

6、_ofrecdi);printf(n”);for(i=0;klength;i+)printf(H%4d舄recdi);printfCV);decode_rs();/DECODEreceivedcodewordrecv*/DECODINGERRORS?wecompareonlythedataportiondecerror=0;biterror=0;for(i=length-k;ilength;i+)if(datai-length+k)!=recdi)decerror+;biterror+=weight(datai-length+k-recdfi);voidread_p()/fReadm,thed

7、egreeofaprimitivepolynomialp(x)usedtocomputetheGaloisfieldGF(2*m).Getprecomputedcoefficientspofp(x).Readthecodelengthtinti;/,ninf;printf(HnSimulationofRScodesnH);printf(,Copyright2002(c)RobertMorelos-Zaragoza.Allrightsreserved.nn);for(i=l;im;i+)Pi=0;p0=pm=l;/LSBFirstif(m=2)/Hlelseif(m=3)pl=1;/1101el

8、seif(m=4)P=2;/11001/elseif(m=4)Pl=1;/Commentedouttoielseif(m=5)P2=1;/101001elseif(m=6)Pl=1;/1100001elseif(m=7)Pl=1;/10010001elseif(m=8)p2=p3=p4=1;/101110001matchexamplep.68elseif(m=9)elseif(m=10)elseif(m=11)elseif(m=12)elseif(m=13)elseif(m=14)elseif(m=15)elseif(m=16)elseif(m=17)elseif(m=18)elseif(m=

9、19)elseif(m=20)p4=l;/1000100001p3=1;/10010000001P2=1;p3=p4=p7=l;Pl=P【3=p4=1;Pl=PH=P(12=1;Pl=1;P【2=p3=p5=1;P3=1;P7=1;Pl=P5=p6=1;P3=1;printf(PrimitivepolynomialofGF(2A%d),(LSBfirst)p(x)=蔦m);n=1;for(i=0;ivectorformalpha_tocontainsj=alpha*i;/vectorform-logformindex_ofj=alpha*i=i/alpha=2istheprimitiveele

10、mentofGF(2Am)registerinti,mask;mask=1;alpha_tom=0;for(i=0;im;i+)alpha_toi=mask;index_ofalpha_toi=i;if(pi!=O)alpha_tomA=mask;mask=1;index_ofalpha_tom=m;mask=1;for(i=m+l;i=mask)alpha_toi=alpha_tomA(alpha_toi-lAmask)l);elsealpha_toi=alpha_toi-ll;index_ofalpha_toi=i;index_of0=-1;#ifdefPRINT_GFprintf(HTa

11、bleofGF(%d):nM,n);printfCAn);printf(itvectortlognu);printfCAn);for(i=0;i=n;i+)printf(,%4dt%4dt%4dn,/i,alpha_toi,index_ofi);#endifvoidgen_poly()/Computethegeneratorpolynomialofthet-errorcorrecting,length/n=(2Am-1)Reed-Solomoncodefromtheproductof(X+alphaAi),for/i=init_zero,init_zero+1,init_zero+length

12、kJregisterinti,j;g0=alpha_toinit_zero;/vectorformofalphaAinit_zerogl=1;/g(x)=(X+alphaAinit_zero)for(i=2;i0;j-)if(gU!=O)gj=gj-lAalpha_to(index_ofgj+i+init-zero-l)%n;elsegj=gD-1;g0=alpha_to(index_ofg0+i+init_zero-l)%n;/convertgtologformforquickerencodingfor(i=0;i=length-k;i+)gi=index_ofgi;#ifdefPRINT_

13、POLYprintf(HGeneratorpolynomial(independenttermfirst):ng(x)=);for(i=0;i=length-k;i+)printf(l,%5dl,/gi);printfCXn);#endifvoidencode_rs()/Computethe2tparitysymbolsinb0.b2*t-l/dataisinputandbisoutputinpolynomialform/Encodingisdonebyusingafeedbackshiftregisterwithconnections/specifiedbytheelementsof或.re

14、gisterintizj;intfeedback;for(i=0;i=0;i-)feedback=index_ofdataiAblength-k-l;if(feedback!=-1)for(j=length-k-l;jO;j-)if(gj!=-Dbj=bj-lAalpha_to(gj+feedback)%n;elsebj=bU-1;b0=alpha_to(g0+feedback)%n;elsefor(j=length-k-l;jO;j-)bj=bj-l;b0=0;voiddecode_rs()registerintiju,q;intelplengthk+2length虬dlength-k+2,

15、Hlength-k+2,ujulength-k+2,sflength-k+1,forneylength-k+2;intcount=0,syn_error=0,tautzroott,loct;interrlength,regt+l,auxt+l,omegalength-k+l,philength-k+l,phiprimelength-k+l;intdegphi,ell;/ComputethesyndromesttifdefPRINT_SYNDROMEprintf(Hns=0H);#endiffor(i=l;i=t2;i+)si=0;for(j=0;jerror/Note:Ifthecodeisu

16、sedonlyforERRORDETECTION,then/exitprogramhereindicatingthepresenceoferrors./si=index_ofsi;ttifdefPRINT_SYNDROMEprintf(%4d”,si);#endifif(syn_error)/ifsyndromesarenonzerothentrytocorrects0=0;/S(x)=1+s_lx+./TOHANDLEERASURES訐(numera)/iferasuresarepresent,computetheerasurelocatorpolynomial,tau(x)for(i=0;

17、il)for(i=l;knumera;i+)pl=erai;p0=0;forG=0;j2;j+)for(ell=0;ell=i;ell+)/Linebelowadded8/17/2003if(pj!=-l)&(auxell!=0)tauj+ellA=alpha_to(pj+index_ofauxell)%n;if(i!=(numera-1)for(ell=0;ell=(i+l);ell+)auxell=tauell;tauell=0;elsetau0=aux0;taul=auxl;/Putinindex(log)formfor(i=0;i=numera;i+)taui=index_oftaui

18、;/*tauinlogform*/#ifdefPRINT_SYNDROMEprintf(Hntau=n);for(i=0;i=numera;i+)printf(H%4d,taui);printffnforney=n);#endif/ComputeFORNEYmodifiedsyndrome:/forney(x)=s(x)tau(x)+1modxAt2for(i=0;i=n-k;i+)forneyi=0;for(i=0;i=nk;i+)for(j=O;j=numera;j+)if(i+j=(n-k)/modxAn-k+lif(si!=-l)&(tauj!=-l)forneyi+jA=alpha_

19、to(si+tauj)%n;forney0A=1;for(i=0;i=n-k;i+)forneyi=index_offorneyi;#ifdefPRINT_SYNDROMEfor(i=0;i=n-k;i+)printf(%4d,forneyfi);#endifelse/Noerasurestau0=0;for(i=l;i=n-k;i+)forneyi=si;#ifdefPRINT_SYNDROMEprintf(”n“);#endif/THEBERLEKAMP-MASSEYALGORITHMFORERRORSANDERASURES/initializetableentriesd0=0;/logf

20、ormdl=forneynumera+l;/logformelp00=0;/logformelpl0=1;/vectorformfor(i=l;it2;i+)elp0i=-1;/logformelpli=0;/vectorformi0=0;I1=0;uJu0=-1;uul=0;u=0;if(numerat2)/IferrorscanbecorrecteddoU+;if(du=-l)ttifdefPRINT_SYNDROMEprintf(Hd%diszeronnzu);#endiflu+l=lu;for(i=0;i0)q-;/havefoundfirstnon-zerodqif(qo)i=q;d

21、oif(dj!=-l)&(u_luq0);ttifdefPRINT_SYNDROMEprintf(Nu=%4d,q=%4d,dq=%4ddu=%4dn,/u,q,dq,du);#endif/havenowfoundqsuchthatdu!=0andu_luqismaximum/storedegreeofnewelppolynomialif(lulq+u-q)lu+l=lu;elselu+l=lq+u-q;#ifdefPRINT_SYNDROMEprintf(lq=%4d,lu=%4dn,/lq,lu);#endif/computenewelp(x)/for(i=0;it2-numera;i+)

22、elpu+li=0;for(i=0;it2;i+)elpu+li=0;for(i=0;i=lq;i+)if(elpqi!=-l)elpu+li+u-q=alpha_to(du+n-dq+elpqi)%n;for(i=0;i=lu;i+)elpu+liA=elpui;elpui=index_ofelpui;#ifdefPRINT_SYNDROMEprintf(lu+l=%4d,elpu+l=lu+l);for(i=0;i=lu+l;i+)printf(%4d,/index_ofelpu+li);printfCV);#endifujuu+l=u-lu+l);/compute(u+l)thdiscr

23、epancy/if(ut2)/nodiscrepancycomputedonlastiterationif(u(t2-numera)/nodiscrepancycomputedonlastiteration/if(u(lu+l+t-l-(numera/2)/if(su+l!=-l)if(forneynumera+u+l!=-l)du+l=alpha_toforneynumera+u+l;elsedu+l=0;#ifdefPRINT_SYNDROMEprintf(udiscrepancyforu=%d:du+l=%4dnll/uzindex_ofdu+l);#endiffor(i=l;i=lu+

24、l);i+)/if(su+l-i!=-l)&(elpu+li!=0)/du+lA=alpha_to(snumera+u+l-iif(forneynumera+u+l-i!=-l)&(elpu+li!=0)du+lA=alpha_to(forneynumera+u+l-i+index_ofelpu+li)%n;#ifdefPRINT_SYNDROMEprintf(“i=%d,forney%d=%4d,du+l=%4dn,i/numera+u+l-i/forneynumera+u+l-iJndex_ofdu+l);#endifdu+l=index_ofdu+l;/putdu+lintoindexf

25、ormttifdefPRINT_SYNDROMEprintf(du+l=%4dn,du+l);#endif/while(u&(lu+l=t);while(u(t2-numera)&(Iu+l=(t2-numera)/2);/else/caseof2terasures/elpl0=0;/count=0;/U+;訐(lu=t-numera/2)/cancorrecterrors/putelpintoindexformfor(i=0;i=lu;i+)elpui=index_ofelpui;printf(nBMalgorithm,after%diterations:nsigma=,(u-1);for(

26、i=0;i=lu;i+)printf(%4d,elpui);printfCXn);/findrootsoftheerrorlocationpolynomialfor(i=l;i=lu;i+)regi=elpui;count=0;for(i=l;i=n;i+)q=l;for(j=l;j=lu;j+)if(regj!=l)regj=(regj+j)%n;qA=alpha_toregj;if(!q)/storerootanderrorlocationnumberindicesrootcount=i;loccount=n-i;#ifdefPRINT_SYNDROMEprintf(loc%4d=%4dn

27、,/count,loccount);#endifcount+;if(count=lu)/no.roots=degreeofelphence=terrors/Computetheerrataevaluatorpolynomial,omega(x)forney0=0;/asalog,toconstruct1+T(x)for(i=0;i=t2;i+)omegai=0;for(i=0;i=t2;i+)forG=O;j=lu;j+)if(i+j=t2)/modxAt2if(forneyi!=-l)&(elpuj!=-l)omegai+jA=alpha_to(forneyi+elpuj)%n;for(i=

28、0;i=t2;i+)omegai=index_ofomegai;#ifdefPRINT_SYNDROMEpnntf(nnomega=”);for(i=0;i=t2;i+)printf(%4d蔦omegai);printfCXn);#endif/Computetheerratalocatorpolynomial,phi(x)degphi=numera+lu;for(i=0;i=degphi;i+)phii=0;for(i=0;i=numera;i+)for(j=O;j=lu;j+)if(taui!=-l)&(elpuj!=-l)phii+jA=alpha_to(taui+elpuj)%n);for(i=0;i=degphi;i+)phii=index_ofphii;#ifdefPRINT_SYNDROMEprintf(phi=);for(i=0;i=degphi;i+)printf(%4d,phii);printfCV);#endif/Computethederivative”ofphi(x)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論