...

LAMPIRAN A LIST PROGRAM

by user

on
Category: Documents
1

views

Report

Comments

Transcript

LAMPIRAN A LIST PROGRAM
LAMPIRAN A
LIST PROGRAM
LAMPIRAN A
Listing Program Utama
clc; clear all
% Insialisasi Masukan %
% ------------------------------------------------------ %
blocklength=200; %Panjang kode
bit_message=100; %Jumlah bit message
wc=3;%weight of column
Iterasi=15;
% Membangun Matrik H %
% ------------------------------------------------------ %
[H,Rj,Ci]=MembuatH(blocklength,bit_message,wc)
% Membangun Matrik G %
% ------------------------------------------------------ %
[G,NewCol]=MembuatG(H);
SNR=[0 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5]
No=1;%Rapat spektral daya satu sisi dari noise
var=No/2; %Variansi dari noise
% Membangkitkan bit message %
% ------------------------------------------------------ %
for z=1:100
message=round(rand(bit_message,1));
message_temp(z,:)=message';
Codeword=LDPCencode(G,NewCol,message);
%Modulasi codeword%
dataout_uncode=BPSK(message); %Data yang tidak dikodekan
dataout=BPSK(Codeword);
DATA_uncode(z,:)=dataout_uncode;
DATA(z,:)=dataout;
end
% Penerima %
% ------------------------------------------------------ %
for i=1:length(SNR)
err_un=0;
err=0;
for k=1:100
Received_uncode=awgn(DATA_uncode(k,:),SNR(i),'measured');
Received=awgn(DATA(k,:),SNR(i),'measured');
RxMessage_uncode=demodBPSK(Received_uncode);
A-1
[RxMessage,Jumlah_Iterasi,Sukses]=SPADecode(H,Rj,Ci,Received,v
ar,Iterasi,NewCol)
[num_un rate_un]=biterr(RxMessage_uncode,message_temp(k,:));
[num rate]=biterr(RxMessage,message_temp(k,:));
err_un=err_un+num_un;
err=err+num;
end
%Menghitung BER
BER_Uncode(i)=err_un/(nummessagebits*k)
BER(i)=err/(nummessagebits*z)
end
Fungsi MembuatH
function [H,Rj,Ci]=MembuatH(blocklength,bit_message,wc);
%Fungsi untuk membangun matrik H dengan input :
%Blocklength : Panjang data
%bit_message : Jumlah bit message
%wc :weight of column
rows=blocklength-bit_message;
cols=blocklength;
wr=ceil(cols*wc/rows);
counter(1:rows,1:wr)=0;
rowcount(1:rows)=0;
%Generate H
for i=1:1:cols
for k=1:1:wc
common(1)=2;
while(max(common)>=2)
common(1:rows)=0;
randnum=round(rand*(rows-1))+1;
while((rowcount(randnum)>wr)||length(find(counter(randnum,:)
==i))>=1)
randnum=round(rand*(rows-1))+1;
end
countertemp=counter(randnum,:);
countertemp(rowcount(randnum)+1)=i;
%menjamin agar tidak terjadi 4 cycle
for j=1:1:rows
if(j~=randnum)
for l=1:1:wr
if(length(find(counter(j,:)==countertemp(l)))>=1 &&
countertemp(l)~=0)
common(j)=common(j)+1;
end
end
end
end
end
A-2
counter(randnum,rowcount(randnum)+1)=i;
rowcount(randnum)=rowcount(randnum)+1;
colcounter(i,k)=randnum;
ParCheck(randnum,i)=1;
end
end
Rj=counter %menunjukan letak bit 1 pada baris matrik H
Ci=colcounter %menunjukan letak bit 1 pada kolom matrik H
Fungsi MembuatG
function [G,newcol]=MembuatG(mat1)
[rows,cols]=size(mat1);
tempH=mat1;
for i=1:rows
NewColPosition(i)=0;
end
%Operasi Gauss-Jordan
for i=1:1:rows
if tempH(i,i)==0
for k=(i+1):1:cols
if (tempH(i,k)==1)
spot=k;
break;
end
end
tempcol=tempH(:,spot);
tempH(:,k)=tempH(:,i);
tempH(:,i)=tempcol;
tempcol=mat1(:,spot);
mat1(:,k)=mat1(:,i);
mat1(:,i)=tempcol;
NewColPosition(i)=spot;
end
for j=1:1:rows
if j~=i
if tempH(j,i)==1
tempH(j,:)=xor(tempH(i,:),tempH(j,:));
end
end
end
end
augmat(1:rows,1:rows)=mat1(1:rows,1:rows);
B(1:rows,1:(cols-rows))=mat1(1:rows,(rows+1):cols);
clear('mat1');
clear('tempH');
newcol=NewColPosition;
%Masukan matrik Identitas
for i=1:1:rows
for j=1:1:rows
A-3
if(i==j)
augmat(i,j+rows)=1;
end
if(i~=j)
augmat(i,j+rows)=0;
end
end
end
%Proses Inversi
for i=1:1:rows
if(augmat(i,i)==0 && i~=rows)
swflag=0;
for k=i+1:1:rows
if(augmat(k,i)==1)
temp=augmat(i,:);
augmat(i,:)=augmat(k,:);
augmat(k,:)=temp;
swflag=1;
break;
end
end
if(swflag==0 || (i==rows && augmat(rows,rows)==0))
disp('Matrik tidak invertable -> Singular')
done=0;
break;
end
end
for j=1:1:rows
if(augmat(j,i)==1 && j~=i)
augmat(j,:)=xor(augmat(i,:),augmat(j,:));
end
end
end
Ainv(1:rows,1:rows)=augmat(1:rows,(rows+1):2*rows)
G=BinaryMultiply(Ainv,B);
for i=1:1:(cols-rows)
for j=1:1:(cols-rows)
if(i==j)
Gprime(rows+i,j)=1;
end
if(i~=j)
G(rows+i,j)=0;
end
end
end
clear('augmat');
Fungsi LDPCencode
function [Codeword]=LDPCencode(G,NewCol,bit_message)
A-4
%Fungsi untuk mendapatkan nilai codeword dengan input :
%G : matrik Generator
%NewCol : Posisi kolom yang baru pada matrik H akibat dari operasi
Gauss-Jordan
%bit_message : bit pesan
CodewordTemp=BinaryMultiply(G,bit_message); %Codeword
rows=length(NewCol);
%Peletakan posisi bit berdasarkan nilai NewCol
for i=rows:-1:1
if(NewCol(i)~=0)
TempBit=CodewordTemp(i);
CodewordTemp(i)=CodewordTemp(NewCol(i));
CodewordTemp(NewCol(i))=TempBit;
end
end
Codeword=CodewordTemp;
clear('Temp');
clear('CodewordTemp');
Fungsi BPSK
function [dataout]=BPSK(datain)
for k=1:length(datain)
b0=datain(k);
if b0==0
phase=1;
elseif b0==1
phase=-1;
end
dataout(k)=phase;
end
end
Fungsi SPAdecode
function[RxMessage,Jumlah_iterasi,Success]=SPADecode(H,Rj,Ci,Recei
ved,var,Iterasi,Newcol)
%Fungsi untuk melakukan algoritma decoding the Sum-Product
sizeofH=size(H);
rows=sizeofH(1);
cols=sizeofH(2);
var=var;
Success=0;
factor=1;
factor1=1;
%Inisialisasi
for i=1:1:cols
for k=1:1:size(Ci,2)
Pi(Ci(i,k),i)=1/(1+exp(-2*Received(i)/var));
Pi(Ci(i,k),i)=1-Pi(Ci(i,k),i);
qmn(0)(Ci(i,k),i)=1-Pi(Ci(i,k),i);
qmn(1)(Ci(i,k),i)=Pi(Ci(i,k),i);
end
A-5
end
%proses decoding
for count=1:1:Iter
%menghitung pesan dari node cek ke node bit
for j=1:1:rows
for kp=1:1:size(Rj,2)
if(Rj(j,kp)~=0)
temp=0.5;
for k=1:1:size(Rj,2)
if(Rj(j,k)~=0 && Rj(j,k)~=Rj(j,kp));
temp=temp*(1-2*qmn(1)(j,Rj(j,k)));
end
end
rmn(0)(j,Rj(j,kp))=0.5+temp;
rmn(1)(j,Rj(j,kp))=1-rmn(0)(j,Rj(j,kp));
end
end
end
%menghitung pesan dari node bit ke node cek
for i=1:1:cols
for kp=1:1:size(Ci,2)
temp0=1;
temp1=1;
for k=1:1:size(Ci,2)
if(Ci(i,k)~=Ci(i,kp))
temp0=temp0*rmn(0)(Ci(i,k),i);
temp1=temp1*rmn(1)(Ci(i,k),i);
end
end
temp0=temp0*(1-Pi(Ci(i,kp),i));
temp1=temp1*Pi(Ci(i,kp),i);
factor1(i)=temp0+temp1;
temp0=temp0/factor1(i);
temp1=temp1/factor1(i);
qmn(0)(Ci(i,kp),i)=temp0;
qmn(1)(Ci(i,kp),i)=temp1;
end
end
%soft decision
for i=1:1:cols
temp0=1;
temp1=1;
for k=1:1:size(Ci,2)
temp0=temp0*rmn(0)(Ci(i,k),i);
temp1=temp1*rmn(1)(Ci(i,k),i);
end
temp0=temp0*(1-Pi(Ci(i,1),i));
temp1=temp1*(Pi(Ci(i,1),i));
factor(i)=temp0+temp1;
temp0=temp0/factor(i);
temp1=temp1/factor(i);
Qi(i)=temp1;
if Qi(i)>0.5
A-6
CodeEst(i)=1;
else
CodeEst(i)=0;
end
end
val=sum(BinaryMultiply(H,transpose(CodeEst)));
if(val==0 && sum(CodeEst)~=0)
Jumlah_iterasi=count;
Success=1;
break
end
end
%Jika tidak sukses
if(Success==0)
NumIterationsPerformed=Iter;
end
%Pesan yang didapatkan
RxMessage=GetMessage(CodeEst,Newcol,cols-rows);
Fungsi DemodBPSK
function Rx_uncode=demodBPSK(datain)
Nbpsc=1;
Rx_uncode=zeros(1,length(datain));
for i=1:length(datain)
s=datain(1,i);
if real(s)<0
s=1;
elseif real(s)>=0
s=0;
end
Rx_uncode(1,i)=s;
end
…
A-7
Fly UP