Go to content Go to navigation Go to search

xeno
form

Category

Search


Links

Scalar Quantization in MATLAB · 2006-12-21

Scalar quantization, as Wikipedia puts it,

“is the process of approximating a continuous range of values (or a very large set of possible discrete values) by a relatively-small set of discrete symbols or integer values.”

Generally, the process involves mapping from an interval of numbers in the range to a single number in the domain. Because it is a process so heavily used in signal compression, it is useful to have a function to perform it at high speeds in MATLAB. The Communications Toolbox includes a quantiz function, which maps a vector of floats to a vector of integers given another vector of ranges, but being implemented as an m-file, it is slow.

However, the base toolbox (the one that has all of the general MATLAB tools), has the histc function. histc, given a data vector and a vector of bin edges, counts the number data elements in each bin. The lesser known second output of histc actually returns the bin number for each data element, the process of quantization.

For example, suppose we wanted to perform 3-bit uniform quantization for the numbers between 0 and 1, i.e., given a data vector x and evenly distributed bins between 0 and 1, we wanted to map each data value to an integer 0 through 7.

 >> x = rand(1, 100);
 >> e = linspace(0, 1, 8);
 >> [n, idx] = histc(x, e);

The previous snippet yields each element of x mapped to a number 1 through 8 in idx (we may subtract 1 to get 3-bit numbers 0 through 7).

To approximate x using the bin edges (codebook) and quantization indices, we may:

 >> y = e(idx);

If we had subtracted 1 from the idx, we would have to add it back in at this stage. The same procedure holds for any non-overlapping, ordered, quantization partitions, e.g., exponential, Lloyd-Max. For large data-sets, and reasonable size codebooks, the space savings are significant as is the performance of histc over quantiz.