Quantization Codebook of Vector Quantization

This code is provided as supplementary material of the lecture Quellencodierung.

This code illustrates

  • quantization cells and Voronoi regions of 2d vector quantizers

In [1]:
%matplotlib inline
import numpy as np
from scipy.spatial import Voronoi, voronoi_plot_2d
import matplotlib.pyplot as plt

In [2]:
D2_points = np.array([[-3,3],[-1,3],[+1,3],[+3,3],[-2,2],[0,2],[2,2],[-3,1],[-1,1],[1,1],[3,1],[-2,0],[0,0],[2,0], [-3,-1],[-1,-1],[1,-1],[3,-1],[-2,-2],[0,-2],[2,-2],[-3,-3],[-1,-3],[1,-3],[3,-3]])

np.random.seed(8)
random_points = np.random.rand(len(D2_points),2)*6 - 3

vor_D2 = Voronoi(D2_points)
vor_ran = Voronoi(random_points)

In [3]:
font = {'size'   : 18}
plt.rc('font', **font)
plt.rc('text', usetex=True)

fig = plt.figure(figsize=(14, 7))
plt.subplot(1,2,1)
voronoi_plot_2d(vor_D2, show_vertices=False, show_points=False,line_colors='gray', ax=fig.gca())
plt.xlim((-3.03,3.03))
plt.ylim((-3.03,3.03))
plt.scatter(D2_points[:,0], D2_points[:,1], s=50)
plt.gca().set_aspect(aspect='equal')
plt.xlabel('$x_1/\Delta$')
plt.ylabel('$x_2/\Delta$')
plt.gca().set_xticks([-3,-2,-1,0,1,2,3])
plt.gca().set_yticks([-3,-2,-1,0,1,2,3])
plt.title('Uniform ($D_2$-lattice)');

plt.subplot(1,2,2)
voronoi_plot_2d(vor_ran, show_vertices=False, show_points=False,line_colors='gray', ax=fig.gca())
plt.xlim((-3.03,3.03))
plt.ylim((-3.03,3.03))
plt.scatter(random_points[:,0], random_points[:,1], s=50)
plt.gca().set_aspect(aspect='equal')
plt.xlabel('$x_1/\Delta$')
plt.ylabel('$x_2/\Delta$')
plt.gca().set_xticks([-3,-2,-1,0,1,2,3])
plt.gca().set_yticks([-3,-2,-1,0,1,2,3])
plt.title('Non-uniform')
plt.savefig('figure_DST_7.14.pdf',bbox_inches='tight')