# Quantization

Yao Wang Polytechnic University, Brooklyn, NY11201 http://eeweb.poly.edu/~yao

Outline
• Review the three process of A to D conversion • Quantization
– Uniform – Non-uniform
• Mu-law

– Demo on quantization of audio signals – Sample Matlab codes

• Binary encoding
– Bit rate of digital signals

EE3414:Quantization

2

Three Processes in A/D Conversion

Sampling

Quantization

Binary Encoding

xc(t)
Sampling Period
T

x[n] = xc(nT)
Quantization Interval Q

x[n]
Binary codebook

c[n]

Sampling: take samples at time nT
– T: sampling period; – fs = 1/T: sampling frequency

• •

Quantization: map amplitude values into a set of discrete values kQ
– Q: quantization interval or stepsize

Binary Encoding
– Convert each quantized value into a binary codeword

EE3414:Quantization

3

2 0.5 A2D_plot.Analog to Digital Conversion 1 T=0.4 0.25 0.5 0 -0.6 0.8 1 ©Yao Wang.m -1 0 0. 2006 EE3414:Quantization 4 .1 Q=0.

• One can trade off T (or fs) and Q (or R) – lower R -> higher fs. For music: fs >= 44 KHz.How to determine T and Q? • T (or fs) depends on the signal frequency range – A fast varying signal should be sampled more frequently! – Theoretically governed by the Nyquist sampling theorem • fs > 2 fm (fm is the maximum signal frequency) • For speech: fs >= 8 KHz. higher R -> lower fs • We considered sampling in last lecture. 2006 EE3414:Quantization 5 . we discuss quantization in this lecture ©Yao Wang. For music: R =16 bits. • Q depends on the dynamic range of the signal amplitude and perceptual sensitivity – Q and the signal range D determine bits/sample R • 2R=D/Q • For speech: R = 8 bits.

Uniform Quantization • Applicable when the signal is in a finite range (fmin. fmax) • The entire data range is divided into L equal intervals of length Q (known as quantization interval or quantization step-size) Q=(fmax-fmin)/L • Interval i is mapped to the middle value of this interval • We store/send only the index of quantized value f − f min  Index of quantized value = Qi ( f ) =  Q    Quantized value = Q ( f ) = Qi ( f )Q + Q / 2 + f min EE3414:Quantization 6 ©Yao Wang. 2006 .

Mid . mid-rise Q(f)=floor(f/q)*q+q/2 L = even. Q ( f ) = Qi ( f ) * Q + Q 2 ©Yao Wang.Riser f Q Qi ( f ) = floor ( ).Special Case I: Signal range is symmetric • (a) L=even. Mid . 2006 L = odd.Tread f Qi ( f ) = round ( ). Q( f ) = Qi ( f ) * Q Q 7 EE3414:Quantization .

2006 EE3414:Quantization 8 .Special Case II: Signal range starts at 0 f min = 0. q = f max / L f Qi ( f ) = floor ( ) Q Q( f ) = Qi ( f ) * Q + Q 2 ©Yao Wang. B = f max .

