0% found this document useful (0 votes)
6 views12 pages

Lab 3

The document presents a lab report by student Nisarg Bhatia (ID: 202301187) detailing various coding techniques including entropy calculation, Huffman encoding, Lempel-Ziv encoding, and arithmetic coding. It includes MATLAB code snippets for each technique, results of encoded lengths, efficiency comparisons, and successful decoding outcomes. The report emphasizes originality and proper citation of sources.

Uploaded by

nisargbhatia0001
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)
6 views12 pages

Lab 3

The document presents a lab report by student Nisarg Bhatia (ID: 202301187) detailing various coding techniques including entropy calculation, Huffman encoding, Lempel-Ziv encoding, and arithmetic coding. It includes MATLAB code snippets for each technique, results of encoded lengths, efficiency comparisons, and successful decoding outcomes. The report emphasizes originality and proper citation of sources.

Uploaded by

nisargbhatia0001
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/ 12

LAB_3_202301187

Student ID: 202301187

I, [Nisarg Bhatia(202301187)], declare that

• the work that I am presenting is my own work

• I have not copied the work (Matlab code, results, etc.) that someone else has done

• Concepts, understanding and insights I will be describing are my own

• Wherever I have relied on an existing work that is not my own, I have provided a proper

reference citation

• I make this pledge truthfully. I know that violation of this solemn pledge can carry

grave consequences

QUE:1

P1 = [1/2, 1/4, 1/8, 1/16, 1/32, 1/32]; % Source S1


P2 = ones(1,6) / 6; % Source S2

H1 = -sum(P1 .* log2(P1));
H2 = -sum(P2 .* log2(P2));

fprintf('Entropy of Source S1: %f \n', H1);

Entropy of Source S1: 1.937500

fprintf('Entropy of Source S2: %f \n', H2);

Entropy of Source S2: 2.584963

if H1 > H2
fprintf('Source S1 has greater entropy.\n');
elseif H1 < H2
fprintf('Source S2 has greater entropy.\n');
else
fprintf('Both sources have equal entropy.\n');

1
end

Source S2 has greater entropy.

QUE:2

function sourceCodingAnalysis()

% Load Data
load('discreteSources.mat');
M = 10^6;

% Huffman Encoding (S1, S2)


[encS1, huffCodesS1] = huffEnc(S1);
[encS2, huffCodesS2] = huffEnc(S2);
mencS1 = length(encS1);
mencS2 = length(encS2);

fprintf('(c) Encoded Lengths:\n');


fprintf(' MencS1: %d bits\n', mencS1);
fprintf(' MencS2: %d bits\n', mencS2);

% (c) Comparing Menc/M with R


rS1 = calcR(S1);
rS2 = calcR(S2);
fprintf('\n(c) Ratio Comp:\n');
fprintf(' MencS1/M: %.4f, rS1: %.4f\n', mencS1 / M, rS1);
fprintf(' MencS2/M: %.4f, rS2: %.4f\n', mencS2 / M, rS2);

% (d) Calculating Entropy and Efficiency


entrS1 = calcEntr(S1);
entrS2 = calcEntr(S2);
effS1Menc = (entrS1 * M) / mencS1;
effS2Menc = (entrS2 * M) / mencS2;
effS1R = entrS1 / rS1;
effS2R = entrS2 / rS2;

fprintf('\n(d) Efficiency:\n');
fprintf(' S1 (Menc): %.4f, S2 (Menc): %.4f\n', effS1Menc, effS2Menc);
fprintf(' S1 (R): %.4f, S2 (R): %.4f\n', effS1R, effS2R);

% (e) Adhoc Encoding (S1, S2) in which taking symbols of higher


% probabilities

2
[adhocS1, adhocCodesS1] = adhocEnc(S1);
[adhocS2, adhocCodesS2] = adhocEnc(S2);
mencAdhocS1 = length(adhocS1);
mencAdhocS2 = length(adhocS2);

fprintf('\n(e) Adhoc Encoded Lengths:\n');


fprintf(' MencAdhocS1: %d bits\n', mencAdhocS1);
fprintf(' MencAdhocS2: %d bits\n', mencAdhocS2);

fprintf('\n(e) Adhoc Ratio Comparision:\n');


fprintf(' MencAdhocS1/M: %.4f, rS1: %.4f\n', mencAdhocS1 / M, rS1);
fprintf(' MencAdhocS2/M: %.4f, rS2: %.4f\n', mencAdhocS2 / M, rS2);

