?? genh.m
字號:
function [H]=genH(rows,cols)
row_flag(1:rows)=0;
parity_check=zeros(rows,cols);
%add bits_per_col 1's to each column with the only constraint being that the 1's should be
%placed in distinct rows
%%%%%%%%使每列隨機產生3個1即列重為3%%%%%%%%%%%%%
bits_per_col=3;
for i=1:cols
a=randperm(rows);
for j=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);
%add 1's to rows having no 1(a redundant row) or only one 1(that bit in the codeword becomes
%zero irrespective of the input)
for i=1:rows
if row_flag(i)==0 %如果該行沒有1,則隨機添加兩個1
for k=1:2
j=unidrnd(cols);
while parity_check(i,j)==1
j=unidrnd(cols);
end
parity_check(i,j)=1; %在找到的新位置上置1
row_flag(i)=row_flag(i)+1; %行重加1
end
end
if row_flag(i)==1 %如果該行只有1個1,則隨機再添加1個1
j=unidrnd(cols);
while parity_check(i,j)==1
j=unidrnd(cols);
end
parity_check(i,j)=1;
row_flag(i)=row_flag(i)+1;
end
end
%try to distribute the ones so that the number of ones per row is as uniform as possible
%嘗試在列上分散1的位置,使得每行1的個數均衡(相近或相一致)
for i=1:rows
j=1;
a=randperm(cols);
while row_flag(i)>max_ones_per_row; %如果該行行重大于允許的最大行重,則進行處理
if parity_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<rows
newrow=unidrnd(rows);
k=k+1;
end
if parity_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%if test
j=j+1;
end%while loop
end%for loop
%try to eliminate cycles of length 4 in the factor graph
%嘗試刪除4環
for loop=1:10
chkfinish=1;
for r=1:rows
ones_position=find(parity_check(r,:)==1);
ones_count=length(ones_position);
for i=[1:r-1 r+1:rows]
common=0;
for j=1:ones_count
if parity_check(i,ones_position(j))==1
common=common+1 ;
if common==1
thecol=ones_position(j);
end
end
if common==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; %make this entry 3 so that we dont use
%of this entry again while getting rid
%of other cylces
newrow=unidrnd(rows);
iteration=0; %嘗試5次在待交換的列中隨機查找0
while parity_check(newrow,coltoberearranged)~=0 & iteration<5
newrow=unidrnd(rows);
iteration=iteration+1;
end
if iteration>=5 %超過5次后則擴大范圍隨機查找非1的0或3,直到找到為止
while parity_check(newrow,coltoberearranged)==1
newrow=unidrnd(rows);
end
end
%把該列中找到的0或3置為1
parity_check(newrow,coltoberearranged)=1;
end%if common==2
end%for j=1:ones_count
end%for i=[1:r-1 r+1:rows]
end%for r=1:rows
%如果本次循環已不存在4環,則結束循環,不進入下一次循環
if chkfinish
break
end
end%for loop=1:10
%replace the 3's with 0's
parity_check=parity_check==1;
%Get the Parity Checks
H=parity_check;
%%%%%下面的求方差僅用作評估%%%%
%%計算列重
%col_flag(1:cols)=0;
%for j=1:cols
% ind=find(H(:,j)==1);
% col_flag(j)=length(ind);
%end
%%計算行重
%row_flag(1:rows)=0;
%for i=1:rows
% ind=find(H(i,:)==1);
% row_flag(i)=length(ind);
%end
%%每行1的個數的方差
%variance=var(row_flag);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -