0% found this document useful (0 votes)
9 views28 pages

ITC Lab Mannual Using MATLAB Programs

The document contains several MATLAB programs that compute entropy, mutual information, and implement various coding techniques including Shannon-Fano, Huffman, Lempel-Ziv, linear block codes, and cyclic block codes. Each program defines probabilities, calculates entropy, average code length, and coding efficiency, and includes functions for encoding and decoding messages. The examples provided illustrate the application of these algorithms for different types of channels and coding methods.

Uploaded by

Mayank Katiyar
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)
9 views28 pages

ITC Lab Mannual Using MATLAB Programs

The document contains several MATLAB programs that compute entropy, mutual information, and implement various coding techniques including Shannon-Fano, Huffman, Lempel-Ziv, linear block codes, and cyclic block codes. Each program defines probabilities, calculates entropy, average code length, and coding efficiency, and includes functions for encoding and decoding messages. The examples provided illustrate the application of these algorithms for different types of channels and coding methods.

Uploaded by

Mayank Katiyar
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
You are on page 1/ 28

# Program1.

#Write a MATLAB program to compute entropy and mutual


information for noise free and noisy channel
% Define the probabilities of each symbol in the input message
p = [0.2, 0.3, 0.1, 0.4]; % Example probabilities, you can change them as needed

% Calculate entropy
H = -sum(p .* log2(p));

% Define the channel transition matrix (probabilities of each output symbol given
an input symbol)
channel_matrix = [0.7, 0.1, 0.1, 0.1; % Probability of output given input A
0.2, 0.6, 0.1, 0.1; % Probability of output given input B
0.1, 0.1, 0.8, 0.0; % Probability of output given input C
0.1, 0.1, 0.1, 0.7]; % Probability of output given input D

% Calculate the joint probabilities of input-output pairs


joint_probs = p' * channel_matrix;

% Calculate the conditional entropy


H_conditional = -sum(sum(joint_probs .* log2(joint_probs)));

% Calculate mutual information


I = H - H_conditional;
disp(['Entropy (H): ', num2str(H)]);
disp(['Conditional Entropy (H(X|Y)): ', num2str(H_conditional)]);
disp(['Mutual Information (I): ', num2str(I)]);
# Program 2
#.Write a MATLAB program to compute entropy and mutual
information for error free and binary symmetric
% Define the probabilities of each symbol in the input message
p = [0.5, 0.5]; % For a binary input, equal probabilities for 0 and 1

% Calculate entropy for error-free channel


H_error_free = -sum(p .* log2(p));

% Define the crossover probability for the binary symmetric channel


p_crossover = 0.1; % Example crossover probability, you can change it as needed

% Calculate probabilities for the binary symmetric channel


p_channel = [1-p_crossover, p_crossover; p_crossover, 1-p_crossover];

% Calculate entropy for binary symmetric channel


H_binary_symmetric = -sum(p .* sum(p_channel .* log2(p_channel), 2));

% Calculate mutual information for error-free channel


I_error_free = H_error_free;

% Calculate joint probabilities for binary symmetric channel


joint_probs_binary_symmetric = [p .* (1-p_crossover), p .* p_crossover; p .*
p_crossover, p .* (1-p_crossover)];
% Calculate conditional entropy for binary symmetric channel
H_conditional_binary_symmetric = -sum(sum(joint_probs_binary_symmetric .*
log2(joint_probs_binary_symmetric)));

% Calculate mutual information for binary symmetric channel


I_binary_symmetric = H_error_free - H_conditional_binary_symmetric;

disp('For the error-free channel:');


disp(['Entropy (H): ', num2str(H_error_free)]);
disp(['Mutual Information (I): ', num2str(I_error_free)]);

disp(' ');

disp('For the binary symmetric channel:');


disp(['Entropy (H): ', num2str(H_binary_symmetric)]);
disp(['Conditional Entropy (H(X|Y)): ',
num2str(H_conditional_binary_symmetric)]);
disp(['Mutual Information (I): ', num2str(I_binary_symmetric)]);

