在 MATLAB 中将双精度类型输入量化为双精度类型输出

Quantizing Double Type Input to Double Type Output in MATLAB

我正在尝试使用 128 级均匀量化器量化一组双精度型样本,我希望我的输出也是双精度型。当我尝试使用 "quantize" 时,matlab 出现错误:不支持 class 'double' 的输入。我也试过 "uencode" 但它的回答是胡说八道。我对 matlab 很陌生,我已经为此工作了几个小时。任何帮助都适用。谢谢

uencode 应该给出整数结果。这就是重点。但关键是它假定一个对称范围。从 -x 到 +x,其中 x 是数据集中的最大值或最小值。因此,如果您的数据是从 0 到 10,您的结果看起来像胡说八道,因为它量化了 -10 到 10 范围内的值。

无论如何,您实际上需要编码值和量化值。我写了一个简单的函数来做到这一点。它甚至几乎没有帮助说明(实际上只是键入 "help 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

所以你可以看到它 returns 编码值作为整数(就像 uencode 但没有奇怪的对称假设)。与 uencode 不同的是,这只是 returns 将所有内容加倍而不是转换为 uint8/16/32。重要的是它也是 returns 量化值,这就是你想要的

这里是函数

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

据我所知这应该总是有效的,但我还没有进行广泛的测试,祝你好运