Lab3 202301445 CT
Lab3 202301445 CT
Honor Code:
I,Pransu Vadsmiya 202301445, 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
ANALYSIS PART
1.
s1=[1/2;1/4;1/8;1/16;1/32;1/32];
s2=[1/6;1/6;1/6;1/6;1/6;1/6];
H1 = 0;
for i=1:6
H1=H1-s1(i)*log(s1(i));
end
disp("Entory for the s1 is : ");
disp(H1);
H2 = 0;
for i=1:6
H2=H2-s2(i)*log(s2(i));
end
disp("Entory for the s2 is : ");
disp(H2);
2.
HUFFMAN CODING
load('discreteSources.mat');
n = 1e6;
key = {1,2,3,4,5,6};
value = {'0','10','110','1110','11110','11111'};
M = containers.Map(key,value);
str = strings(1,n);
for i =1:n
str(i) = M(S1(i));
end
count = sum(strlength(str));
d1 = count/n;
fprintf("M_enc / M = %0.4f\n",d1);
value = {'000','001','010','011','10','11'};
M = containers.Map(key,value);
str2 = strings(1,n);
for i = 1:n
str2(i) = M(S2(i));
end
count2 = sum(strlength(str2));
d2 = count2 / n;
fprintf("M_enc / M = %0.4f\n",d2);
n1 = H1 / d1;
n2 = H2 / d2;
fprintf("Efficiency of S1 is : %0.4f\n",n1);
fprintf("Efficiency of S2 is : %0.4f\n",n2);
ADHOC
load('discreteSources.mat');
n = 1e6;
key = {6,5,4,3,2,1}; %change order
value = {'0','10','110','1110','11110','11111'};
M = containers.Map(key,value);
str = strings(1,n);
for i =1:n
str(i) = M(S1(i));
end
count = sum(strlength(str));
d1 = count/n;
fprintf("M_enc / M = %0.4f\n",d1);
value = {'000','001','010','011','10','11'};
M = containers.Map(key,value);
str2 = strings(1,n);
for i = 1:n
str2(i) = M(S2(i));
end
count2 = sum(strlength(str2));
d2 = count2 / n;
fprintf("M_enc / M = %0.4f\n",d2);
n1 = H1 / d1;
n2 = H2 / d2;
fprintf("Efficiency of S1 is : %0.4f\n",n1);
fprintf("Efficiency of S2 is : %0.4f\n",n2);
3.
for i = 1:sInput
sym = [sym, input(i)];
sym1 = num2str(sym);
if (i == 1)
s = struct('prevPos', 0, 'currPos', lastPos);
lastPos = lastPos + 1;
dict(sym1) = s;
list = [0, input(i)];
sym = [];
elseif (i ~= sInput)
if (isKey(dict, sym1))
sym2 = sym1;
else
if (isKey(dict, sym2))
s = dict(sym2);
else
s = struct('prevPos', 0, 'currPos', 0);
end
s1 = struct('prevPos', s.currPos, 'currPos', lastPos);
lastPos = lastPos + 1;
dict(sym1) = s1;
list = [list; [s.currPos, input(i)]];
sym2 = "null";
sym = [];
end
else
if (isKey(dict, sym1))
s = dict(sym1);
s1 = struct('prevPos', s.currPos, 'currPos', lastPos);
lastPos = lastPos + 1;
dict(sym1) = s1;
list = [list; [s.currPos, Inf]];
sym2 = "null";
sym = [];
else
if (isKey(dict, sym2))
s = dict(sym2);
else
s = struct('prevPos', 0, 'currPos', 0);
end
s1 = struct('prevPos', s.currPos, 'currPos', lastPos);
lastPos = lastPos + 1;
dict(sym1) = s1;
list = [list; [s.currPos, input(i)]];
sym2 = "null";
sym = [];
end
end
end
end
% Lempel-Ziv Decoder function
function message = LempelZivDecoder(list)
message = [];
[length, ~] = size(list);
while cd ~= 0
if (list(cd, 2) ~= Inf)
sym = [list(cd, 2), sym];
end
cd = list(cd, 1);
end
a = [1; 0; 1; 1; 0; 1; 0; 1; 0; 0; 0; 1; 0];
disp("Original message a:");
disp(a');
b = LempelZivEncoder(a);
disp("Encoded message b:");
disp(b);
m = LempelZivDecoder(b);
disp("Decoded message m:");
disp(m');
s2 = transpose(s1);
disp("s1 transposed:");
disp(s2);
c = LempelZivEncoder(s2);
disp("Encoded s1 transposed:");
disp(c);
d = LempelZivDecoder(c);
disp("Decoded s1 transposed:");
disp(d);
for j = 1:length(barriers)-1
quant_pos(j) = (integral(f_weighted, barriers(j), barriers(j+1)) / ...
integral(pdf, barriers(j), barriers(j+1)));
end
% Update barriers
barriers = zeros(levels+1, 1);
barriers(1) = xmin;
barriers(levels+1) = xmax;
for k = 1:levels-1
barriers(k+1) = (quant_pos(k) + quant_pos(k+1))/2;
end
% distortion
error_total = 0;
for n = 1:levels
pos = quant_pos(n);
error_fn = @(x) ((x - pos).^2) .* pdf(x);
region_error = integral(error_fn, barriers(n), barriers(n+1));
error_total = error_total + region_error;
end
figure;
fplot(pdf, [xmin, xmax]);
hold on;
xline(quant_pos, '--b');
xline(barriers, 'color', 'green');
title(['Error: ', num2str(error_total)]);
legend('PDF', 'Levels', 'Boundaries');
hold off;
end
disp('Final quantization levels:');
disp(quant_pos);
.
.
.
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];
sort(samples);
L = 6;
centroids = samples(1:L);
closestCentroid = zeros(1, length(samples));
max_iter = 25;
% K-Mean Algorithm
for iter = 1:max_iter
% Step 1 : Compute distances of each sample from each centroid
dist = zeros(length(samples), L);
for i = 1:length(samples)
for j = 1:L
dist(i, j) = abs(samples(i) - centroids(j));
end
end
% Step 2 : Assign each sample to the nearest centroid
for i = 1:length(samples)
min_dist = inf;
closest_centroid_idx = 0;
for j = 1:L
if dist(i, j) < min_dist
min_dist = dist(i, j);
closest_centroid_idx = j;
end
end
closestCentroid(i) = closest_centroid_idx;
end
% Step 3 : Update each centroid as the mean of assigned points
new_centroids = zeros(1, L);
count = zeros(1, L);
for i = 1:length(samples)
cluster_idx = closestCentroid(i);
new_centroids(cluster_idx) = new_centroids(cluster_idx) + samples(i);
count(cluster_idx) = count(cluster_idx) + 1;
end
for i = 1:L
if count(i) > 0
new_centroids(i) = new_centroids(i) / count(i); % Compute mean
else
new_centroids(i) = centroids(i); % Keep previous centroid if no
points are assigned
end
end
centroids = new_centroids;
end
fprintf('Final Quantization Levels: %s\n',mat2str(centroids));
% b) Compute Distortion
distortion = 0;
for i = 1:length(samples)
quantized_value = centroids(closestCentroid(i));
distortion = distortion + (samples(i) - quantized_value)^2;
end
distortion = distortion / length(samples);
fprintf('Distortion: %.6f\n', distortion);
% (c) Compute SNR
signal_power = mean(samples.^2);
snr = 10 * log10(signal_power / distortion);
fprintf('SNR: %.2f dB\n', snr);