effAdhocS1Menc = (entrS1 * M) / mencAdhocS1;


effAdhocS2Menc = (entrS2 * M) / mencAdhocS2;

fprintf('\n(e) Adhoc Efficiency (Menc):\n');


fprintf(' S1 (Menc): %.4f, S2 (Menc): %.4f\n', effAdhocS1Menc,
effAdhocS2Menc);
end

function [encStr, huffCodes] = huffEnc(src)


syms = unique(src);
freqs = zeros(1, length(syms));
for i = 1:length(syms)
freqs(i) = sum(src == syms(i));
end
probs = freqs / length(src);
[huffCodes] = genHuffCodes(syms, probs, 'descend');
encStr = '';
for i = 1:length(src)
encStr = [encStr, huffCodes(num2str(src(i)))];
end
end

function [encStr, adhocCodes] = adhocEnc(src)


syms = unique(src);
freqs = zeros(1, length(syms));
for i = 1:length(syms)
freqs(i) = sum(src == syms(i));
end
probs = freqs / length(src);
[adhocCodes] = genHuffCodes(syms, probs, 'ascend');
encStr = '';
for i = 1:length(src)
encStr = [encStr, adhocCodes(num2str(src(i)))];
end
end

function [huffCodes] = genHuffCodes(syms, probs, order)

3
nodes = struct('sym', num2cell(syms), 'prob', num2cell(probs), 'left',
cell(1, length(syms)), 'right', cell(1, length(syms)));
while length(nodes) > 1
[~, idx] = sort([nodes.prob], order);
nodes = nodes(idx);
left = nodes(1);
right = nodes(2);
newNode.sym = [];
newNode.prob = left.prob + right.prob;
newNode.left = {left};
newNode.right = {right};
nodes = nodes(3:end);
nodes(end + 1) = newNode;
end
root = nodes(1);
huffCodes = containers.Map('KeyType', 'char', 'ValueType', 'char');
huffCodes = genCodesRec(root, '', huffCodes);
end

function huffCodes = genCodesRec(node, code, huffCodes)


if ~isempty(node.sym)
huffCodes(num2str(node.sym)) = code;
else
if ~isempty(node.left)
huffCodes = genCodesRec(node.left{1}, [code, '0'], huffCodes);
end
if ~isempty(node.right)
huffCodes = genCodesRec(node.right{1}, [code, '1'], huffCodes);
end
end
end

function entr = calcEntr(src)


syms = unique(src);
probs = zeros(1, length(syms));
for i = 1:length(syms)
probs(i) = sum(src == syms(i)) / length(src);
end
probs(probs == 0) = [];
entr = -sum(probs .* log2(probs));
end

function r = calcR(src)
syms = unique(src);
probs = zeros(1, length(syms));
for i = 1:length(syms)
probs(i) = sum(src == syms(i)) / length(src);
end
r = sum(probs .* ceil(log2(1 ./ probs)));
end

4
sourceCodingAnalysis();

(c) Encoded Lengths:


MencS1: 4532734 bits
MencS2: 3336588 bits

(c) Ratio Comp:


MencS1/M: 4.5327, rS1: 2.1851
MencS2/M: 3.3366, rS2: 3.0000

(d) Efficiency:
S1 (Menc): 0.4271, S2 (Menc): 0.7747
S1 (R): 0.8859, S2 (R): 0.8617

(e) Adhoc Encoded Lengths:


MencAdhocS1: 1935764 bits
MencAdhocS2: 2665780 bits

(e) Adhoc Ratio Comparision:


MencAdhocS1/M: 1.9358, rS1: 2.1851
MencAdhocS2/M: 2.6658, rS2: 3.0000

(e) Adhoc Efficiency (Menc):


S1 (Menc): 1.0000, S2 (Menc): 0.9697

QUE:3

function lempelZivExample()
source = [1, 2, 3, 4, 5, 6];
encodedString = lzEncodeExample(source);
fprintf('LZ Encoded String (Binary): %s\n', encodedString);
decodedSource = lzDecodeExample(encodedString, max(source));
fprintf('LZ Decoded Source: ');
fprintf('%d ', decodedSource);
fprintf('\n');
if isequal(source, decodedSource)
fprintf('Decoding successful.\n');
else
fprintf('Decoding failed.\n');
end
end

function encodedString = lzEncodeExample(source)


