0% found this document useful (0 votes)
3 views3 pages

Cs Program Code

The document contains MATLAB code for three types of error-correcting codes: Cyclic Block Code, Linear Block Code, and Convolution Coding. Each section prompts the user for input parameters, generates codewords, introduces random errors, and decodes the received messages while displaying relevant matrices and results. The code ensures that the message length is less than the code length for both block coding methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Cs Program Code

The document contains MATLAB code for three types of error-correcting codes: Cyclic Block Code, Linear Block Code, and Convolution Coding. Each section prompts the user for input parameters, generates codewords, introduces random errors, and decodes the received messages while displaying relevant matrices and results. The code ensures that the message length is less than the code length for both block coding methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CYCLIC BLOCK CODE:

clc;
clear all;
%%%%code can take values (7,4),(7,3),(8,3),(8,4),....
codeLength=input('Enter the length of the code :'); %Length of the output code word
messageLength=input('Enter the length of the message:'); %Length of the input message bits
if (messageLength<codeLength)
Message = input('Enter the message bits:');
disp(Message);

disp ('Cyclic polynomial');


cyclicPolynomial=cyclpoly(codeLength,messageLength,'min'); %Creates a polynomials
for cyclic codes
disp(cyclicPolynomial);

disp('Encoded word');
%Encodes the message bits using cyclic code
code=encode(Message,codeLength,messageLength,'cyclic/fmt',cyclicPolynomial);
disp(code);

disp('Error pattern generation’);


error=randerr(1,codeLength); %Introduces a random one bit error
disp(error);

disp('Received vector');
receivedCode = xor(code,error); %Xor the error with the code word
disp(receivedCode);

disp('Decode message bits');


%Decodes the received code word and recovers the original message
msg=decode(receivedCode,codeLength,messageLength,'cyclic');
disp(msg);
else
disp('k value should be less than n');
end
LINEAR BLOCK CODE
clc;
clear all;
n=input('Enter the code size(n):'); %Length of the output code word
k=input('Enter the message size(k):'); %Length of the input message bits
if(k<n)
m=input('Enter the message:');
p=[1 1 0;0 1 1;1 1 1;1 0 1]; %Parity matrix
g=[eye(k),p]; %Generator matrix
disp('Generator matrix:');
disp (g);

% encode
c=rem(m*g,2); %Create the code by multiplying the message and generator polynomial
disp('coded message at transmission side:');
disp (c);

% noise
e=randerr(1,n);
r=xor(c,e); %Introduce a random one bit error in the message
disp('Received code at received side:');
disp(r);
%Parity matrix
h=[eye(n-k),[p]']; %Creates a parity check matrix
disp('Parity matrix:');
disp(h);
%Syndrome
disp('Syndrome');
ht=h'; % Transpose of the parity check matrix
s=mod(r*ht,2); % Calculates the syndrome value from the received code and parity
matrix
disp(s);
%Find the error bit from the transpose of the parity check matrix
for j=1:n
t=n-k;
for i=1:n-k
if(s(1,i)==ht(j,i))
t=t-1;
end
end
if(t==0)
break;
end
end
disp('Position of errorbit is');
disp(j);

%Error pattern
r(j)=-r(j); %Correct the error
disp('Corrected code:');
disp(r);

for i=1:k
dm(i)=r(i); %The first k bits of the corrected code word is the message
end
disp('Decoded message:');
disp(dm);

else
disp('k should be less than n');
end

CONVOLUTION CODING
clc;
clear all;
close all;
m=[1 0 1 1];
p=2 %Number of flipflops
z=zeros(1,p);
mm=horzcat(m,z); %Additional zeros added with message sequence
x=[]; % flipflop states
c=[]; %code vector
for i = 1:1:length(mm)
d1(i+1) = mm(i);
d2(i+1) = d1(i);
x=[x; d1(i) d2(i)]; % states of shift register
u(i) = xor(x(i,1),x(i,2));
c1(i) = xor(u(i), mm(i));
c2(i)=xor(mm(i), x(i,2));
c= [c c1(i) c2(i)]
end
disp („ States of the shift register:‟);
x
disp(„Code Vector‟)
c

You might also like