Lloyd Max Quantizer (MATLAB Program)
Lloyd Max Quantizer (MATLAB Program)
n = 2;
%number of bits
g = 1;
while n <= 64
%main loop
COURSE: EE320
A = zeros(n, 10000);
q = zeros(1, n);
%counter to tell where the last non zero entry is in every row of A
L = -10:(20/n):10;
v = zeros(1, n);
plotter = 1:100;
%////////CODE FOR ARRANGING THE SAMPLES OF THE SIGNAL INTO SUITABLE SUBDIVISIONS////////
for major=1:100
q = zeros(1,n);
for i=1:10000
for p=1:n
%pth subdivision
if ((L(p)<=X(i))&&((X(i)<L(p+1))))
subdivision
q(p)=q(p)+1;
A(p, q(p))=X(i);
else
end
end
end
%////////CODE FOR CALCULATING THE QUANTIZATION LEVELS AND THE MEAN SQUARED ERROR////////
MSE(major)=0;
for p=1:n
%pth subdivision
if (q(p)==0)
v(p)=(L(p)+L(p+1))/2; %assume the level for that group as the average of the L and U limits
else
tempsum=0;
for z=1:q(p)
%optimal pth level is the av. of all samples in the pth sub division
for z=1:q(p)
MSE(major) = MSE(major) + ((v(p)-A(p, z))*(v(p)-A(p, z)));
end
end
end
MSE(major) = (MSE(major))/10000;
for y=2:n
L(y) = (v(y-1)+v(y))/2;
end
end
sat(g) = MSE(100);
hold on
plot (plotter, MSE)
xlabel('Number of Iterations');
ylabel('Mean Squared Error');
title('MSE v/s Number of Iterations');
end
g = g+1;
n = (2*n);