Communications Toolbox | ![]() ![]() |
Quantizing a Signal
The previous section described how you can represent the partition and codebook that determine your scalar quantization process. This section shows how to use these parameters in the quantiz
function.
The code below shows how the quantiz
function uses partition
and codebook
to map a real vector, samp
, to a new vector, quantized
, whose entries are either -1, 0.5, 2, or 3.
partition = [0,1,3]; codebook = [-1, 0.5, 2, 3]; samp = [-2.4, -1, -.2, 0, .2, 1, 1.2, 1.9, 2, 2.9, 3, 3.5, 5]; [index,quantized] = quantiz(samp,partition,codebook); quantized quantized = Columns 1 through 6 -1.0000 -1.0000 -1.0000 -1.0000 0.5000 0.5000 Columns 7 through 12 2.0000 2.0000 2.0000 2.0000 2.0000 3.0000 Column 13 3.0000
This example illustrates the nature of scalar quantization more clearly. After quantizing a sampled sine wave, it plots the original and quantized signals. The plot contrasts the x
's that make up the sine curve with the dots that make up the quantized signal. The vertical coordinate of each dot is a value in the vector codebook
.
t
=[0:.1:2*pi]; % Times at which to sample the sine function
sig
=sin(t); % Original signal, a sine wave
partition
=[-1:.2:1]; % Length 11, to represent 12 intervals
codebook
=[-1.2:.2:1]; % Length 12, one entry for each interval
[index,quants]
=quantiz(sig,partition,codebook); % Quantize.
plot(t,sig,'x',t,quants,'.')
axis([-.2 7 -1.2 1.2])
Determining Which Interval Each Input Is In
The quantiz
function also returns a vector that tells which interval each input is in. For example, the output below says that the input entries lie within the intervals labeled 0, 6, and 5, respectively. Here, the 0th interval consists of real numbers less than or equal to 3; the 6th interval consists of real numbers greater than 8 but less than or equal to 9; and the 5th interval consists of real numbers greater than 7 but less than or equal to 8.
If you continue this example by defining a codebook vector such as
then the equation below relates the vector index
to the quantized signal quants
.
This formula for quants
is exactly what the quantiz
function uses if you instead phrase the example more concisely as below.
partition = [3,4,5,6,7,8,9];
codebook = [3,3,4,5,6,7,8,9];
[index,quants] = quantiz([2 9 8],partition,codebook);
![]() | Representing Quantization Parameters | Optimizing Quantization Parameters | ![]() |