%quantize a given sequence using x using vector quantizer with codebook y
x=[0.63, -0.60, 0.55, -1.10, 0.09, -2.00, -0.49, 0.46, -0.32, 2.24, -0.63, ...
-2.32, -1.23, 1.05, -0.11, 0.38, 0.94, -2.12, -0.64, -0.70]
b=[1:2];
y=[-0.41,0.03, 0.5,-1.45, -0.67,1.58, -0.30,-1.63];
res=0;
Num=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Computing error for each input block of length 2
for i=0:2:length(x)-2
disp('Current input block');
block=x(i+b);
disp(block);
k=1;
D=zeros(1,4);
for j=0:2:length(y)-2
appr=y(j+b);
D(k)=(block-appr)*(block-appr)';
k=k+1;
end;
disp('Errors');
disp(D);
%%%%%%%Finding the index of approximating block and value of minimum error
[M,Ind]=min(D);
disp('Index and value of the minimal error');
disp(Ind);
disp(M);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Num=[Num Ind];
res=res+M;
end;
disp('Quantized sequence');
disp(Num);
disp('Relative error');
disp(res/(x*x'));