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

[a,b] = size(IN);
ratio1 = 2/3;
diff = abs(Amax-Amin);

if bits == 1
  for j = 1:a,
    for k = 1:b,
	if abs(Amax-Amean) > abs(Amin-Amean)
	  if IN(j,k) == 0
		OUT(j,k) = Amin + (.5)*(ratio1)*abs(Amean-Amin);
	  else
		OUT(j,k) = Amean;
	  end
	else
	  if IN(j,k) == 1
		OUT(j,k) = Amax - (.5)*(ratio1)*abs(Amean - Amax);
	  else
		OUT(j,k) = Amean;
	  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) <= llevel
		OUT(j,k) = IN(j,k)*(lowmeanedge - Amin)*(1/llevel) + Amin;
	elseif IN(j,k) >= (llevel + 2)
		OUT(j,k) = (IN(j,k) - llevel - 2)*(Amax-highmeanedge)*(1/(2^bits - llevel - 3)) + highmeanedge;
	else
		OUT(j,k) = Amean;
	end
    end
  end
end