5) with 4 levels.Example • For the following sequence {1.5 Yellow dots indicate the partition levels (boundaries between separate quantization intervals) Red dots indicate the reconstruction levels (middle of each interval) 1.2 fall between 0.0.-0.125 • -1.-0.1.75 0 0.125 -0.2. 2006 EE3414:Quantization 9 .375 0.125.375. Quantizer is illustrated below.2. and hence is quantized to 1.75 1. Solution: Q=3/4=0. -1.125 • Quantized sequence: {1. Quantize it using a uniform quantizer in the range of (-1.-0.125.375. and write the quantized sequence.5.5 -0.375.3…}.375 1.-0.75.0.1.5.1.75 and 1.0.125} ©Yao Wang.89.5.1.4.

5 -1 -1.5 0 0.m Q=0.5 -1 -1.4 1.5 0 -0.25 1.5 1 0.5 0.2 demo_sampling_quant.4 ©Yao Wang.Effect of Quantization Stepsize Q=0.5 1 0.5 0 -0.2 0.5 0 0. 2006 EE3414:Quantization 10 .

016 2.01 Quantized Mozart_q16.008 2.005 -0.02 4 x 10 ©Yao Wang.018 2.Demo: Audio Quantization 0. 2006 EE3414:Quantization 11 .02 original at 16 bit quantized at 4 bit Original Mozart.004 2.wav 0.01 2 2.014 2.wav 0.005 0 -0.012 2.002 2.01 2.015 0.006 2.

012 2. 2006 EE3414:Quantization 12 .01 2 2.Demo: Audio Quantization (II) 0.002 2.01 0.004 2.01 2 2.006 2.005 -0.015 0.018 2.008 2.006 2.016 2.01 0.015 0.005 0.014 2.018 2.02 4 x 10 x 10 Quantized Mozart_q16.02 original at 16 bit quantized at 6 bit 0.02 original at 16 bit quantized at 4 bit 0.016 2.wav Quantized Mozart_q64.002 2.012 2.02 4 -0.01 2.008 2.01 2.005 -0.004 2.wav ©Yao Wang.014 2.005 0 0 -0.

2006 EE3414:Quantization 13 .Non-Uniform Quantization • Problems with uniform quantization – Only optimal for uniformly distributed signal – Real audio signals (speech and music) are more concentrated near zeros – Human ear is more sensitive to quantization errors at small values • Solution – Using non-uniform quantization • quantization interval is smaller near zero ©Yao Wang.

Quantization: General Description Quantization levels : L Partition values : bl Partition regions : Bl = [bl −1 . 2006 EE3414:Quantization 14 . bl ) Reconstruction values : g l Quantized Index : Qi ( f ) = l . if f ∈ Bl Quantizer value : Q( f ) = g l . if f ∈ Bl ©Yao Wang.

Function Representation Q ( f ) = gl . 2006 EE3414:Quantization 15 . if f ∈ Bl ©Yao Wang.

2006 EE3414:Quantization 16 .Design of Non-Uniform Quantizer • Directly design the partition and reconstruction levels • Non-linear mapping+uniform quantization µ-law quantization ©Yao Wang.

µ-Law Quantization y =F [ x ]  |x|  log 1+µ X max   . 2006 EE3414:Quantization 17 .sign[ x] = X max log[1 + µ ] ©Yao Wang.

2006 EE3414:Quantization 18 .sign[ x] log[1 + µ ] – Quantize the transformed value using a uniform quantizer: y->y^ – Transform the quantized value back using inverse µ-law: y^->x^ x =F −1[ y ] X = max µ 1+ µ ) y   log( 10 X max − 1 sign(y)     ©Yao Wang.Implementation of µ-Law Quantization (Direct Method) – Transform the signal using µ-law: x->y y =F [ x ]  |x|  log 1+µ  X max  = X max  .

then y also has the same range. – Applicable both for uniform and non-uniform quantizers – How to find the partition and reconstruction levels for mu-law quantizer • Apply inverse mu-law mapping to the partition and reconstruction levels of the uniform quantizer for y. 2006 EE3414:Quantization 19 . • Note that the mu-law formula is designed so that if x ranges from (-x_max.Implementation of µ-Law Quantization (Indirect Method) • Indirect Method: – Instead of applying the above computation to each sample. one can pre-design a quantization table (storing the partition and reconstruction levels) using the above procedure. x_max). The actual quantization process can then be done by a simple table look-up. ©Yao Wang.

we only need to find the inverse values for y=0. Quantize it using a mu-law quantizer in the range of (-1. ©Yao Wang.-0.4.75.125 µ=9. and write the quantized sequence.3…}. 0.125->0. 1.2.75->0.375.5) with 4 levels. x_max=1.7706 • – Then quantize each sample using the above partition and reconstruction levels.5. 0. Solution (indirect method): – apply the inverse formula to the partition and reconstruction levels found for the previous uniform quantizer example.1297. 2006 EE3414:Quantization 20 .-0.3604.1.2.0.0. Because the mu-law mapping is symmetric.375->0.1.1.5.0.Example • For the following sequence {1.5.89.

77 -0.-0.13 0.75 1.77 -1.77.5 -0.5 Inverse µ-law x =F −1[ y ] X = max µ 1+ µ ) y  log(  X 10 max − 1 sign(y)     -0.36 1.75 0 0.4.89.Example (cntd) -1.13 0.-0.-0.1.77.2.0.5.13.77} ©Yao Wang.125 -1.3…} • Quantized sequence – {0.375 0.0.5 • Original sequence: {1.125 -0.0.0.2.0.36 0 0. 2006 EE3414:Quantization 21 .375 1.-0.5 -0.77.77.

5 -1 -1.5 0 -0.5 1.4 µ-law: Q=0. µ-Law Quantization Uniform: Q=0. but large values are represented more coarsely.Uniform vs. ©Yao Wang.µ=16 With µ-law.5 -1 -1.5 1 0. 2006 EE3414:Quantization 22 .5 1 0.2 0. small values are represented more accurately.5 0 -0.5 0 0.5 0 0.5.4 1.2 0.

wav 0 0 Mozart_q32_m16.02 q32µ4 0.Uniform vs.01 2.01 0 Mozart_q64.01 0 Mozart_q32. µ-Law for Audio 0.01 Mozart_q32_m4.02 x 10 4 0.01 0.01 2.wav -0.01 2 2.wav 0.02 q32 0.01 2 2.01 2.01 2 2.wav -0.01 2.02 q64 0.02 x 10 4 -0.02 x 10 4 .02 q32µ16 0.01 2 2.02 x 10 4 -0.

Evaluation of Quantizer Performance • Ideally we want to measure the performance by how close is the quantized sound to the original sound to our ears -.Perceptual Quality • But it is very hard to come up with a objective measure that correlates very well with the perceptual quality • Frequently used objective measure – mean square error (MSE) between original and quantized samples or signal to noise ratio (SNR) 2 MSE : σ q = 1 N ˆ (n)) ∑ ( x ( n) − x n 2 2 2 SNR(dB) : SNR = 10 log10 σ x /σ q ( ) where N is the number of samples in the sequence. 1 2 σx is the variance of the original signal. 2006 EE3414:Quantization 24 . σ z2 = ∑ ( x(n)) 2 N n ©Yao Wang.

2006 EE3414:Quantization 25 .m” ©Yao Wang. “quant_mulaw.Sample Matlab Code Go through “quant_uniform.m”.

101.110.100.Binary Encoding • Convert each quantized level index into a codeword consisting of binary bits • Ex: natural binary encoding for 8 levels: – 000.001.011.111 • More sophisticated encoding (variable length coding) – Assign a short codeword to a more frequent symbol to reduce average bit rate – To be covered later ©Yao Wang.010. 2006 EE3414:Quantization 26 .

10.75 0 0. 10.125 -1. 00.0. 11.125.125} Bitstream representing quantized sequence: {11.0.89.2. 2006 EE3414:Quantization 27 .-0.1.2.375.5 -0.-0.Example 1: uniform quantizer • • • For the following sequence {1.375.1. Quantize it using a uniform quantizer in the range of (-1.375.-0. 11} ©Yao Wang. Codewords: 4 levels can be represented by 2 bits.125 01 -0. Solution: Q=3/4=0. and write the quantized sequence and the corresponding binary bitstream.75 1. 01.75. 11 codewords Quantized values 00 -1. 01.375 10 0.125.5.3…}.5) with 4 levels.5.1. Quantizer is illustrated below. 01.0.375 11 1.1.5 • • Quantized value sequence: {1.4.-0.

75 1.2.2.77 01 -0.0.75 0 0.11.-0.36 0 0.13 10 0.1.00.77.5 Inverse µ-law codewords 00 -0.-0.77.5 • Original sequence: {1.36 1.77.125 -1.11.125 01 -0.13.Example 2: mu-law quantizer codewords 00 -1.0.375 11 1.01.0.5.77 x =F −1[ y ] X = max µ 1+ µ ) y  log(  X 10 max − 1 sign(y)     -1.89.375 10 0.-0.5 -0.77.11} ©Yao Wang.4.-0. 2006 EE3414:Quantization 28 .5 -0.13 11 0.77} • Bitstream: {11.3…} • Quantized sequence: {0.0.0.

B=[log2(L)] Bit rate: R=f_s B bit/sec Ex: speech signal sampled at 8 KHz. – To be covered later. – To be covered later. quantized to 16 bit/sample. ©Yao Wang. R=44*16=704 Kbps • Ex: stereo music with each channel at 704 Kbps: R=2*704=1.4 Mbps • Required bandwidth for transmitting a digital signal depends on the modulation technique. 2006 EE3414:Quantization 29 .Bit Rate of a Digital Sequence • • • • Sampling rate: f_s sample/sec Quantization resolution: B bit/sample. • Data rate of a multimedia signal can be reduced significantly through lossy compression w/o affecting the perceptual quality. quantized to 8 bit/sample. R=8*8 = 64 Kbps • Ex: music signal sampled at 44 KHz.

2006 EE3414:Quantization 30 .5 ˆ= x 1 if x ≥ 0.5 -0.Advantages of Digital Representation (I) More immune to noise added in channel and/or storage The receiver applies a threshold to the received signal: 0 if x < 0.5 original signal received signal 1 0.5 0 1.5 0 10 20 30 40 50 60 ©Yao Wang.

any odd number of bit errors). By computing check-sum after receiving the signal. ©Yao Wang. Internet. one can detect single errors (in fact. DVDs. 2006 EE3414:Quantization 31 . corrupted bits can be detected and corrected – Ex: adding a “check-sum” to the end of a digital sequence (“0” if sum=even. wireless phones.Advantages of Digital Representation (II) • Can correct erroneous bits and/or recover missing bits using “forward error correction” (FEC) technique – By adding “parity bits” after information bits. “1” if sum=odd). – Used in CDs. etc.

and can perform mu-law quantization • Can perform uniform and mu-law quantization on a given sequence.What Should You Know • Understand the general concept of quantization • Can perform uniform quantization on a given signal • Understand the principle of non-uniform quantization. 2006 EE3414:Quantization 32 . generate the resulting quantized sequence and its binary representation • Can calculate bit rate given sampling rate and quantization levels • Know advantages of digital representation • Understand sample matlab codes for performing quantization (uniform and mu-law) ©Yao Wang.

1. 2006 EE3414:Quantization 33 .References • Y. ©Yao Wang. Wang. Experiment on Speech and Audio Compression. 1-2. Lab Manual for Multimedia Lab. Sec. (copies provided).