quant.m

来自「英文书《Digital Signal Processing with Examp」· M 代码 · 共 26 行

M
26
字号
function [y,scl,shft]=quant(x,n)
% [y,scl,shft]=quantize(x,n)
%
% Inputs:    x =vector with real elements.
%            n =number of bits per quantized element of x.
%
% Outputs:   y =quantized x with n-bit integer elements.
%               The elements of y range from 0 to 2^n-1.
%          scl =scaling factor used to approximate x from y.
%         shft =shifting factor used to approximate x from y.
%               x may be approximated by (y-shft)*scl.
if n<1 | n>32,
    error('Number of bits (n) is out of range.');
end

y=x;                        %initially, y=x
x=row_vec(x);               %make x a row vector
if range(x)==0,
    return                  %if range(x)=0, y=x (no change)
end
scl=range(x)/(2^n-1);
x=x/scl;
%scale x to range 2^n-1.
shft=-min(x);
x=x+shft;                   %shift so min(x)=0.
y(:)=round(x);              %convert x to integers in y.

⌨️ 快捷键说明

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