查看文档,这是一个非常简单的函数,在 Python 中很容易编写。我重命名了函数以添加缺少的“e”,因为这让我很烦。反正:
def quantize(signal, partitions, codebook):
indices = []
quanta = []
for datum in signal:
index = 0
while index < len(partitions) and datum > partitions[index]:
index += 1
indices.append(index)
quanta.append(codebook[index])
return indices, quanta
尝试使用文档中的示例:
>>> index, quants = quantize([3, 34, 84, 40, 23], range(10, 90, 10), range(10, 100, 10))
>>> index
[0, 3, 8, 3, 2]
>>> quants
[10, 40, 90, 40, 30]
对于更高效但不太灵活的版本,我们可以绕过范围并仅使用数学:
from __future__ import division
import math
def opt_quantize(signal, num_quanta, partition_start, partition_step,
codebook_start, codebook_step):
indices = []
quanta = []
for datum in signal:
index = int(math.floor((datum - partition_start) / partition_step + 1))
if index < 0:
index = 0
if index >= num_quanta:
index = num_quanta - 1
indices.append(index)
quanta.append(codebook_start + codebook_step * index)
return indices, quanta
尝试使用文档中的示例:
>>> index, quants = opt_quantize([3, 34, 84, 40, 23], 9, 10, 10, 10, 10)
>>> index
[0, 3, 8, 4, 2]
>>> quants
[10, 40, 90, 50, 30]
因此,在由于浮点错误导致数据恰好位于分区上的情况下,结果略有不同,但如果分区上没有任何内容,则它可以工作。
这样可以减少运行时间,其中 n 是信号的长度,m 是从 O(mn) 到 O(n) 的分区数。这应该会给您带来显着的性能提升。我们能做得更好吗?
是的。使用我们新的基于数学的方法,代码很容易矢量化,我们可以让 Numpy 完成艰苦的工作:
import numpy as np
def np_quantize(signal, num_quanta, partition_start, partition_step,
codebook_start, codebook_step):
signal = np.asarray(signal, dtype=float)
indices = np.empty_like(signal, dtype=int)
np.floor_divide((signal - partition_start + partition_step), \
partition_step, indices)
np.clip(indices, 0, num_quanta - 1, indices)
quanta = np.asarray(indices, dtype=float) * codebook_step + codebook_start
return indices, quanta
我偶然地对它进行了基准测试,而且似乎我的每一个优化都让它变慢了,所以要么我做错了可怕的事情,要么我没有测试足够大的数据来摊销常数。
~$ python -m timeit -s 'from quantize import orig_quantize' 'orig_quantize([-3, -2, -1, 0, 1, 2, 3], [-0.5, 0.5], [-1, 0, 1])'
100000 loops, best of 3: 8.58 usec per loop
~$ python -m timeit -s 'from quantize import opt_quantize' 'opt_quantize([-3, -2, -1, 0, 1, 2, 3], 3, -0.5, 1, -1, 1)'
100000 loops, best of 3: 10.8 usec per loop
~$ python -m timeit -s 'from quantize import np_quantize' 'np_quantize([-3, -2, -1, 0, 1, 2, 3], 3, -0.5, 1, -1, 1)'
10000 loops, best of 3: 57.4 usec per loop
对于踢球,我尝试使用Cython和 Numpy:
cimport cython
cimport numpy as np
cdef extern from "math.h":
float floorf(float)
@cython.boundscheck(False)
def cynp_quantize(np.ndarray[float, ndim=1] signal, int num_quanta,
float partition_start, float partition_step,
float codebook_start, float codebook_step):
cdef int i
cdef int index
cdef np.ndarray[np.int_t, ndim=1] indices = np.empty_like(signal, dtype=int)
cdef np.ndarray[float, ndim=1] quanta = np.empty_like(signal)
for i in range(signal.shape[0]):
index = <int>floorf((signal[i] - partition_start)
/ partition_step + 1.0)
if index < 0:
index = 0
if index >= num_quanta:
index = num_quanta - 1
indices[i] = index
quanta[i] = codebook_start + index * codebook_step
return indices, quanta
据我所知,Cython 还实验性地支持 OpenMP,这可以让它用多个线程完成所有事情。不过,无论有没有线程,我都无法测试这个 Cython 解决方案的性能(我缺少编译结果所需的头文件)。