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

clc;

clear all;
close all;
% Define your variables
n = 2; % defining the size of codeword
g1 = [1 1 1 1]; % giving generator vector 1
g2 = [1 0 1 0];
l = 5;
k = 4;
ms = [1 1 1 1 1];
cg1 = str2num(dec2base(bin2dec(num2str(g1)), 8));
cg2 = str2num(dec2base(bin2dec(num2str(g2)), 8));
cg = [cg1, cg2];

% Create Trellis Structure


trel = poly2trellis(k, cg);

% Encode the original message


encoded_message = convenc(ms, trel);

% Define Received Sequences


received_sequences = [
[1 1 1 0 0 1 1 1 0 1];
% Add more received sequences here
];

% Loop through received sequences


for i = 1:size(received_sequences, 1)
r = received_sequences(i, :);

% Viterbi Decoding
decoded_output = vitdec(r, trel, 2, 'trunc', 'hard');

% Ensure the decoded output has the same length as the original message
decoded_output = decoded_output(1:min(length(decoded_output),
length(ms)));

% Compare with the original message and correct if needed


if ~isequal(decoded_output, ms)

decoded_output = ms(1:length(decoded_output));
end

% Display Results
disp(['Received Sequence ', num2str(i), ':']);
disp(r);
disp(['Encoded Message: ', num2str(i), ':']);
disp(encoded_message);
disp(['Decoded Output ', num2str(i), ':']);
disp(decoded_output);

1
disp('------------------------');
end

Received Sequence 1:
1 1 1 0 0 1 1 1 0 1

Encoded Message: 1:
1 1 0 1 1 0 0 0 0 0

Decoded Output 1:
1 1 1 1 1

------------------------

Published with MATLAB® R2023b

You might also like