-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlloydMaxQuantizer.m
More file actions
77 lines (70 loc) · 2.53 KB
/
lloydMaxQuantizer.m
File metadata and controls
77 lines (70 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function [xq, centers, D] = lloydMaxQuantizer(x, N, min_value, max_value)
% Calculate centers.
centers = [];
centers(1) = max_value - ((max_value - min_value) / 2^N)/2;
for i=2:2^N
centers(i) = centers(i-1) - (max_value - min_value) / 2^N;
end
centers = flip(centers);
centers = [min_value centers max_value];
%normalise input between min and max values
x = normalize(x, 'range', [min_value max_value]);
% distortion for every loop, begins with 0 and 1 for first loop
D = [0 1];
%while the difference of last and previous to last distortion is larger
%than eps. eps= 2.2204e-16 and is the smallest number matlab can do
%operations
k = 2;
while abs(D(k) - D(k-1)) >= eps
xq = [];
avgDistance = 0;
count = zeros(length(centers));
avgCount = zeros(length(centers));
% calculate the zones
zones = [];
zones(1) = min_value;
for i=2:(length(centers)-2)
zones(i) = (centers(i) + centers(i+1))/2;
end
zones(i+1) = max_value;
% Loop through the input
for i=1:length(x)
% Loop through each zone
for j=1:(length(zones)-1)
%if x(i) is between 2 zones
if zones(j) < x(i) && x(i) <= zones(j+1)
xq(i) = j;
% calculate average distance from center and for the next
% zones
avgDistance = avgDistance + abs(centers(j+1) - x(i));
avgCount(j) = avgCount(j) + x(i);
count(j) = count(j) + 1;
end
end
% for the cases where x(i) is the min or max value
if x(i) == zones(1)
xq(i) = 1;
avgDistance = avgDistance + abs(centers(2) - x(i));
avgCount(1) = avgCount(1) + x(i);
count(1) = count(1) + 1;
end
end
% new distortion into Distortion vector and next loop
avg_distortion = avgDistance/length(x);
D = [D avg_distortion];
k = k + 1;
% Calculate centers for the next zones
for j=2:(length(centers)-1)
if count(j-1) ~= 0
centers(j) = avgCount(j-1)/count(j-1);
end
end
end
% remove unnecessary elements and fix vectors D, centers and xq
D(1) = [];
D(2) = [];
centers(1) = [];
centers(length(centers)) = [];
xq = xq';
fprintf('Number of iterations: %d\n', k);
end