dictionary = {};
encodedString = '';
currentString = [];
maxSource = max(source);
symbolBits = ceil(log2(maxSource));

for i = 1:length(source)

5
currentString = [currentString, source(i)];
currentStringChar = cellfun(@num2str, num2cell(currentString),
'UniformOutput', false);

found = false;
for j = 1:length(dictionary)
if isequal(dictionary{j}, currentStringChar)
found = true;
break;
end
end

if ~found
if isempty(dictionary)
index = 0;
else
currentPrefixChar = cellfun(@num2str,
num2cell(currentString(1:end-1)), 'UniformOutput', false);

index = 0;
for j = 1:length(dictionary)
if isequal(dictionary{j}, currentPrefixChar)
index = j;
break;
end
end
end

binaryIndex = dec2bin(index, ceil(log2(length(dictionary) + 1)));


symbolBinary = dec2bin(source(i), symbolBits);

encodedString = [encodedString, binaryIndex, symbolBinary];


dictionary{end + 1} = currentStringChar;
currentString = [];
end
end
end

function decodedSource = lzDecodeExample(encodedString, maxSource)


dictionary = {};
decodedSource = [];
indexBits = 0;
symbolBits = ceil(log2(maxSource));

i = 1;
while i <= length(encodedString)
if isempty(dictionary)
indexBits = 1;
else
indexBits = ceil(log2(length(dictionary) + 1));

6
end
if i + indexBits + symbolBits - 1 > length(encodedString)
break;
end

indexBinary = encodedString(i:i + indexBits - 1);


symbolBinary = encodedString(i + indexBits:i + indexBits +
symbolBits - 1);

index = bin2dec(indexBinary);
symbol = bin2dec(symbolBinary);

if index == 0
decodedSource = [decodedSource, symbol];
dictionary{end + 1} = {num2str(symbol)};
else
if index <= length(dictionary)
decodedSource = [decodedSource, cellfun(@str2num,
dictionary{index}), symbol];
dictionary{end + 1} = [dictionary{index}, {num2str(symbol)}];
else
break;
end
end

i = i + indexBits + symbolBits;
end
end

lempelZivExample();

LZ Encoded String (Binary): 000100100001100100000101000110


LZ Decoded Source: 1 2 3 4 5 6
Decoding successful.

%%QUE4

% Arithmetic Coding (Encoding)


function enc = arith_enc(syms, probs)
l = 0;
h = 1;
for i = 1:length(syms)
sym = syms(i);
pl = 0;
for j = 1:sym - 1
pl = pl + probs(j);
end

7
ph = pl + probs(sym);
nl = l + (h - l) * pl;
nh = l + (h - l) * ph;
l = nl;
h = nh;
end
enc = (l + h) / 2;

end
% Arithmetic Coding (Decoding)
function dec_syms = arith_dec(enc, probs, num_syms)
l = 0;
h = 1;
dec_syms = [];
for i = 1:num_syms
for sym = 1:length(probs)
pl = 0;
for j = 1:sym - 1
pl = pl + probs(j);
end
ph = pl + probs(sym);
nl = l + (h - l) * pl;
nh = l + (h - l) * ph;
if enc >= nl && enc < nh
dec_syms = [dec_syms, sym];
l = nl;
h = nh;
break;
end
end
end
end

% Example Usage:
syms1 = [1, 2, 1, 3];
probs = [0.4, 0.3, 0.2, 0.1];
enc1 = arith_enc(syms1, probs);
dec_syms1 = arith_dec(enc1, probs, length(syms1));
syms2 = [1, 1, 2, 2, 3];
enc2 = arith_enc(syms2, probs);
dec_syms2 = arith_dec(enc2, probs, length(syms2));
disp(['Encoded 1: ', num2str(enc1)]);

Encoded 1: 0.1984

disp(['Decoded 1: ', num2str(dec_syms1)]);

Decoded 1: 1 2 1 3

disp(['Encoded 2: ', num2str(enc2)]);

8
Encoded 2: 0.09472

disp(['Decoded 2: ', num2str(dec_syms2)]);

Decoded 2: 1 1 2 2 3

%%QUE:5

L = 6;
st = -3;
en = 3;

pl = linspace(st, en, L);


mx_it = 100;
t = 0.00001;
p = 0;

for it = 1:mx_it

b1 = 0.5 * (pl(1:end-1) + pl(2:end));


b2 = [-inf, b1, inf];

d = 0;

for i = 1:L
x1 = b2(i);
x2 = b2(i+1);

