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)>.5;
%%% coding
for row=1:26
row_coded(row,1:32)=extended_hamming_coding(info(row,:));
end
for col=1:32
col_coded(1:32,col)=(extended_hamming_coding(row_coded(1:26,col)'))';
end
tx=(col_coded-0.5)*2;
tx=reshape(tx,1,[]);
rx=awgn(tx,snr,'measured');
rx=reshape(rx,32,32);
%%% the minimum hamming distance is 4
W=zeros(32,32);
for iteration=1:8
%% column decoding
for col=1:32
%% form test vector.
R=rx(1:32,col)+alpha(iteration)*W(1:32,col);
Y=(1+sign(R))/2;
[minimum,place1]=min(abs(R));
[minimum,place2]=min(abs([R(1:place1-1);inf;R(place1+1:end)]));
if place1>place2
temp=place1;
place1=place2;
place2=temp;
end
[minimum,place3]=min(abs([R(1:place1-1);inf;R(place1+1:place2-1);inf;R(place2+1:end)]));
if place3<place1
temp=place3;
place3=place2;
place2=place1;
place1=temp;
elseif place3<place2
temp=place3;
place3=place2;
place2=temp;
end
T1=[Y(1:place1-1);0;Y(place1+1:place2-1);0;Y(place2+1:place3-1);0;Y(place3+1:end)];
T2=[Y(1:place1-1);0;Y(place1+1:place2-1);0;Y(place2+1:place3-1);1;Y(place3+1:end)];
T3=[Y(1:place1-1);0;Y(place1+1:place2-1);1;Y(place2+1:place3-1);0;Y(place3+1:end)];
T4=[Y(1:place1-1);0;Y(place1+1:place2-1);1;Y(place2+1:place3-1);1;Y(place3+1:end)];
T5=[Y(1:place1-1);1;Y(place1+1:place2-1);0;Y(place2+1:place3-1);0;Y(place3+1:end)];
T6=[Y(1:place1-1);1;Y(place1+1:place2-1);0;Y(place2+1:place3-1);1;Y(place3+1:end)];
T7=[Y(1:place1-1);1;Y(place1+1:place2-1);1;Y(place2+1:place3-1);0;Y(place3+1:end)];
T8=[Y(1:place1-1);1;Y(place1+1:place2-1);1;Y(place2+1:place3-1);1;Y(place3+1:end)];
C=[];
S=mod(H*T1,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T1(k)=mod(T1(k)+1,2);
end
end
if flag==1 C=[C,T1]; end
S=mod(H*T2,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T2(k)=mod(T2(k)+1,2);
end
end
if flag==1 C=[C,T2];end
S=mod(H*T3,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T3(k)=mod(T3(k)+1,2);
end
end
if flag==1 C=[C,T3];end
S=mod(H*T4,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T4(k)=mod(T4(k)+1,2);
end
end
if flag==1 C=[C,T4];end
S=mod(H*T5,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T5(k)=mod(T5(k)+1,2);
end
end
if flag==1 C=[C,T5];end
S=mod(H*T6,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T6(k)=mod(T6(k)+1,2);
end
end
if flag==1 C=[C,T6];end
S=mod(H*T7,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T7(k)=mod(T7(k)+1,2);
end
end
if flag==1 C=[C,T7];end
S=mod(H*T8,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T8(k)=mod(T8(k)+1,2);
end
end
if flag==1 C=[C,T8];end
[m,n]=size(C);
if n==0 C=Y; end
[D,R_out]=reliability_computing(C,R,beta(iteration));
W(1:32,col)=R_out-R;
end
%%%% row decoding
decided=zeros(26,32);
for row=1:26
%% form test vector.
R=rx(row,1:32)'+alpha(iteration)*W(row,1:32)';
Y=(1+sign(R))/2;
[minimum,place1]=min(abs(R));
[minimum,place2]=min(abs([R(1:place1-1);inf;R(place1+1:end)]));
if place1>place2
temp=place1;
place1=place2;
place2=temp;
end
[minimum,place3]=min(abs([R(1:place1-1);inf;R(place1+1:place2-1);inf;R(place2+1:end)]));
if place3<place1
temp=place3;
place3=place2;
place2=place1;
place1=temp;
elseif place3<place2
temp=place3;
place3=place2;
place2=temp;
end
T1=[Y(1:place1-1);0;Y(place1+1:place2-1);0;Y(place2+1:place3-1);0;Y(place3+1:end)];
T2=[Y(1:place1-1);0;Y(place1+1:place2-1);0;Y(place2+1:place3-1);1;Y(place3+1:end)];
T3=[Y(1:place1-1);0;Y(place1+1:place2-1);1;Y(place2+1:place3-1);0;Y(place3+1:end)];
T4=[Y(1:place1-1);0;Y(place1+1:place2-1);1;Y(place2+1:place3-1);1;Y(place3+1:end)];
T5=[Y(1:place1-1);1;Y(place1+1:place2-1);0;Y(place2+1:place3-1);0;Y(place3+1:end)];
T6=[Y(1:place1-1);1;Y(place1+1:place2-1);0;Y(place2+1:place3-1);1;Y(place3+1:end)];
T7=[Y(1:place1-1);1;Y(place1+1:place2-1);1;Y(place2+1:place3-1);0;Y(place3+1:end)];
T8=[Y(1:place1-1);1;Y(place1+1:place2-1);1;Y(place2+1:place3-1);1;Y(place3+1:end)];
C=[];
S=mod(H*T1,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T1(k)=mod(T1(k)+1,2);
end
end
if flag==1 C=[C,T1];end
S=mod(H*T2,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T2(k)=mod(T2(k)+1,2);
end
end
if flag==1 C=[C,T2];end
S=mod(H*T3,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T3(k)=mod(T3(k)+1,2);
end
end
if flag==1 C=[C,T3];end
S=mod(H*T4,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T4(k)=mod(T4(k)+1,2);
end
end
if flag==1 C=[C,T4];end
S=mod(H*T5,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T5(k)=mod(T5(k)+1,2);
end
end
if flag==1 C=[C,T5];end
S=mod(H*T6,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T6(k)=mod(T6(k)+1,2);
end
end
if flag==1 C=[C,T6];end
S=mod(H*T7,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T7(k)=mod(T7(k)+1,2);
end
end
if flag==1 C=[C,T7];end
S=mod(H*T8,2);
flag=0;
if sum(S)==0
flag=1;
end
for k=1:32
if sum(S==H(:,k))==6
flag=1;
T8(k)=mod(T8(k)+1,2);
end
end
if flag==1 C=[C,T8];end
[m,n]=size(C);
if n==0 C=Y; end
[D,R_out]=reliability_computing(C,R,beta(iteration));
decided(row,1:32)=D';
W(row,1:32)=(R_out-R)';
end
end
decoded=decided(1:26,1:26);
info=reshape(info,1,[]);
decoded=reshape(decoded,1,[]);
err=err+sum(info~=decoded);
end
Pe=[Pe,err/(Frame_num*26*26)];
end
semilogy(EbN0,Pe,'*k-');
Pe
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -