在Matlab中量化数据

时间:2014-09-07 20:32:20

标签: matlab

说我在Matlab中有一些情节如下:

x = linspace(0,10,10000);
input= sin(x);

我想将数据量化为一定数量的位。 (我从技术上认识到MATLAB量化了它的所有图形。)我已经尝试了以下内容:

bits = 7;
output =floor(2^bits*input)/2^bits

但这仅在输入介于0和1之间时才有效。我该怎么办?

1 个答案:

答案 0 :(得分:3)

方法#1 bsxfun基于"插值 - "计划 -

x = linspace(min(input),max(input),2^bits) %// Setup the quantizied levels 
                                         %// ranging from min to max of the input data
[~,ind1] = min(abs(bsxfun(@minus,input,x.'))) %//' Find the indices of the 
                                              %// levels nearest to the input data
output = x(ind1) %// Get the quantized values

另外,尽量不要使用与内置MATLAB函数名相同的变量名,在这种情况下为input


方法#2 interp1基于

x = linspace(min(input),max(input),2^bits) %// Setup the quantizied levels 
                                         %// ranging from min to max of the input data
output = interp1(x,x,input,'nearest') %// Get quantized values with 1-D interpolation
                                      %// to the nearest quantized levels

示例 -

input [Input data] =
    0.8017    1.0533   -0.7489   -0.9363   -1.2691    0.4980    2.7891
bits [No. of bits used for quantization ] =
     2
x [These are 2^bits quantized levels ranging from min to max of input] =
   -1.2691    0.0836    1.4364    2.7891
output [Input data is brought to the nearest quantized levels taken from x] =
    1.4364    1.4364   -1.2691   -1.2691   -1.2691    0.0836    2.7891