#This program calculates the entropy and mutual information for both an error-free
channel and a binary symmetric channel. You can adjust the probabilities and the
crossover probability for the binary symmetric channel according to your specific
scenario.
# Program3.
#Write a MATLAB program to implement algorithm for generation
and evaluation of Shannan Fano coding and decoding. compute
entropy, average length and coding efficiency.
% Define the probabilities of each symbol in the input message
p = [0.2, 0.3, 0.15, 0.15, 0.1, 0.1]; % Example probabilities, you can change them
as needed

% Sort probabilities in descending order


[sorted_p, idx] = sort(p, 'descend');

% Calculate cumulative probabilities


cumulative_p = cumsum(sorted_p);

% Function to generate Shannon-Fano code recursively


function [codes, lengths] = generate_code(probabilities, prefix)
% Base case: if there's only one symbol left, assign code and return
if length(probabilities) == 1
codes{1} = prefix;
lengths(1) = length(prefix);
else
% Find the split point
total_prob = sum(probabilities);
half_prob = total_prob / 2;
split_idx = find(cumulative_p <= half_prob, 1, 'last');

% Generate codes for the left and right partitions recursively


[left_codes, left_lengths] = generate_code(probabilities(1:split_idx),
strcat(prefix, '0'));
[right_codes, right_lengths] = generate_code(probabilities(split_idx+1:end),
strcat(prefix, '1'));

% Combine the codes


codes = [left_codes, right_codes];
lengths = [left_lengths, right_lengths];
end
end

% Generate Shannon-Fano code


[codes, lengths] = generate_code(sorted_p, '');

% Sort the codes based on the original probabilities order


codes = codes(idx);
lengths = lengths(idx);

% Display codes and lengths


disp('Symbol | Probability | Code | Length');
for i = 1:length(p)
fprintf('%d\t%f\t%s\t%d\n', i, p(i), codes{i}, lengths(i));
end

% Calculate entropy
H = -sum(p .* log2(p));

% Calculate average code length


L_avg = sum(p .* lengths);

% Calculate coding efficiency


efficiency = H / L_avg;

% Display results
disp(['Entropy (H): ', num2str(H)]);
disp(['Average Code Length (L_avg): ', num2str(L_avg)]);
disp(['Coding Efficiency: ', num2str(efficiency)]);
This program generates the Shannon-Fano codes for a given set of probabilities,
computes entropy, average code length, and coding efficiency.
#Program 4
#Write a matlab program to implement algorithm for generation and
evaluation of Huffman coding and decoding. Compute entropy
average length and coding efficiency.
% Define the probabilities of each symbol in the input message
p = [0.2, 0.3, 0.15, 0.15, 0.1, 0.1]; % Example probabilities, you can change them
as needed

% Function to generate Huffman code


function [codes, lengths] = huffman_coding(probabilities)
% Initialize symbol nodes
symbols = cell(length(probabilities), 1);
for i = 1:length(probabilities)
symbols{i} = struct('symbol', i, 'probability', probabilities(i), 'code', '');
end

% Build Huffman tree


while length(symbols) > 1
[probabilities, indices] = sort(probabilities);
symbols = symbols(indices);

% Merge two symbols with the lowest probabilities


new_symbol = struct('symbol', [symbols{1}.symbol, symbols{2}.symbol], ...
'probability', symbols{1}.probability + symbols{2}.probability, ...
'code', '');
symbols{1} = new_symbol;
symbols(2) = [];

% Update codes
symbols{1}.code = strcat(symbols{1}.code, '0');
symbols{2}.code = strcat(symbols{2}.code, '1');
end

% Extract codes and lengths


codes = cell(length(probabilities), 1);
lengths = zeros(length(probabilities), 1);
for i = 1:length(probabilities)
codes{i} = symbols{i}.code;
lengths(i) = length(symbols{i}.code);
end
end

% Generate Huffman code


[codes, lengths] = huffman_coding(p);

% Display codes and lengths


