Lab 3
Lab 3
• I have not copied the work (Matlab code, results, etc.) that someone else has done
• 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
H1 = -sum(P1 .* log2(P1));
H2 = -sum(P2 .* log2(P2));
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
QUE:2
function sourceCodingAnalysis()
% Load Data
load('discreteSources.mat');
M = 10^6;
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);
2
[adhocS1, adhocCodesS1] = adhocEnc(S1);
[adhocS2, adhocCodesS2] = adhocEnc(S2);
mencAdhocS1 = length(adhocS1);
mencAdhocS2 = length(adhocS2);
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 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();
(d) Efficiency:
S1 (Menc): 0.4271, S2 (Menc): 0.7747
S1 (R): 0.8859, S2 (R): 0.8617
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
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
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
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();
%%QUE4
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
Decoded 1: 1 2 1 3
8
Encoded 2: 0.09472
Decoded 2: 1 1 2 2 3
%%QUE:5
L = 6;
st = -3;
en = 3;
for it = 1:mx_it
d = 0;
for i = 1:L
x1 = b2(i);
x2 = b2(i+1);
if abs(d - p) < t
break;
end
px = zeros(size(pl));
for i = 1:L
a1 = b2(i);
a2 = b2(i+1);
if t ~= 0
px(i) = s / t;
else
px(i) = pl(i);
9
end
end
pl = px;
p = d;
end
d = 0;
for i = 1:L
x1 = b2(i);
x2 = b2(i+1);
Number of Iterations: 2
Quantization Levels:
disp(pl);
0.1255
QUE: 6
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.
SNR: 6.6863 dB
12