d = d + integral(@(x)((x - pl(i)).^2) .* (1 / sqrt(4 * pi)) .* exp(-


x.^2 / 4), x1, x2);
end

if abs(d - p) < t
break;
end

px = zeros(size(pl));

for i = 1:L
a1 = b2(i);
a2 = b2(i+1);

f1 = @(x) x .* (1 / sqrt(4 * pi)) .* exp(-x.^2 / 4);


s = integral(f1, a1, a2);
t = integral(@(x) (1 / sqrt(4 * pi)) .* exp(-x.^2 / 4), a1, a2);

if t ~= 0
px(i) = s / t;
else
px(i) = pl(i);

9
end
end

pl = px;
p = d;
end

b1 = 0.5 * (pl(1:end-1) + pl(2:end));


b2 = [-inf, b1, inf];

d = 0;
for i = 1:L
x1 = b2(i);
x2 = b2(i+1);

d = d + integral(@(x)((x - pl(i)).^2) .* (1 / sqrt(4 * pi)) .* exp(-


x.^2 / 4), x1, x2);
end

fprintf('Number of Iterations: %d',it);

Number of Iterations: 2

fprintf('Quantization Levels: ');

Quantization Levels:

disp(pl);

-2.9809 -1.6965 -0.5649 0.5649 1.6965 2.9809

fprintf('Distortion is: %.4f',d);

Distortion is: 0.1255

0.1255

QUE: 6

samples = [1.6475, 0.8865, 0.1062, 0.4972, -0.985, 2.3987, 0.0835, 2.5414,


0.3734, 1.2327, -2.0452, -0.9916, 1.7621, -0.9036, 0.8165, -0.5092, -0.1917,
-1.9083,-1.7967, 1.3924];
L = 6;
x_tilde = samples(1:L);

10
ep = 1e-6;
max_iterations = 1000;
iterat = 0;
distort_old = Inf;
while iterat < max_iterations
iterat = iterat + 1;
x_tilde_old = x_tilde;
x_k = zeros(1, L-1);
for k = 1:L-1
x_k(k) = (x_tilde(k) + x_tilde(k+1)) / 2;
end
for k = 1:L
if k == 1
lb = -Inf;
ub = x_k(1);
elseif k == L
lb= x_k(L-1);
ub = Inf;
else
lb = x_k(k-1);
ub = x_k(k);
end
indices = find(samples >= lb & samples < ub);
if ~isempty(indices)
x_tilde(k) = mean(samples(indices));
end
end
distortion = 0;
for k = 1:L
if k == 1
lb = -Inf;
ub= x_k(1);
elseif k == L
lb = x_k(L-1);
ub = Inf;
else
lb = x_k(k-1);
ub = x_k(k);
end
indices = find(samples >= lb & samples < ub);
distortion = distortion + sum((samples(indices) -
x_tilde(k)).^2);
end
distortion = distortion / length(samples);
if abs(distortion - distort_old) < ep
break;
end
distort_old = distortion;
end
disp(['K-means Converged after ', num2str(iteration), ' iterations.']);

11
K-means Converged after 6 iterations.

disp(['Optimal Quantization Levels: ', num2str(x_tilde)]);

Optimal Quantization Levels: -1.4384 -0.35045 0.09485 0.4353 1.082 2.0874

disp(['Distortion on 20 Samples: ', num2str(distortion)]);

Distortion on 20 Samples: 0.11485

% (c) Evaluate distortion and SNR on the PDF of Problem 5


mean_value = 0;
sigma = 2;
num_samples_pdf = 10000;
x_pdf = mean_value + sigma * randn(1, num_samples_pdf);
distortion_pdf = 0;
for k = 1:L
if k == 1
lb = -Inf;
ub= x_k(1);
elseif k == L
lb = x_k(L-1);
ub = Inf;
else
lb = x_k(k-1);
ub = x_k(k);
end
indices = find(x_pdf >= lb & x_pdf < ub);
distortion_pdf = distortion_pdf + sum((x_pdf(indices) -
x_tilde(k)).^2);
end
distortion_pdf = distortion_pdf / num_samples_pdf;
signal_power = sigma^2;
SNR = 10 * log10(signal_power / distortion_pdf);
disp(['Distortion on PDF: ', num2str(distortion_pdf)]);

Distortion on PDF: 0.8579

disp(['SNR: ', num2str(SNR), ' dB']);

SNR: 6.6863 dB

12

You might also like