/
bquantize.m
executable file
·42 lines (39 loc) · 1.23 KB
/
bquantize.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function y = bquantize(x,nsd,abstol,reltol)
%y = bquantize(x,nsd=3,abstol=eps,reltol=10*eps)
% Bidirectionally quantize a n by 1 vector x to nsd signed digits,
% Terminate early if the error is less than the specified tolerances.
% y is a structure array with the following fields:
% y(i).val is the quantized value in floating-point form
% y(i).csd is a 2-by-nsd (or less) matrix containing
% the powers of two (first row) and their signs (second row).
% See also bunquantize.m.
% Handle the input arguments
parameters = {'x' 'nsd' 'abstol' 'reltol'};
defaults = { NaN 3 eps 10*eps };
for i=1:length(defaults)
parameter = char(parameters(i));
if i>nargin | ( eval(['isnumeric(' parameter ') ']) & ...
eval(['any(isnan(' parameter ')) | isempty(' parameter ') ']) )
eval([parameter '=defaults{i};'])
end
end
n = length(x);
q = zeros(2*n,nsd);
y = struct('val',cell(1,n),'csd',cell(1,n));
offset = -log2(0.75);
for i = 1:n
xp = x(i);
y(i).val = 0;
for j = 1:nsd
error = abs(y(i).val-x(i));
if error <= abstol | error <= abs(x(i))*reltol
break;
end
p = floor( log2(abs(xp)) + offset );
p2 = pow2( p );
sx = sign(xp);
xp = xp- sx*p2;
y(i).val = y(i).val + sx*p2;
y(i).csd(1:2,j) = [p;sx];
end
end