# Pyramid vector quantization

Pyramid vector quantization (PVQ) is a method used in audio and video codecs to quantize and transmit unit vectors, i.e. vectors whose magnitudes are known to the decoder but whose directions are unknown. PVQ may also be used as part of a gain/shape quantization scheme, whereby the magnitude and direction of a vector are quantized separately from each other. PVQ was initially described in 1986 in the paper "A Pyramid Vector Quantizer" by Thomas R. Fischer.
One caveat of PVQ is that it operates under the taxicab distance (L1-norm). Conversion to/from the more familiar Euclidean distance (L2-norm) is possible via vector projection, though results in a less uniform distribution of quantization points (the poles of the Euclidean n-sphere become denser than non-poles). No efficient algorithm for the ideal (i.e., uniform) vector quantization of the Euclidean n-sphere is known as of 2010. This non-uniformity can be reduced by applying deformation like coordinate-wise power before projection, reducing mean-squared quantization error by ~10%.
PVQ is used in the CELT audio codec (inherited into Opus) and the Daala video codec.

## Overview

As a form of vector quantization, PVQ defines a codebook of M quantization points, each of which is assigned an integer codeword from 0 to M−1. The goal of the encoder is to find the codeword of the closest vector, which the decoder must decode back into a vector.
The PVQ codebook consists of all N-dimensional points ${\vec {p}}$ with integer-only coordinates whose absolute values sum to a constant K (i.e. whose L1-norm equals K). In set-builder notation:
    $S(N,K)=\left\{{\vec {p}}\in \mathbb {Z} ^{N}:\left\|{\vec {p}}\right\|_{1}=K\right\}$
where $\left\|{\vec {p}}\right\|_{1}$ denotes the L1-norm of ${\vec {p}}$.
As it stands, the set S tesselates the surface of an N-dimensional pyramid. If desired, we may reshape it into a sphere by "projecting" the points onto the sphere, i.e. by normalizing them:
    $S_{\text{sphere}}(N,K)=\left\{{\frac {\vec {p}}{\left\|{\vec {p}}\right\|_{2}}}:{\vec {p}}\in S(N,K)\right\}$
where $\left\|{\vec {p}}\right\|_{2}$ denotes the L2-norm of ${\vec {p}}$.
Increasing the parameter K results in more quantization points, and hence typically yields a more "accurate" approximation of the original unit vector ${\vec {v}}$ at the cost of larger integer codewords that require more bits to transmit.

### Example

Suppose we wish to quantize three-dimensional unit vectors using the parameter K=2. Our codebook becomes:
| Codeword | Point | Normalized point |
| 0 | <−2, 0, 0> | <−1.000, 0.000, 0.000> |
| 1 | <−1, −1, 0> | <−0.707, −0.707, 0.000> |
| 2 | <−1, 0, −1> | <−0.707, 0.000, −0.707> |
| 3 | <−1, 0, 1> | <−0.707, 0.000, 0.707> |
| 4 | <−1, 1, 0> | <−0.707, 0.707, 0.000> |
| 5 | <0, −2, 0> | <0.000, −1.000, 0.000> |
| 6 | <0, −1, −1> | <0.000, −0.707, −0.707> |
| 7 | <0, −1, 1> | <0.000, −0.707, 0.707> |
| 8 | <0, 0, −2> | <0.000, 0.000, −1.000> |
| Codeword | Point | Normalized point |
| 9 | <0, 0, 2> | <0.000, 0.000, 1.000> |
| 10 | <0, 1, −1> | <0.000, 0.707, −0.707> |
| 11 | <0, 1, 1> | <0.000, 0.707, 0.707> |
| 12 | <0, 2, 0> | <0.000, 1.000, 0.000> |
| 13 | <1, −1, 0> | <0.707, −0.707, 0.000> |
| 14 | <1, 0, −1> | <0.707, 0.000, −0.707> |
| 15 | <1, 0, 1> | <0.707, 0.000, 0.707> |
| 16 | <1, 1, 0> | <0.707, 0.707, 0.000> |
| 17 | <2, 0, 0> | <1.000, 0.000, 0.000> |
(0.707 = ${\sqrt {2}}/2$ rounded to 3 decimal places.)
Now, suppose we wish to transmit the unit vector <0.592, −0.720, 0.362> (rounded here to 3 decimal places, for clarity). According to our codebook, the closest point we can pick is codeword 13 (<0.707, −0.707, 0.000>), located approximately 0.381 units away from our original point.
Increasing the parameter K results in a larger codebook, which typically increases the reconstruction accuracy. For example, based on the Python code below, K=5 (codebook size: 102) yields an error of only 0.097 units, and K=20 (codebook size: 1602) yields an error of only 0.042 units.

### Python code
## Complexity

The PVQ codebook can be searched in $O(KN)$. Encoding and decoding can likewise be performed in $O(KN)$ using $O(K+N)$ memory.
The codebook size obeys the recurrence
    $V(N,K)=V(N-1,K)+V(N,K-1)+V(N-1,K-1)$
with $V(N,0)=1$ for all $N\geq 0$ and $V(0,K)=0$ for all $K\neq 0$.
A closed-form solution is given by
    $V(N,K)=2N\cdot_{2}F_{1}(1-K,1-N;2;2).$
where $_{2}F_{1}$ is the hypergeometric function.