bquantize.m

来自「高阶sigma-delta调制器设计matlab工具包, 半波带滤波器设计工具」· M 代码 · 共 43 行

M
43
字号
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 argumentsparameters = {'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};'])    endendn = 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 <= 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];     endend

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?