function OUT = quantize(IN, bits, Amin, Amax, Amean);

diff = abs(Amax - Amin);

ratio1 = 2/3;
[a,b] = size(IN);
if bits == 1,
  if abs(Amax-Amean) > abs(Amin-Amean)
	for j = 1:a,
	  for k = 1:b,
		if IN(j,k) <= (Amin + ratio1*abs(Amean-Amin))
		  OUT(j,k) = 0;
		else
		  OUT(j,k) = 1;
		end
	  end
	end
  else
	for j = 1:a,
	  for k = 1:b,
		if IN(j,k) >= (Amax - ratio1*abs(Amax-Amean))
		  OUT(j,k) = 1;
		else
		  OUT(j,k) = 0;
		end
	  end
	end
  end
else
  diffspace = diff/(2^bits);
  lowmeanedge = Amean - diffspace;
  highmeanedge = Amean + diffspace;
  if abs(Amean - Amin) < abs(Amax - Amean)
    llevel = 2^(bits - 1);
  else
    llevel = 2^(bits - 1) - 1;
  end
  for j = 1:a,
    for k = 1:b,
	if IN(j,k) < lowmeanedge
		OUT(j,k) = round((IN(j,k) - Amin)*llevel*(1/(lowmeanedge-Amin)));
	elseif IN(j,k) > highmeanedge
		OUT(j,k) = round((IN(j,k) - highmeanedge)*(2^bits - llevel - 3)*(1/(Amax-highmeanedge))) + llevel + 2;
	else
		OUT(j,k) = llevel + 1;
	end
    end
  end
end





