📄 ldpc_demo.m
字号:
clear all;
clc;
%%%%%%%%%%%% for total variable%%%%%%%%%%%%%%%%%
total_count = 0;
Count = 10;
loop = 0;
%%%%%%%%%%%%define dimension of the parity check matrix%%%%%%%%
rows=128;
cols=256;
h=gen_ldpc(rows,cols);%generates a 100 x 200 H matrix( the IG parity check matrix)
% load h.mat;
% h = check1;
[newh,rearranged_cols]=rearrange_cols(h);
% newh holds the rearranged version of H and rearranged_cols holds the
% rearrangement order of columns
%%%%%%%%%%%%Get A and B matrices %%%%%%%%%%%%%%%%
for i=1:rows
for j=1:rows
A(i,j)=newh(i,j);
end
end
for i=1:rows
for j=rows+1:cols
B(i,j-rows)=newh(i,j);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while loop < Count
%%%%%%%%%%%%%%message bits%%%%%%%%%%%%%%%%%%%%%
%generate message bits randomly
%for j=1:10%simulate 1000 flame
for i=1:cols-rows
s(i)=round(rand);
end
%%%%%%%%%%%%%%%% for the test of writing C code.%%%%%%%%%%%%%%%%%%%
%s(1) = 1; s(2) = 0; s(3) = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if loop == 0
s=s';
end
% make it a row vector
%%%%%%% or load s from file %%%%type "help load" at matlab prompt for details
%%%%%%%%%%%%%% get check bits %%%%%%%%%%%%%%%%
d=mul_GF2(inv_GF2(A),B);
c=mul_GF2(d,s);
%%%%%%%%%%%%%%%%%form u'%%%%%%%%%%%%% the info bits is at back of check bits.
u1=c;
for i=rows+1:cols
u1(i)=s(i-rows);
end
%%%%%%%%%%%%%%% reorder u?to get the CW u %%%%%%%%%%%%%%%%%%%
u=reorder_bits(u1,rearranged_cols);
%%%%%%%%%%%%%%% Modulation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% assume the codeword c, the parity check matrix H and No are already loaded into the MATLAB memory
tx_waveform=bpsk(u,1); %amp= 1 , this is the modulator with bpsk.
%%%%%%%%%%%%%% for the channel. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
No=7; % for setting the Eb/No(SNR).
% rx_waveform=awgn(tx_waveform,No); % add noise.
%%%%%%%%%%%%% modified by lilei %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sigma = add_noise(tx_waveform, No, 1, 1);
rx_waveform = tx_waveform + sigma * randn(1,length(tx_waveform));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
scale(1:length(u))=1; %No fading.
%%%%%%%%%%%%% LDPC decoder %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
vhat=decode_ldpc(rx_waveform,No,1,h,scale); %since amp=1;
% assume we have the rearranged_cols array output by the rearrange_cols( ) function already loaded into MATLAB memory
uhat = extract_mesg(vhat,rearranged_cols);
message = uhat';
%%%%%%%%%%%%%%% u is the codeword to be transmitted %%%%%%%%%%%%%%%%%%%
nerr=0;
errmax=mod((s+uhat'),2);
for i=1:(cols - rows)
if(errmax(i)==1)
nerr=nerr+1;
end
end
%end
total_count = total_count + nerr;
loop = loop + 1;
loop
end
% ber=nerr/(rows*10);
ber = total_count / ((cols - rows) * Count);
ber
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -