我正在尝试使用 128 级均匀量化器量化一组双类型样本,并且我希望我的输出也是双类型。当我尝试使用“量化”时,matlab 出现错误:不支持“双”类的输入。我也试过“uencode”,但它的答案是胡说八道。我对matlab很陌生,我已经为此工作了几个小时。任何帮助appriciated。谢谢
问问题
1552 次
1 回答
0
uencode应该给出整数结果。这就是它的重点。但关键是它假设一个对称范围。从 -x 到 +x,其中 x 是数据集中的最大值或最小值。因此,如果您的数据是从 0 到 10,那么您的结果看起来像是无稽之谈,因为它量化了 -10 到 10 范围内的值。
无论如何,您实际上需要编码值和量化值。我写了一个简单的函数来做到这一点。它甚至几乎没有帮助说明(实际上只需键入“帮助 ValueQuantizer”)。我还让它非常灵活,所以它可以处理任何数据大小(假设你有足够的内存)它可以是向量、2d 数组、3d、4d ......等
这是一个例子,看看它是如何工作的。我们的数字是从 -0.5 到 3.5 的均匀分布,这表明与 uencode 不同,我的函数适用于非对称数据,并且适用于负值
a = 4*rand(2,4,2) - .5
[encoded_vals, quant_values] = ValueQuantizer(a, 3)
生产
a(:,:,1) =
0.6041 2.1204 -0.0240 3.3390
2.2188 0.1504 1.4935 0.8615
a(:,:,2) =
1.8411 2.5051 1.5238 3.0636
0.3952 0.5204 2.2963 3.3372
encoded_vals(:,:,1) =
1 4 0 7
5 0 3 2
encoded_vals(:,:,2) =
4 5 3 6
1 1 5 7
quant_values(:,:,1) =
0.4564 1.8977 -0.0240 3.3390
2.3781 -0.0240 1.4173 0.9368
quant_values(:,:,2) =
1.8977 2.3781 1.4173 2.8585
0.4564 0.4564 2.3781 3.3390
所以你可以看到它以整数形式返回编码值(就像 uencode 但没有奇怪的对称假设)。与 uencode 不同,这只是将所有内容作为双精度返回,而不是转换为 uint8/16/32。重要的部分是它还返回量化值,这是你想要的
这是功能
function [encoded_vals, quant_values] = ValueQuantizer(U, N)
% ValueQuantizer uniformly quantizes and encodes the input into N-bits
% it then returns the unsigned integer encoded values and the actual
% quantized values
%
% encoded_vals = ValueQuantizer(U,N) uniformly quantizes and encodes data
% in U. The output range is integer values in the range [0 2^N-1]
%
% [encoded_vals, quant_values] = ValueQuantizer(U, N) uniformly quantizes
% and encodes data in U. encoded_vals range is integer values [0 2^N-1]
% quant_values shows the original data U converted to the quantized level
% representing the number
if (N<2)
disp('N is out of range. N must be > 2')
return;
end
quant_values = double(U(:));
max_val = max(quant_values);
min_val = min(quant_values);
%quantizes the data
quanta_size = (max_val-min_val) / (2^N -1);
quant_values = (quant_values-min_val) ./ quanta_size;
%reshapes the data
quant_values = reshape(quant_values, size(U));
encoded_vals = round(quant_values);
%returns the original numbers in their new quantized form
quant_values = (encoded_vals .* quanta_size) + min_val;
end
据我所知,这应该总是有效,但我还没有进行广泛的测试,祝你好运
于 2015-03-27T22:04:59.110 回答