Activity 1 & 2 - Manual
Activity 1 & 2 - Manual
Activity 1
Name:__________________________ USN:_________________
1. Consider a source that generates symbols with the following probabilities:
Symbol Probability
A 0.4
B 0.3
C 0.2
D 0.1
Encode the sequence 'AABBCCDD' using the Huffman code using the Scilab platform.
Program:
Probabilities = [0.4, 0.3, 0.2, 0.1];
symbols = ['A', 'B', 'C', 'D'];
function [huffman_code, avg_length] = huffman(probabilities, symbols)
n = length(probabilities);
huffman_code = cell(1, n);
sorted_probs = probabilities;
sorted_symbols = symbols;
while length(sorted_probs) > 1
[sorted_probs, sorted_symbols] = sort_symbols(sorted_probs, sorted_symbols);
min1 = sorted_symbols{1};
min2 = sorted_symbols{2};
sorted_symbols = sorted_symbols(3:$);
for i = 1:length(min1)
huffman_code{min1(i)} = ['0', huffman_code{min1(i)}];
end
for i = 1:length(min2)
huffman_code{min2(i)} = ['1', huffman_code{min2(i)}];
end
combined_prob = sum(probabilities(min1)) + sum(probabilities(min2));
sorted_probs = [sorted_probs, combined_prob];
sorted_symbols = [sorted_symbols, {[min1, min2]}];
probabilities = [probabilities, combined_prob];
end
avg_length = 0;
for i = 1:n
avg_length = avg_length + probabilities(i) * length(huffman_code{i});
end
end
function [sorted_probs, sorted_symbols] = sort_symbols(probabilities, symbols)
[sorted_probs, idx] = gsort(probabilities);
sorted_symbols = symbols(idx);
end
sequence = 'AABBCCDD';
[huffman_code, avg_length] = huffman(probabilities, symbols);
encoded_sequence = '';
for i = 1:length(sequence)
idx = find(symbols == sequence(i));
encoded_sequence = [encoded_sequence, huffman_code{idx}];
end
disp('Huffman Code:');
disp(huffman_code);
disp('Encoded Sequence:');
disp(encoded_sequence);
disp('Average Length:');
disp(avg_length);
// The encoded sequence 'AABBCCDD'
2. Given the alphabet {A, B, C, D} with probabilities: P(A) = 0.4, P(B) = 0.3, P(C) = 0.2 & P(D) = 0.1
Encode the sequence 'AABBCCDD' using arithmetic coding using the Scilab platform.
// Define the probabilities
probabilities = [0.4, 0.3, 0.2, 0.1];
// Define the sequence to be encoded
sequence = "AABBCCDD";
// Initialize the range and cumulative probabilities
lower_bound = 0;
upper_bound = 1;
cumulative_prob = cumsum(probabilities);
// Encode the sequence
for i = 1:length(sequence)
symbol = sequence(i);
index = find(["A", "B", "C", "D"] == symbol);
% Calculate the new range
range = upper_bound - lower_bound;
upper_bound = lower_bound + range * cumulative_prob(index);
lower_bound = lower_bound + range * cumulative_prob(index - 1);
end
// Output the encoded value (average of the final range)
encoded_value = (upper_bound + lower_bound) / 2;
disp(encoded_value);
// This code will output the encoded value for the sequence 'AABBCCDD' using arithmetic coding.
3. Consider a source that generates symbols with the following frequencies:
Symbol Probability
A 40
B 30
C 20
Symbol Probability
D 10
Apply the Shannon-Fano-Elias coding algorithm to encode the symbol 'ABBACD' using the Scilab platform.
// Define the symbols and their probabilities
symbols = ["A", "B", "C", "D"];
probabilities = [40, 30, 20, 10];
// Sort symbols and probabilities in descending order of probabilities
[probabilities_sorted, indices] = gsort(probabilities, 'd');
symbols_sorted = symbols(indices);
// Define a function to generate Shannon-Fano codes recursively
function codes = generate_codes(symbols, probabilities, prefix)
n = length(symbols);
if n == 1
codes = struct(symbols, prefix);
else
% Find the split index
cumulative_prob = cumsum(probabilities);
split_index = find(cumulative_prob >= sum(probabilities)/2, 1, 'first');
% Generate codes recursively for left and right groups
left_symbols = symbols(1:split_index);
right_symbols = symbols(split_index+1:end);
left_probabilities = probabilities(1:split_index);
right_probabilities = probabilities(split_index+1:end);
left_codes = generate_codes(left_symbols, left_probabilities, prefix + "0");
right_codes = generate_codes(right_symbols, right_probabilities, prefix + "1");
% Merge left and right codes
codes = struct(left_codes, right_codes);
end
endfunction
// Generate Shannon-Fano codes
codes = generate_codes(symbols_sorted, probabilities_sorted, "");
// Encode the sequence 'ABBACD' using the generated codes
sequence = "ABBACD";
encoded_sequence = "";
for i = 1:length(sequence)
encoded_sequence = encoded_sequence + codes.(sequence(i));
end
disp(encoded_sequence);
// Encoded sequence for 'ABBACD' using Shannon-Fano-Elias coding algorithm.
4. Consider a (7, 4) Hamming code with the generator matrix:
G = [1 0 0 0 1 1 0
0100101
0010011
0 0 0 1 1 1 1]
Encode the message vector m = [1 0 1 1] using the given generator matrix the Scilab platform.
// Define the generator matrix
G = [1 0 0 0 1 1 0;
0 1 0 0 1 0 1;
0 0 1 0 0 1 1;
0 0 0 1 1 1 1];
// Define the message vector
m = [1 0 1 1];
// Encode the message using matrix multiplication
encoded_message = m * G;
disp(encoded_message);
// Encoded codeword: 1 1 1 1 0 1 0
5. Consider a (7,4) cyclic Hamming code with the generator polynomial: g(x) = 1+x2+x3
Determine the codeword for the message vector m= [1011].
// Define the message vector
m = [1 0 1 1];
// Define the generator polynomial coefficients
g = [1 0 1 1];
// Perform polynomial multiplication
c = gfconv(m, g);
// Extract the coefficients of the codeword polynomial
codeword = c.coeffs;
// Display the codeword
disp("Codeword:");
disp(codeword);
// Codeword C = [0 0 0 0 0 0 0].
6. Consider a (2,1) convolutional encoder circuit shown in the figure,
Fig. 6
Encode the message 11011101.
function encoded_message = convolutional_encode(message, G1, G2)
% Encodes the message 'message' using generator polynomials 'G1' and 'G2'
% Initialize shift registers
reg1 = zeros(1, K); % K = constraint length (replace with actual value)
reg2 = zeros(1, K);
encoded_message = [];
% Loop through each bit of the message
for i = 1:length(message)
% Shift registers
reg1 = shift_register(reg1, 1);
reg2 = shift_register(reg2, 1);
% Update registers with current bit
reg1(1) = message(i);
% Perform XOR operations based on generator polynomials
out1 = mod2_add(reg1, G1);
out2 = mod2_add(reg2, G2);
% Append output bits to encoded message
encoded_message = [encoded_message, out1(1), out2(1)];
end
end
// Encoded message: [1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1]
Activity 2
Name:__________________________ USN:_________________
1. Using Matlab, take two prime numbers and then compute Public and Private keys. Then encrypt the
message using the public key and decrypt using Private key.
function [publicKey, privateKey] = generateKeys(p, q)
% Calculate modulus and phi(n)
n = p * q;
phi_n = (p - 1) * (q - 1);
% Choose a random public exponent (coprime with phi_n)
e = randi(phi_n - 1);
while gcd(e, phi_n) ~= 1
e = randi(phi_n - 1);
end
% Calculate the private exponent using modular inverse
d = modinv(e, phi_n);
% Public and private key pairs
publicKey = [e, n];
privateKey = [d, n];
end
function cipher = encrypt(message, publicKey)
% Extract public key components
e = publicKey(1);
n = publicKey(2);
% Encrypt the message using modular exponentiation
cipher = modpow(message, e, n);
end
function message = decrypt(cipher, privateKey)
% Extract private key components
d = privateKey(1);
n = privateKey(2);
% Decrypt the message using modular exponentiation
message = modpow(cipher, d, n);
end
% Example usage
p = 11;
q = 13;
2. Implement a public key cryptosystem using MATLAB with AES encryption and decryption.
function encrypted_data = aes_encrypt(data, key)
% Convert key and data to uint8 arrays
key = uint8(key);
data = uint8(data);
% Use Cipher object for AES encryption in CBC mode
cipher = Cipher('AES-256/CBC');
cipher.Key = key;
cipher.Mode = 'CBC';
encrypted_data = cipher.encrypt(data);
end
function decrypted_data = aes_decrypt(encrypted_data, key)
% Convert key and encrypted data to uint8 arrays
key = uint8(key);
encrypted_data = uint8(encrypted_data);
% Use Cipher object for AES decryption in CBC mode
cipher = Cipher('AES-256/CBC');
cipher.Key = key;
cipher.Mode = 'CBC';
decrypted_data = cipher.decrypt(encrypted_data);
end
3. Implementing the RSA algorithm in MATLAB for generating public and private keys, encrypting a
message, and decrypting the ciphertext.
function [publicKey, privateKey] = generateKeys(p, q)
% Calculate modulus and phi(n)
n = p * q;
phi_n = (p - 1) * (q - 1);