-
Notifications
You must be signed in to change notification settings - Fork 0
/
vqlbg.m
72 lines (63 loc) · 2.15 KB
/
vqlbg.m
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
function c = vqlbg(d, k)
% VQLBG Vector quantization using the Linde-Buzo-Gray algorithm
%
% Inputs:
% d contains training data vectors (one per column)
% k is number of centroids required
%
% Outputs:
% c contains the result VQ codebook (k columns, one for each centroids)
e = 0.01;
% dist_thresh = 0.1;
% dist_thresh = 3.1;
dist_thresh = 0.1;
q=1;
%computes the initial centroid of all the acoustic vectors
centr = mean(d,2);
codebook = centr;
while size(codebook,2)<k
split = zeros(size(d , 1) , size(codebook,2)*2);
for i = 1 : size(codebook,2)
%splits the codebook; for each codeword currently in the book, it splits
%it into two (doubling the size of the codebook)
% split(: , 2*i-1:2*i ) = [codebook(:,i)-e , codebook(:,i)+e];
split(: , 2*i-1:2*i ) = [codebook(:,i).*(1+e) , codebook(:,i).*(1-e)];
end
codebook = split;
convg = 10;
dist = 10;
while convg > dist_thresh
z = disteu(d , codebook);
%finds the closest codeword in codebook1 to each acoustic vector, where
%the vector output, j=ind(i) tells that acoustic vector i is closes to
%codework j
[m , ind] = min(z , [] , 2);
convg = abs(mean(m) - dist);
dist = mean(m);
indices = unique(ind);
%finds all the acoustic vectors associated with code word j, and thus
%are all part of the same cluster
codebook = zeros(size(d , 1) , size(indices,1));
for j = 1 : size(indices,1)
%seperates the clusters and find the new centroids for the updates
%the code book
clust = d(:, find(ind == indices(j) ));
codebook(: , j) = mean(clust,2);
% if avg_dist < dist_thresh
% % subplot(2,2,q)
% % % q=q+1;
% % plot(clust(1,:), clust(2,:),'o')
% % hold on
% % plot(codebook(1,j), codebook(2,j),'o','LineWidth',3)
% end
end
end
q;
q=q+1;
end
c = codebook;
% figure(q-1)
% plot(d(1,:),d(2,:),'o')
% hold on
% plot(c(1,:) , c(2,:), 'o','LineWidth',3)
end