% VQ codebook training - random initialization 
% training data are in Aspeech matrix

L = 8; 

% initialize - random selection of L vectors 
[dummy,Nfr] = size(Aspeech); 
initvecs = randperm(Nfr);    % random permutation of all indices
initvecs = initvecs(1:L);    % take only L of them
cb = Aspeech(:,initvecs);    % and take only these vectors. 

% - first code here -
oldgd=9999999;
[sym, gd]=vq_code (Aspeech, cb);
disp (['orig: ' num2str(gd)]);
iter=1;

while ((oldgd-gd)/gd > 0.005)
  % - make new code-vectors
  [cb, nbc]=vq_clust (Aspeech,sym,L);
  
  % - and code - 
  oldgd=gd;
  [sym, gd]=vq_code (Aspeech, cb);
  disp (['iter: ' num2str(iter) ': ' num2str(gd) ]);
  iter=iter+1;
end
