You are on page 1of 5

FUNCTIONS:

LY - EC -2 (C-BATCH)
WIRELESS COMMUNICATION (2171004)
OEP TOPIC: LDPC coding and decoding

(1) PARSE.h
>> function [Gprime,newcol]=ParseH(mat1)
[rows,cols]=size(mat1);
%Column Rearrangement to guarantee non-singular Matrix
tempH=mat1;
for i=1:rows
NewColPosition(i)=0;
end
%Performs Gauss-Jordan on dummy variable to move columns of H to make
%submatrix A invertable
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
%Reassign Matrices to proper location
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;
%Augment Identity Matrix with Square Matrix
for i=1:1:rows

for j=1:1:rows
if(i==j)
augmat(i,j+rows)=1;
end
if(i~=j)
augmat(i,j+rows)=0;
end
end
end
%Begin GF2 Inversion
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('Matrix was not 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
%Augment with Identity matrix to create a full generation matrix
Ainv(1:rows,1:rows)=augmat(1:rows,(rows+1):2*rows);
Gprime=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)
Gprime(rows+i,j)=0;
end
end
clear('augmat');

(2) LDPCencoder.h
>> function [Codeword]=LDPCencode(G,NewColArrangement,Message)
%Encodes the given message
CodewordTemp=BinaryMultiply(G,Message); %Create Codeword
rows=length(NewColArrangement);
%Perform position adjustments based on column rearrangment of H
for i=rows:-1:1
if(NewColArrangement(i)~=0)
TempBit=CodewordTemp(i);
CodewordTemp(i)=CodewordTemp(NewColArrangement(i));
CodewordTemp(NewColArrangement(i))=TempBit;
end
end
Codeword=CodewordTemp;
clear('Temp');
clear('CodewordTemp');
(3) BinaryMultiply.h
>> function [result]=BinaryMultiply(mat1,mat2)
%Performs GF2 (binary) Multiplication of 2 Matrices
[row1,col1]=size(mat1);
[row2,col2]=size(mat2);
if(col1==row2)
for i=1:1:row1
for j=1:1:col2
result(i,j)=mod(sum(and(mat1(i,:),transpose(mat2(:,j)))),2);
end
end
end
if(col1~=row2)
disp('Error.. Matrices cannot be multiplied')
end
(4) GetMessage.h
>> function[Message]=GetMessage(Codeword,NewColArrangement,MessageLength)
%Returns Message from codeword
for i=1:1:MessageLength
if(NewColArrangement(i)~=0)
TempBit=Codeword(i);
Codeword(i)=Codeword(NewColArrangement(i));
Codeword(NewColArrangement(i))=TempBit;
end
end
Message(1:MessageLength)=Codeword((length(Codeword)-MessageLength)+1:lengt
h(Codeword));

MAIN PROGRAM:
>> mat=[1 1 0 1 1 1 0 1
1 0 1 1 0 0 0 1];
M=[1
0
1
0
1
1];
Newcol=ones(length(M));
[G,N]=ParseH(mat);
disp(G);
disp(N);
C=LDPCencode(G,Newcol,M);
disp(C);
[Msg]=GetMessage(C,Newcol,6);
disp(Msg);

OUTPUT:
>> Implement
GENERATOR MATRIX:
1
1
0
0
1
0
1
1
1
0
0
0
0
1
0
0
0
0
1
0
0
0
0
1
0
0
0
0
0
0
0
0
0

0
0
0
0
0
0
1
0

1
0
0
0
0
0
0
1

RECEIVED MESSAGE
0
1
0
1
0
0
1
1
DECODED MESSAGE
1
0
1
0