Vous êtes sur la page 1sur 6

BCH Encoder:

In Matlab you can implement the BCH encoder by the encoding circuit of
cyclic code. The generator polynomial for (7, 4) BCH code will be g(X) = 1+
X + X
2
. The encoder circuit will be the following,



You can easily implement this encoder by following way.

clear all;
close all;

u = [0 0 1 0]; %Message
u_flp = fliplr(u); %To Flip the msg
x = zeros(1,3); %Initialize the shift registers
x_prev = zeros(1,3); %Previous states of Registers

for i = 1 : length(u)
x_prev(1) = x(1);
x(1) = xor(x_prev(3), u_flp(i));

x_prev(2) = x(2);
x(2) = xor(x_prev(1),x(1));

x(3) = x_prev(2);
x_prev(3) = x(3);
end
codeword = [x u] %Codeword

The output is:
codeword =
1 1 1 0 0 1 0
You can verify this program by Matlab BCH encoder function like following
way,
clear all;
close all;

n = 7;
k = 4;
u = [0 1 0 0]; % Note that msg should be flipped order here
msg = gf(u); % Take msg in GF array
codeword = bchenc(msg, n, k) %Generate codeword

The output is,

codeword = GF(2) array.

Array elements =

0 1 0 0 1 1 1

Note that the output is generated in the reverse way of previous one.

Similarly you can encode a sequence of binary message in the following way.

clear all;
close all;

u = randn(1,100)>0; %Generate Binary 100 msg
x = zeros(1,3); %Initialize registers
x_prev = zeros(1,3); %Previous states of Registers
codeword = []; %Codeword storage empty matrix

for j = 1:4:length(u)
block = u(j:j+4-1);
block_flp = fliplr(block); %To flip

for i = 1 : length(block)
x_prev(1) = x(1);
x(1) = xor(x_prev(3), block_flp(i));

x_prev(2) = x(2);
x(2) = xor(x_prev(1),x(1));

x(3) = x_prev(2);
x_prev(3) = x(3);
end
codeword = [codeword x block];
end

Useful Matlab function cell()

Matlab can create Cell array by cell() function.
Example:
x = cell(4,4) %Creating 4 rows and 4 column empty matrices
x(1,1) = {[0 1 1 ]};
x
x{1,1}
y = [0 1 1];
isequal(x{1,1},y)

The output is
x =
[] [] [] []
[] [] [] []
[] [] [] []
[] [] [] []
x =
[1x3 double] [] [] []
[] [] [] []
[] [] [] []
[] [] [] []
ans =
0 1 1
ans =
1
This function may be useful to store the syndrome table and the standard
array.
Convolutional codes:
The structure of convolutional codes is different from that of block codes.
During each unit of time, the input to a convolutional code encoder is also a
k-bit message block and the corresponding output is an n-bit coded block
with k<n.
Each coded n-bit output block depends not only on the corresponding k bit
input message block, but also on the m previous message blocks.
Convolutional codes are usually specified by three parameters (m, k, n)
n = number of input bits
k = number of output bits
m = number of memory registers
Rate 2/1= R convolutional codes with (m, k, n) = (2,1,2) and two generator
sequences are:
g1 = (1 0 1)
g2 = (1 1 1)

Suppose input sequence is:
input = (1 0 1 1 1 0 0 )

Then the two output sequences are:
output1 = (1 0 0 1 0 1 1 )
output2 = (1 1 0 0 1 0 1 )

The entire output sequence is:
output = (1 1, 0 1, 0 0, 1 0, 0 1, 1 0, 1 1, )

State diagram:

Each circle represents a state. At any one time, the encoder resides in one of
these states. The lines to and from it shows state transition when bits arrive.
The state diagram does not have time dimension.

Trellis Diagram:

Trellis diagram is messy but generally preferred over state diagram because
it represents linear time sequencing of events. The x-axis is discrete time
and all possible states are shown in y-axis. Each transition means new bits
have arrived. There are only two choices at each state; they are determined
by the arrival of 1 bit or 0 bit. The arrow going upwards represents a 1 bit
and going downwards represents a 0 bit. The trellis diagram is unique to
each code.





Example for a convolutional coder:

clear all;
close all;

% input_bit_num is the number of bits entering
% the memory register of encoder at one time
input_bit_num=1;
% G is the generator matrix of the convolutional code
% detemines the output sequence of a binary convlutional code
% with n0 rows and p*input_bit_num columns, its rows are
%G1,G2,...Gn
G=[1 0 1; 1 1 1;];
% input the binary input sequence
input=[1 0 1 1 1 0 0],
n=length(input)/input_bit_num;
%determine p and n0
p=size(G,2)/input_bit_num;
n0=size(G,1);
% add extra zeros
encd_seq=[zeros(1,2),input,zeros(1,2)];
% generate uu, a matrix whose columns are the contents of
% convolutional encoder at various clock cycles
u1=encd_seq(p*input_bit_num:-1:1);
for i=1:n+p-2
u1=[u1,encd_seq((i+p)*input_bit_num:-1:i*input_bit_num+1)];
end

uu=reshape(u1,p*input_bit_num,n+p-1);
%determine the output
output=reshape(rem(G*uu,2),1,n0*(p+n-1)),

The output is:
input =
1 0 1 1 1 0 0

output =
1 1 0 1 0 0 1 0 0 1 1 0 1 1 0 0 0 0

Vous aimerez peut-être aussi