Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

N=input('N : '); K=input('K : ');

g=input('gen : ');

binary_matrix = generate_binary_matrix(K);

for i=1:2^K
coded = cyclic_encoder(binary_matrix(i,:),N,K,g);
res(i,:)=coded;
end

dminn = calculate_d_min(res);
disp('hamming distance is ');disp(dminn);

err_crct=(dminn-1)/2;

disp('number of errors that can be corrected is ');disp(err_crct);

function binary_matrix = generate_binary_matrix(n)


% Generate binary matrix
binary_matrix = zeros(2^n, n); % Initialize binary matrix
for i = 0:2^n-1
binary_number = dec2bin(i, n); % Convert integer to binary
binary_matrix(i+1, :) = arrayfun(@str2double,
num2cell(binary_number)); % Convert string to array and assign to matrix
row
end
end

function d_min = calculate_d_min(binary_matrix)


[num_rows, ~] = size(binary_matrix);
d_min = inf; % Initialize minimum Hamming distance to infinity
for i = 1:num_rows
for j = i+1:num_rows % Compare each pair of rows only once
hamming_distance = sum(binary_matrix(i, :) ~= binary_matrix(j,
:));
if hamming_distance < d_min
d_min = hamming_distance;
end
end
end
end

function coded= cyclic_encoder(msg_seq,N,K,g)


% Cyclic (N,K) encoding of input msg_seq m with generator polynomial g
Lmsg=length(msg_seq); Nmsg=ceil(Lmsg/K);
Msg= [msg_seq(:); zeros(Nmsg*K-Lmsg,1)];
Msg= reshape(Msg,K,Nmsg).';
coded= [];
for n=1:Nmsg
msg= Msg(n,:);
for i=1:N-K, x(i)=0; end
for k=1:K
tmp= rem(msg(K+1-k)+x(N-K),2); % msg(K+1-k)+g(N-K+1)*x(N-K)
for i=N-K:-1:2, x(i)= rem(x(i-1)+g(i)*tmp,2); end
x(1)=g(1)*tmp;
end
coded= [msg x];
end

You might also like