disp('Symbol | Probability | Code | Length');
for i = 1:length(p)
fprintf('%d\t%f\t%s\t%d\n', i, p(i), codes{i}, lengths(i));
end

% Calculate entropy
H = -sum(p .* log2(p));

% Calculate average code length


L_avg = sum(p .* lengths);

% Calculate coding efficiency


efficiency = H / L_avg;

% Display results
disp(['Entropy (H): ', num2str(H)]);
disp(['Average Code Length (L_avg): ', num2str(L_avg)]);
disp(['Coding Efficiency: ', num2str(efficiency)]);
This program generates the Huffman codes for a given set of probabilities,
computes entropy, average code length, and coding efficiency.
#Program5
#Write a MATLAB program to implement algorithm for generation
and evaluation of Lempel Ziv dictionary method. Compute entropy
average length and coding efficiency.
% Define the input data
input_data = 'abcbabcbabaaaa';

% Function to perform LZ77 compression


function [compressed_data, dictionary] = lz77_compress(input_data)
compressed_data = [];
dictionary = {};
i = 1;
while i <= length(input_data)
% Search for the longest match in the dictionary
longest_match_length = 0;
longest_match_index = 0;
for j = 1:min(length(dictionary), i-1)
if strcmp(input_data(i:i+longest_match_length-1), dictionary{j})
longest_match_length = longest_match_length + 1;
longest_match_index = j;
else
break;
end
end
% Append the match to the compressed data
if longest_match_length > 0
compressed_data = [compressed_data, [longest_match_index-1,
longest_match_length, input_data(i+longest_match_length)]];
dictionary{end+1} = input_data(i:i+longest_match_length);
i = i + longest_match_length + 1;
else
compressed_data = [compressed_data, [0, 0, input_data(i)]];
dictionary{end+1} = input_data(i);
i = i + 1;
end
end
end

% Perform LZ77 compression


[compressed_data, dictionary] = lz77_compress(input_data);

% Display the compressed data


disp('Compressed Data:');
disp(compressed_data);

% Function to calculate entropy


function H = calculate_entropy(data)
unique_symbols = unique(data);
p = zeros(1, length(unique_symbols));
for i = 1:length(unique_symbols)
p(i) = sum(data == unique_symbols(i)) / length(data);
end
H = -sum(p .* log2(p));
end

% Calculate entropy of the input data


entropy_input = calculate_entropy(input_data);

% Calculate entropy of the compressed data


