程序.docx
- 文档编号:17541319
- 上传时间:2023-07-26
- 格式:DOCX
- 页数:13
- 大小:47.35KB
程序.docx
《程序.docx》由会员分享,可在线阅读,更多相关《程序.docx(13页珍藏版)》请在冰点文库上搜索。
程序
附录一
1.1程序代码
1、ldpc_main.m
clearall;
clc;
%tic
rows=128;
cols=256;
count=20;
SNR=0:
0.2:
4;
forSNRindex=1:
length(SNR)
total_err=0;
errmax=0;
nerr=0;
loop=0;
whileloop s=round(rand(1,cols-rows));%1*cols-rows的0,1矩阵 %产生H矩阵 H=genH(rows,cols); %使用H矩阵进行LDPC编码 [u,P,rearranged_cols]=ldpc_encode(s,H); %SNR=4;%信噪比 amp=1;%信号幅值 tx_waveform=bpsk(u,amp); rx_waveform=awgn(tx_waveform,SNR(SNRindex)); scale(1: length(u))=1;%Nofading. %LDPC译码 [uhatvhat]=ldpc_decode(rx_waveform,SNR(SNRindex),amp,scale,H,rearranged_cols); errmax=find(s~=uhat); nerr=length(errmax); total_err=total_err+nerr; loop=loop+1; %loop end errratio=total_err/(length(s)*count); lerror(SNRindex)=errratio; SNRindex end %errratio %end %figure (1); semilogy(SNR,lerror,'-bo'); xlabel('信噪比/dB') ylabel('误码率') gridon title('averageBER') axis([0410^-510^0]) legend('LDPC编码') %toc 2、ldpc_encode.m function[u,P,rearranged_cols]=ldpc_encode(s,H) %高斯消元 %设H=[A|B]==========>[I|P] %u=[c|s] %∵H*u'=u*H'=0 %代入得: %__ %|c'| %[I|P]||=0 %|s'| %-- %∴I*c'+P*s'=0 %∴I*c'=P*s'(在GF (2)上) %∴c'=P*s' %再由u=[c|s]即可得到编码后的码字。 %如果高斯消元过程中进行了列交换, %则只需记录列交换,并以相反次序对编码后的码字同样进行列交换即可。 %解码时先求出u,再进行列交换得到uu=[c|s],后面部分即是想要的信息。 dim=size(H); rows=dim (1); cols=dim (2); [P,rearranged_cols]=H2P(H);%高斯消元函数 c=mul_GF2(P,s'); u1=[c's]; u=reorder_bits(u1,rearranged_cols); 3、ldpc_decode.m function[uhat,vhat]=ldpc_decode(rx_waveform,SNR,amp,scale,H,rearranged_cols) dim=size(H); rows=dim (1); cols=dim (2); vhat(1,1: cols)=0;%1行cols列的矩阵 zero(1,1: rows)=0; s=struct('qmn0',0,'qmn1',0,'dqmn',0,'rmn0',0,'rmn1',0,'qn0',0,'qn1',0,'alphamn',1);%? 为什么要建立一个结构 %associatethisstructurewithallnonzeroelementsofH %PriorProbabilities %先验概率 N0=1/(exp(SNR*log(10)/10)); pl1=1./(1+exp(-2*amp*rx_waveform.*scale/(N0/2))); pl0=1-pl1; %initialization %初始化: 对特定的信道预设信息比特的先验概率。 newh(1: rows,1: cols)=s; [h1ih1j]=find(H==1); h1num=length(h1i);%从行来看1的个数 fori=1: h1num newh(h1i(i),h1j(i)).qmn0=pl0(h1j(i)); newh(h1i(i),h1j(i)).qmn1=pl1(h1j(i)); end %迭代次数100 foriteration=1: 100 %horizontalstep %横向步骤: 由信息节点的先验概率按置信传播算法得出各校验节点的后验概率。 fori=1: h1num%计算概率差 newh(h1i(i),h1j(i)).dqmn=newh(h1i(i),h1j(i)).qmn0-newh(h1i(i),h1j(i)).qmn1; end fori=1: rows colind=find(h1i==i); colnum=length(colind); forj=1: colnum drmn=1; fork=1: colnum ifk~=j drmn=drmn*newh(i,h1j(colind(k))).dqmn; end end newh(i,h1j(colind(j))).rmn0=(1+drmn)/2; newh(i,h1j(colind(j))).rmn1=(1-drmn)/2; end end %verticalstep %纵向步骤: 由校验节点的后验概率推算出信息节点的后验概率。 forj=1: cols rowind=find(h1j==j); rownum=length(rowind); fori=1: rownum prod_rmn0=1; prod_rmn1=1; fork=1: rownum ifk~=j prod_rmn0=prod_rmn0*newh(h1i(rowind(k)),j).rmn0; prod_rmn1=prod_rmn1*newh(h1i(rowind(k)),j).rmn1; end end const1=pl0(j)*prod_rmn0; const2=pl1(j)*prod_rmn1; newh(h1i(rowind(i)),j).alphamn=1/(const1+const2); newh(h1i(rowind(i)),j).qmn0=newh(h1i(rowind(i)),j).alphamn*const1; newh(h1i(rowind(i)),j).qmn1=newh(h1i(rowind(i)),j).alphamn*const2; %updatepseudoposteriorprobability %更新伪后验概率 const3=const1*newh(h1i(rowind(i)),j).rmn0; const4=const2*newh(h1i(rowind(i)),j).rmn1; alpha_n=1/(const3+const4); newh(h1i(rowind(i)),j).qn0=alpha_n*const3; newh(h1i(rowind(i)),j).qn1=alpha_n*const4; %tentativedecoding %译码尝试,对信息节点的后验概率作硬判决 ifnewh(h1i(rowind(i)),j).qn1>0.5 vhat(j)=1; else vhat(j)=0; end end end ifmul_GF2(vhat,H.')==zero %如果判决条件满足,译码结束,否则继续迭代 break; end end uhat=extract_mesg(vhat,rearranged_cols); 4、get_H.m function[H]=genH(rows,cols) row_flag(1: rows)=0; parity_check=zeros(rows,cols); %addbits_per_col1'stoeachcolumnwiththeonlyconstraintbeingthatthe1'sshouldbe %placedindistinctrows %%%%%%%%使每列随机产生3个1即列重为3%%%%%%%%%%%%% bits_per_col=3; fori=1: cols a=randperm(rows); forj=1: bits_per_col parity_check(a(j),i)=1; row_flag(a(j))=row_flag(a(j))+1; end end %计算每行1的最多个数 max_ones_per_row=ceil(cols*bits_per_col/rows); %add1'storowshavingno1(aredundantrow)oronlyone1(thatbitinthecodewordbecomes %zeroirrespectiveoftheinput) fori=1: rows ifrow_flag(i)==0%如果该行没有1,则随机添加两个1 fork=1: 2 j=unidrnd(cols);%在指定的整数范围内产生一个随机数 whileparity_check(i,j)==1 j=unidrnd(cols); end parity_check(i,j)=1;%在找到的新位置上置1 row_flag(i)=row_flag(i)+1;%行重加1 end end ifrow_flag(i)==1%如果该行只有1个1,则随机再添加1个1 j=unidrnd(cols); whileparity_check(i,j)==1 j=unidrnd(cols); end parity_check(i,j)=1; row_flag(i)=row_flag(i)+1; end end %trytodistributetheonessothatthenumberofonesperrowisasuniformaspossible %尝试在列上分散1的位置,使得每行1的个数均衡(相近或相一致) fori=1: rows j=1; a=randperm(cols); whilerow_flag(i)>max_ones_per_row;%如果该行行重大于允许的最大行重,则进行处理 ifparity_check(i,a(j))==1%随机选择某一该行上为1的列来处理,将该列该行上的1分散到其他的行 %随机查找该列上适合放置1(行重小于允许的最大行重,且该位置上为0)的行 newrow=unidrnd(rows); k=0; while(row_flag(newrow)>=max_ones_per_row|parity_check(newrow,a(j))==1)&k newrow=unidrnd(rows); k=k+1; end ifparity_check(newrow,a(j))==0 %将待处理行上的1转放到找到的行上 parity_check(newrow,a(j))=1; row_flag(newrow)=row_flag(newrow)+1; parity_check(i,a(j))=0; row_flag(i)=row_flag(i)-1; end end%iftest j=j+1; end%whileloop end%forloop %trytoeliminatecyclesoflength4inthefactorgraph %尝试删除4环 forloop=1: 10 chkfinish=1;%是否还寻找四环的标志位 forr=1: rows ones_position=find(parity_check(r,: )==1);%找出r行中的1的位置 ones_count=length(ones_position);%r行中1的数量 fori=[1: r-1r+1: rows]%相当于行坐标 common=0; forj=1: ones_count%相当于列坐标 ifparity_check(i,ones_position(j))==1%相同位置上存在1 common=common+1; ifcommon==1 thecol=ones_position(j);%把该位置赋给thecol end end ifcommon==2 chkfinish=0;%如果还存在4环,则不结束循环,还进入下一次循环 common=common-1; if(round(rand)==0)%随机决定是保留前面的列还是后面的列 coltoberearranged=thecol;%保留后面的列,交换前面的列 thecol=ones_position(j); else coltoberearranged=ones_position(j);%保留前面的列,交换后面的列 end parity_check(i,coltoberearranged)=3;%makethisentry3sothatwedontuse %ofthisentryagainwhilegettingrid %ofothercylces newrow=unidrnd(rows);%在指定的整数范围内产生一个随机数 iteration=0;%尝试5次在待交换的列中随机查找0 whileparity_check(newrow,coltoberearranged)~=0&iteration<5 newrow=unidrnd(rows); iteration=iteration+1; end ifiteration>=5%超过5次后则扩大范围随机查找非1的0或3,直到找到为止 whileparity_check(newrow,coltoberearranged)==1 newrow=unidrnd(rows); end end %把该列中找到的0或3置为1 parity_check(newrow,coltoberearranged)=1; end%ifcommon==2 end%forj=1: ones_count end%fori=[1: r-1r+1: rows] end%forr=1: rows %如果本次循环已不存在4环,则结束循环,不进入下一次循环 ifchkfinish break end end%forloop=1: 10 %replacethe3'swith0's parity_check=parity_check==1; %GettheParityChecks H=parity_check;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序
![提示](https://static.bingdoc.com/images/bang_tan.gif)