matlab code about function of 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.

2 comentarios

Azzi Abdelmalek
Azzi Abdelmalek el 13 de Mzo. de 2013
What is y?
fdsa
fdsa el 13 de Mzo. de 2013
the signal, y = wavread('.wav');

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Mzo. de 2013

0 votos

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

4 comentarios

fdsa
fdsa el 13 de Mzo. de 2013
Great! I get the idea but...
(as to your example) why we should multiply by 2^2 (4) but not 10/100/1000...what I means is we want to round the number to what by multiplying 2^2 (4)...
Thank you very much for your response!!
Walter Roberson
Walter Roberson el 13 de Mzo. de 2013
You want to quantize to a certain number of bits. bits is binary. binary uses 2^n representation, not 10^n
fdsa
fdsa el 13 de Mzo. de 2013
Get it!!! One last problem: In the code, why we need the expression: -sign(y(i))*q/2 ? Am I missing any knowledge about quantization?
I wonder it just take the mid-value between two quantization levels but why the sign is "-" but not "+".....?
Thanks!!
fdsa
fdsa el 15 de Mzo. de 2013
Do not know why???
Thank you.

Iniciar sesión para comentar.

Más respuestas (3)

Azzi Abdelmalek
Azzi Abdelmalek el 13 de Mzo. de 2013
function [symbols_quant]=quantization(y)
b=12
q=2/(2^b-1)
symbols_quant=round(y/q)*q

3 comentarios

fdsa
fdsa el 13 de Mzo. de 2013
I've got this expression once... However what is the meaning of the division by q and then ...multiplication by q?
Thanks!
Example
y=10
q=0.3
% after quantization y should be 9.9
% round(y/q)=round(10/0.3)=round(33.33)=33
% round(y/q)*q=33*0.3=9.9
fdsa
fdsa el 13 de Mzo. de 2013
get it!! thz!!

Iniciar sesión para comentar.

Azzi Abdelmalek
Azzi Abdelmalek el 13 de Mzo. de 2013
Editada: Azzi Abdelmalek el 13 de Mzo. de 2013
One way how a real ADC works
% for values from 0 to 10
n=12
q=10/(2^n-1)
max_val=10/2
u=6.5555 % Example
bit=zeros(1,n)
for k=1:n
if u>max_val
bit(k)=1
u=u-max_val
end
max_val=max_val/2
end
disp(bit) % The bits that codify your input.
quantized_value=sum(bit.*2.^(n-1:-1:0)*q)

2 comentarios

fdsa
fdsa el 13 de Mzo. de 2013
I've tried my best to understand but... anyway, thanks for your help!!!
fdsa
fdsa el 15 de Mzo. de 2013
One more question: In the code, why we need the expression: -sign(y(i))*q/2 ? Am I missing any knowledge about quantization?
I wonder it just take the mid-value between two quantization levels but why the sign is "-" but not "+".....?
Thanks!!

Iniciar sesión para comentar.

fdsa
fdsa el 15 de Mzo. de 2013

0 votos

One more question: In the code, why we need the expression: -sign(y(i))*q/2 ? Am I missing any knowledge about quantization?
I wonder it just take the mid-value between two quantization levels but why the sign is "-" but not "+".....?
Thanks!!

Etiquetas

Preguntada:

el 13 de Mzo. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by