compressed_symbols = unique(compressed_data(:, 1:2));
compressed_lengths = compressed_data(:, 2);
compressed_entropy = calculate_entropy([compressed_symbols(:)',
compressed_lengths(:)']);

% Calculate average length of the compressed data


average_length = mean(sum(compressed_data(:, 1:2), 2) + 1);

% Calculate coding efficiency


coding_efficiency = entropy_input / average_length;

% Display results
disp(['Entropy of Input Data: ', num2str(entropy_input)]);
disp(['Entropy of Compressed Data: ', num2str(compressed_entropy)]);
disp(['Average Length of Compressed Data: ', num2str(average_length)]);
disp(['Coding Efficiency: ', num2str(coding_efficiency)]);
This program performs LZ77 compression on the input data, calculates its
entropy, average length, and coding efficiency.
#Program6
#Write a MATLAB program to implement algorithm for encoding and
decoding of linear block code.
#Implementing a linear block code encoder and decoder involves
defining a generator matrix for encoding and a parity check matrix for
decoding. Here's a MATLAB program that implements a simple (7, 4)
Hamming code, which encodes 4-bit messages into 7-bit codewords
and decodes them back:
% Define generator matrix for (7, 4) Hamming code
G=[
1 0 0 0 1 1 1;
0 1 0 0 1 0 1;
0 0 1 0 0 1 1;
0001110
];

% Define parity check matrix


H=[
1 1 0 1 1 0 0;
0 1 1 1 0 1 0;
1011001
];

% Function to encode a 4-bit message into a 7-bit codeword


function codeword = encode(message, G)
codeword = mod(message * G, 2);
end

% Function to decode a received codeword using syndrome decoding


function decoded_message = decode(received_codeword, H)
syndrome = mod(received_codeword * H', 2);
% Syndrome table for error correction
syndrome_table = [
0 0 0 0 0 0 0 0;
1 1 1 0 0 0 0 1;
1 1 0 1 1 0 1 0;
10110110
];
error_index = find(ismember(syndrome_table, syndrome, 'rows'));
if ~isempty(error_index)
received_codeword(error_index) = mod(received_codeword(error_index) +
1, 2); % Flip the erroneous bit
end
decoded_message = received_codeword(1:4);
end

% Example usage:
message = [1 0 1 1]; % Example 4-bit message
codeword = encode(message, G); % Encode the message
disp(['Encoded Codeword: ', num2str(codeword)]);
received_codeword = codeword;
% Introduce an error in the received codeword
received_codeword(3) = mod(received_codeword(3) + 1, 2);
decoded_message = decode(received_codeword, H); % Decode the received
codeword
disp(['Decoded Message: ', num2str(decoded_message)]);
This program defines the generator matrix G and parity check matrix H for a (7, 4)
Hamming code. It includes functions to encode a 4-bit message into a 7-bit
codeword using matrix multiplication and to decode a received codeword using
syndrome decoding.
#Program7
#Write a MATLAB program to implement algorithm for encoding and
decoding of cyclic block code.
#Implementing a cyclic block code encoder and decoder involves defining a
generator polynomial for encoding and a syndrome polynomial for decoding.
Here's a MATLAB program that implements a simple (7, 4) cyclic code, which
encodes 4-bit messages into 7-bit codewords and decodes them back:
% Define generator polynomial for (7, 4) cyclic code (example: x^3 + x^2 + 1)
gen_poly = [1 1 0 1];

% Function to encode a 4-bit message into a 7-bit codeword


function codeword = encode(message, gen_poly)
n = length(gen_poly) - 1; % Length of generator polynomial
m = length(message); % Length of message
padded_message = [message, zeros(1, n - m)]; % Pad message with zeros
remainder = [padded_message, zeros(1, n)]; % Initialize remainder
for i = 1:m
if remainder(1) == 1
remainder = mod(remainder + gen_poly, 2);
end
remainder = [remainder(2:end), padded_message(i)];
end
codeword = [message, mod(padded_message + remainder(end-n+1:end), 2)];
end
% Function to decode a received codeword using syndrome decoding
function decoded_message = decode(received_codeword, gen_poly)
n = length(gen_poly) - 1; % Length of generator polynomial
m = length(received_codeword) - n; % Length of message
syndrome = mod(conv(received_codeword, fliplr(gen_poly)), 2);
error_pos = find(syndrome, 1);
if isempty(error_pos)
decoded_message = received_codeword(1:m);
else
error_vector = zeros(1, length(received_codeword));
error_vector(end-error_pos+1) = 1;
corrected_codeword = mod(received_codeword + error_vector, 2);
decoded_message = corrected_codeword(1:m);
end
end

% Example usage:
message = [1 0 1 1]; % Example 4-bit message
codeword = encode(message, gen_poly); % Encode the message
disp(['Encoded Codeword: ', num2str(codeword)]);
received_codeword = codeword;
% Introduce an error in the received codeword
received_codeword(3) = mod(received_codeword(3) + 1, 2);
decoded_message = decode(received_codeword, gen_poly); % Decode the
received codeword
disp(['Decoded Message: ', num2str(decoded_message)]);
This program defines the generator polynomial for a (7, 4) cyclic code and
includes functions to encode a 4-bit message into a 7-bit codeword using
polynomial division and to decode a received codeword using syndrome
decoding. Finally, it demonstrates an example of encoding, introducing an error,
and decoding a message. You can adjust the generator polynomial and the
message accordingly.
#Program8
#Write a MATLAB program to implement algorithm for generating
convolutional code by code tree.
#Generating a convolutional code using a code tree involves defining
the code tree structure and traversing it to generate the code
sequences. Here's a MATLAB program to generate a convolutional
code using a code tree:
% Define the generator polynomials for the convolutional code
g1 = [1 1 1]; % Example generator polynomial 1
g2 = [1 0 1]; % Example generator polynomial 2

% Define the constraint length


K = length(g1) - 1;

% Function to generate the code tree


function tree = generate_code_tree(K, g1, g2)
num_states = 2^K; % Number of states in the code tree
tree = cell(num_states, 1); % Initialize the code tree

% Initialize the state transition matrix


transition_matrix = zeros(num_states, 2);
for i = 1:num_states
input_0 = mod([i-1, bitshift(i, -1)], 2); % Input sequence for transition with
input 0
input_1 = mod([bitshift(i, -1), 1], 2); % Input sequence for transition with
input 1
transition_matrix(i, 1) = bi2de(conv(input_0, fliplr(g1)), 'left-msb'); % Next
state for input 0
transition_matrix(i, 2) = bi2de(conv(input_1, fliplr(g2)), 'left-msb'); % Next
state for input 1
end

% Populate the code tree


for state = 1:num_states
tree{state} = struct('input', [], 'output', []);
for input = 0:1
next_state = transition_matrix(state, input+1);
tree{state}(input+1).input = input;
tree{state}(input+1).output = mod([fliplr(conv([input, 0], g1(2:end))),
fliplr(conv([0, input], g2(2:end)))], 2);
tree{state}(input+1).next_state = next_state + 1;
end
end
end

% Generate the code tree


code_tree = generate_code_tree(K, g1, g2);

% Display the code tree


disp('Code Tree:');
disp(code_tree);
This program defines the generator polynomials and generates a convolutional
code tree using these polynomials. Each node in the tree represents a state in the
convolutional code, and each transition represents a possible input and the
corresponding output.
#Program9
#Write a MATLAB program to implement algorithm for generating
convolutional code by code trellis.
#To generate a convolutional code using the trellis representation, we can
utilize MATLAB's built-in function poly2trellis. Here's a MATLAB program that
generates a convolutional code using the trellis representation:
% Define the generator polynomials for the convolutional code
g1 = [1 1 1]; % Example generator polynomial 1
g2 = [1 0 1]; % Example generator polynomial 2

% Define the constraint length


K = length(g1) - 1;

% Convert the generator polynomials to a trellis structure


trellis = poly2trellis(K, [g1; g2]);

% Display the trellis structure


disp('Trellis Structure:');
disp(trellis);
This program uses the poly2trellis function to convert the generator polynomials
into a trellis structure. The trellis structure defines the states, transitions, and
outputs of the convolutional code. You can adjust the generator polynomials and
the constraint length according to your specific requirements.
#Program10
#Write a MATLAB program to implement algorithm for encoding and
decoding of BCH code.
#To implement encoding and decoding of BCH (Bose-Chaudhuri-
Hocquenghem) codes in MATLAB, you can use the bchenco and
bchdec functions provided by the Communications Toolbox. Here's an
example program:
% Define parameters
n = 15; % Codeword length
k = 9; % Message length
t = 2; % Error correction capability

% Generate a random message vector


msg = randi([0 1], 1, k);

% Encode the message


codeword = bchenco(msg, n, k);

% Introduce errors into the codeword


received_codeword = codeword;
error_indices = randperm(n, t); % Introduce t random errors
received_codeword(error_indices) = mod(received_codeword(error_indices) + 1,
2);

% Decode the received codeword


decoded_msg = bchdec(received_codeword, n, k);

% Display results
disp('Original Message:');
disp(msg);
disp('Encoded Codeword:');
disp(codeword);
disp('Received Codeword with Errors:');
disp(received_codeword);
disp('Decoded Message:');
disp(decoded_msg);
This program demonstrates how to encode a random message using a BCH code,
introduce errors into the codeword, and then decode the received codeword to
recover the original message. Adjust the parameters n, k, and t according to your
specific requirements.

You might also like