对于我喜欢的前两行代码,
Fs = 10;
L = 10;
t = (0 : L - 1) / Fs;
x = 0.3 * cos(2 * pi * t);
其中Fs是采样频率、L样本数并t显示时间。
请注意,它x是频率为的正弦曲线,Fx = 1 Hz或者我们可以说它是周期性的Tx = 1 sec。
对于8-bit量化,我们有256级别。由于L / Fs = [10 sample] / [10 sample/sec] = 1 sec等于Tx(整个周期x),我们可以使用正样本。
mx = max(abs(x));
mx is defined because in order to use floor we need to scale the x.
q256 = mx*(1/128)*floor(128*(x/mx));
mx shows the maximum value for x so x / mx will take values over [-1 1] and 128*x/mx over [-128 128] will cover all 256 levels.
So we will quantize it with floor and scale it back (mx*1/128).
e256 = (1/L)*sum(abs(x-q256))
e256 simply shows the mean error over 10 samples.
Note that if L / Fs < Tx then this quantization won't be the optimum one.
Have in mind
The answer that you are given has some problems!
suppose x = [-1 -.2 0 .7 1]; and we want to quantize it with 2 bits.
mx = max(abs(x));
q4 = mx * (1/2) * floor(2*(x/mx));
Will give q4 = [-1 -0.5 0 0.5 1] which has 5 levels (instead of 2^2 = 4).
这可能不是什么大问题,你可以删除关卡x=1并拥有q4 = [-1 -0.5 0 0.5 0.5],但代码仍然需要一些改进,当然错误会增加。
一个简单的解决方案是添加
[~,ind] = max(x);
x(ind) = x(ind) - 1e-10;
在定义mxso 的最大值之后,x将被量化到低一级。
误差将增加到0.0012。