You are on page 1of 6

LAB 1

%Program to simulate encoding and decoding of (6,3) Linear block code


clear all;
clc;
G=[1 0 0 1 0 1 %code generator
0 1 0 0 1 1
0 0 1 1 1 0]
H=[1 0 1
0 1 1
1 1 0
1 0 0
0 1 0
0 0 1]'
E=[0 0 0 0 0 0
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
1 0 0 0 1 0]
K=size (E,1);
Syndrome=mod(mtimes(E,H'),2); %find Syndrome list
r=[1 1 1 0 1 1]; %Received codeword
display(['Syndrome','Error Pattern'])
display(num2str([Syndrome E]))
x=mod(r*H',2); %compute Syndrome
for kk=1:K,
if Syndrome(kk,:)==x,
idxe=kk; %Find the Syndrome index
end
end %Display Syndrome
syndrome= Syndrome(idxe,:);
error= E(idxe,:);
cword= xor(r,error); %error correction

OUTPUT:G =

1
0
0

0
1
0

0
0
1

1
0
1

0
1
1

1
1
0

1
0
1

0
1
1

1
1
0

1
0
0

0
1
0

0
0
1

H =

E =

0
1
0
0
0
0
0
1

0
0
1
0
0
0
0
0

0
0
0
1
0
0
0
0

0
0
1
1
0
1
0
1

0
1
1
0
0
0
1
1

0
0
0
0
1
0
0
0

0
0
0
0
0
1
0
1

0
0
0
0
0
0
1
0

Syndrome =
0
1
0
1
1
0
0
1

SyndromeError Pattern
0
1
0
1
1
0
0
1

0
0
1
1
0
1
0
1

0
1
1
0
0
0
1
1

0
1
0
0
0
0
0
1

0
0
1
0
0
0
0
0

0
0
0
1
0
0
0
0

0
0
0
0
1
0
0
0

0
0
0
0
0
1
0
1

0
0
0
0
0
0
1
0

syndrome =
0

error =
0
cword =
1

% Program to encode a message for a given(7,4) code, add some noise to


% encoded message, then decode noisy code and should also detect errors.
clear all;
clc;
n = 7;
k = 4;
genmat= [1 0 0 0 1 0 1 ; 0 1 0 0 1 0 0 ; 0 0 1 0 0 1 1 ; 0 0 0 1 1 1 0]
msg = [1 0 1 1; 1 0 0 1; 1 1 1 0]
parmat = gen2par(genmat)%parity matrix from generator matrix
code = encode(msg,7,4,'linear',genmat)%linear encoder
noisycode = rem(code + randerr(3,7,[0 2; .7 .3]),2) %addition of noise
trt = zeros(2^(n-k),n)% transmission media
[newmsg,error] = decode(noisycode,n,k,'linear',genmat,trt),% linear decoder
err_words=find(error~=0)% to find error word

OUTPUT:genmat =
1
0
0
1
0
0
0
0
msg =
1
0
1
0
1
1
parmat =
1
1
0
0
1
0
code =
1
0
1
0
1
1
noisycode =
1
1
1
trt =

0
0
0
0
0
0
0
0

0
0
1
0

0
0
0
1

1
1
0
1

0
0
1
1

1
0
1
0

1
0
1

1
1
0

0
1
1

1
1
0

1
0
0

0
1
0

0
0
1

1
0
1

1
1
0

0
0
0

0
1
1

0
1
0

1
1
1

1
0
1

1
1
0

0
0
0

1
0
1

0
1
0

0
0
0
0
0
0
0
0

0
0
0
0
0
0
0
0

0
0
0
0
0
0
0
0

0
0
0
0
0
0
0
0

0
0
0
0
0
0
0
0

0
0
0
0
0
0
0
0

newmsg =
1
1
1
1
1
1
error =
-1
-1
0
err_words =
1
2

1
0
1

1
1
0

Lab-2
%Program to simulate convolutional encoder.
clear all;
clc;
t= poly2trellis([3],[7,5,6]) % Define trellis.
%g1=[111]g2=[101]g3=[110]
msg=[1 0 0 0]
codeword1=convenc(msg,t)%covolutional encoder
noise=randerr(1,12,[2])
ncode = rem(codeword1+noise,2) % Add noise.
tb = 2; % Traceback length for decoding
decoded = vitdec(ncode,t,tb,'trunc','hard') % Decode.
%trunc-initial state of encoder assumed all zero
%hard-code contains binary input values.
OUTPUT:numInputSymbols:
numOutputSymbols:
numStates:
nextStates:
outputs:

2
8
4
[4x2 double]
[4x2 double]

msg =
1

codeword1 =
1

noise =
1
ncode =
0
decoded =
1

OUTPUT:numInputSymbols:
numOutputSymbols:
numStates:
nextStates:
outputs:

2
8
4
[4x2 double]
[4x2 double]

msg =
1

codeword1 =
1

noise =
0
ncode =
1
decoded =
1

You might also like