Monday, October 19, 2009

Generator & Syndromer for (15,7) Cyclic Code

Here is the combined generator and decoder matlab script for (15,7) cyclic code.
In the transmitter, (15,7) cyclic code is sent in generator while syndrome is obtaianed in the receiver side. Can refer back to previous post.






%Given generator g(X)=1+X^4+X^6+X^7+X^8 for a (15,7) cyclic code

%Calculate the transmitted cyclic code for a given message and
%show its operation step by step

clc
clear all;
close all;

%Generator part-------------------------------------------------------------
m=[1 0 1 1 1 0 0]; %A given message with k size (can change to any value)
disp(sprintf('\n\nThe message is %d%d%d%d%d%d%d\n\n',m))
m_flp= fliplr(m); %reverse the sequence because MSB is first shifted in
x=zeros(1,8); %initiallized shift register to zero
x_prev = zeros(1,8); %initialized virtual previous state shift register to zero

for i=1: length(m)

%store all previous value
x_prev(1)=x(1);
x_prev(2)=x(2);
x_prev(3)=x(3);
x_prev(4)=x(4);
x_prev(5)=x(5);
x_prev(6)=x(6);
x_prev(7)=x(7);
x_prev(8)=xor(x(8),m_flp(i));

%encoder connection based on the generator g(X)
x(1) = x_prev(8);
x(2) = x_prev(1);
x(3) = x_prev(2);
x(4) = x_prev(3);
x(5) = xor(x_prev(4),x_prev(8));
x(6) = x_prev(5);
x(7) = xor(x_prev(6),x_prev(8));
x(8) = xor(x_prev(7),x_prev(8));

%storing values
x_store(i+1,:)=[x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8)]; %the +1 to include initial state
shift_number(i+1,:)=i;
m_input(i+1)=m_flp(i);

end

%display nicely
for i=1: (length(m)+1) %plus 1 to include initial state
disp(sprintf('Shift number = %d\tRegister-->%d%d%d%d%d%d%d%d,\tinput=%d',shift_number(i),x_store(i,:),m_input(i)))
end

output=[x_store(length(m)+1,:) m]; % Transmited output

disp(sprintf('\n\nThe transmitted cyclic code is %d%d%d%d%d%d%d%d%d%d%d%d%d%d%d\n\n',output))


%Receive part-------------------------------------------------------------

%Calculate the received cyclic code for a given messege and

%show its operation step by step to get syndrome

z=output;

%to enable corrupted bit
%z=xor(output , [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0])

z_flp= fliplr(z); %reverse the sequence because MSB is first shifted in
x=zeros(1,8); %initiallized shift register to zero
x_prev = zeros(1,8); %initiallized virtual previous shift register to zero

for i=1: length(z)

%store all previous value
x_prev(1)=x(1);
x_prev(2)=x(2);
x_prev(3)=x(3);
x_prev(4)=x(4);
x_prev(5)=x(5);
x_prev(6)=x(6);
x_prev(7)=x(7);
x_prev(8)=x(8);

%decoder connection based on the generator g(X)
x(1) = xor(x_prev(8),z_flp(i));
x(2) = x_prev(1);
x(3) = x_prev(2);
x(4) = x_prev(3);
x(5) = xor(x_prev(4),x_prev(8));
x(6) = x_prev(5);
x(7) = xor(x_prev(6),x_prev(8));
x(8) = xor(x_prev(7),x_prev(8));

%storing values
x_store(i+1,:)=[x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8)];
shift_number(i+1,:)=i;
z_input(i+1)=z_flp(i);

end

%display nicely
for i=1: (length(z)+1) %plus 1 to include initial state
disp(sprintf('Shift number = %d\tRegister-->%d%d%d%d%d%d%d%d,\tinput=%d',shift_number(i),x_store(i,:),z_input(i)))
end

disp(sprintf('\n\nThe syndrome is %d%d%d%d%d%d%d%d',x_store(length(z)+1,:)))



------------------------------------------------------
Work screen
------------------------------------------------------

The message is 1011100


Shift number = 0 Register-->00000000, input=0
Shift number = 1 Register-->00000000, input=0
Shift number = 2 Register-->00000000, input=0
Shift number = 3 Register-->10001011, input=1
Shift number = 4 Register-->01000101, input=1
Shift number = 5 Register-->00100010, input=1
Shift number = 6 Register-->00010001, input=0
Shift number = 7 Register-->00001000, input=1


The transmitted cyclic code is 000010001011100


Shift number = 0 Register-->00000000, input=0
Shift number = 1 Register-->00000000, input=0
Shift number = 2 Register-->00000000, input=0
Shift number = 3 Register-->10000000, input=1
Shift number = 4 Register-->11000000, input=1
Shift number = 5 Register-->11100000, input=1
Shift number = 6 Register-->01110000, input=0
Shift number = 7 Register-->10111000, input=1
Shift number = 8 Register-->01011100, input=0
Shift number = 9 Register-->00101110, input=0
Shift number = 10 Register-->00010111, input=0
Shift number = 11 Register-->00000000, input=1
Shift number = 12 Register-->00000000, input=0
Shift number = 13 Register-->00000000, input=0
Shift number = 14 Register-->00000000, input=0
Shift number = 15 Register-->00000000, input=0


The syndrome is 00000000

2 comments:

Eciov said...

I found this example to e very informative and helpful as it teaches the inner working mechanism of a systematic cyclic code generation and decoding, it would nice there is an example on error-correction in the case of the syndrome being non zero string

Preethi Kethineni said...

was very helpful