You are on page 1of 2

 MATLAB CODE:

while(1)
x = ' Enter type of block Code([7, 4], [8, 4], etc) =';
type = input(x);
typeh = [type(:,1)-type(:,2),type(:,1)];
clc
if ((length(type)== 2 ) && type(:, 1)> type(:, 2) )
break
clc
else
disp('error: Invalid Input')
disp(' Enter again: ')
end
end
if(type == [7 4])
H = [ 1 0 1 1 1 0 0; 1 1 0 1 0 1 0; 0 1 1 1 0 0 1];
end
if(type == [8 4])
H = [ 1 0 1 1 1 0 0 0; 1 1 0 1 0 1 0 0; 0 1 1 1 0 0 1 0; 1 1 1 0 0 0 0 1];
end
disp(['Block code: (',num2str(type),')'])
disp('Parity check matrix :')
H
disp('Generator matrix :')
G = gen2par(H)
while(1)
x = 'Input Msg word : ';
msg = input(x);
if (length(msg)==type(:,2))
break ;
else
disp('error: Invalid msg input')
disp('Please enter again ')
end
end
code = mod(double(msg)*double(G),2);
rx_code = code ;
disp(['Code Word : [',num2str(code),']'])
x = 'Number of error bits = ';
err_num = input(x);
while(1)
k = 0;
bit_nos = ceil(10*rand(1,err_num));
for j = 1:err_num
if(bit_nos(:, j) <= type(:, 1))
k = k+1;
end
end
if(k >=err_num)
break
end
end
rx_code(:,bit_nos) = mod(double(code(:,bit_nos))+double(1),2);
disp(['Recived Code Word: [',num2str(rx_code),']'])
H = transpose(H);
sydrm = mod(double(rx_code)*double(H),2);
disp(['Syndrome: [',num2str(sydrm),']'])
H = transpose(H);
j = 1;
while(1)
if(H(:,j) == transpose(sydrm))
break;
disp(' !!Error Detected!!')
else
j = j + 1;
if(j > type(:,1))
break;
disp('Error detected but cannot be corrected')
end
end
if(sydrm == [ 0 0 0 ])
break;
end
end
if(j <= type(:, 1))
rx_code(:,j) = mod(double(rx_code(:,j))+double(1),2);
disp(['Corrected Code Word: [',num2str(rx_code),']'])
end
rx_msg = rx_code(:, type(:,2) : type(:,1));
disp(['Recived Message: [',num2str(rx_msg),']'])

You might also like