MATLAB: Matlab code about function of quantization

quantization

I have been given a quantization function (matlab code) but I'm confused in some places, pls help….(PS: y is a signal(-1 to 1) and 12 bits is used to quantize)
===============================================================================
function [symbols_quant]=quantization(y)
b=12;
q=2/(2^b);
for i =1: length(y)
symbols_quant(i)= max(min((round(y(i) * 2^(b-1))/(2^(b-1)))-sign(y(i))*q/2 , 1) , -1);
==============================================================================
Question 1: why we should multiply the sample,y(i), with by 2^(b-1) , and then round it to get the quantized value? But not directly round it…What is the principle behind?
Thank you very much if you can give me a hand.

Best Answer

Consider a number such as 3/8. If you round it directly you will get 0; multiply that by 2^2 (4) and you would still get 0. But if you multiply 3/8 by 2^2 (4) and then round you will get round(3/2) = 2. So clearly multiplying before rounding makes a difference.
Keep in mind the only rounding operation available is to round to integer. There is no function to round to nearest 1/4 or 1/10(for example.)
The principle is the same you would use for rounding to a certain number of decimal places. For example, to round 3.141 to 1 decimal place, multiply it by 10 to get 31.41, round to get 31, divide again by 10 to get 3.1