📄 uquant.m
字号:
function xo=uquant(p1,p2,p3,p4);%UQUANT Simulate uniform quantization.% Usage: x=uquant(x,nbits);% x=uquant(x,nbits,xmax);% x=uquant(qtype,x,nbits,xmax);% x=uquant(qtype,x,nbits,xmax);%% UQUANT(x,nbits) simulates the effect of uniform quantization of x using% nbits bit. The output is simply x rounded to 2^{nbits} different values.%% UQUANT(x,nbits,xmax) does as above, but allows you to specify the% maximal value that should be quantifiable. If not specified, the% maximal value of the signal will be used.%% UQUANT(qtype,x,nbits) or UQUANT(qtype,x,nbits,xmax) uses quantization% depending on qtype. qtype may be one of:%% 's','signed' - Default quantization type. This assumes that the signal% has a both positive and negative part. Usefull for sound% signals.% 'u','unsigned' - Assumes the signal is positive. Negative values are% silently rounded to zero. Usefull for images.% %% If this function is applied to a complex signal, it will simply be% applied to the real and imaginary part separately.%% This program is free software: you can redistribute it and/or modify% it under the terms of the GNU General Public License as published by% the Free Software Foundation, either version 3 of the License, or% (at your option) any later version.% % This program is distributed in the hope that it will be useful,% but WITHOUT ANY WARRANTY; without even the implied warranty of% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the% GNU General Public License for more details.% % You should have received a copy of the GNU General Public License% along with this program. If not, see <http://www.gnu.org/licenses/>.error(nargchk(2,4,nargin));specified=0;if ischar(p1) qtype=p1; xi=p2; if nargin==2 error('Too few input parameters.'); end; nbits=p3; if nargin==4 specified=1; xmaxspecified=p4; end;else qtype='unsigned'; xi=p1; nbits=p2; if nargin==3 specified=1; xmaxspecified=p3; end; end;% ------ handle complex values ------------------if ~isreal(xi) if ~specified xo = uquant(qtype,real(xi),nbits) + i* uquant(qtype,imag(xi),nbits); else xo = uquant(qtype,real(xi),nbits,xmaxspecified) + ... i* uquant(qtype,imag(xi),nbits,xmaxspecified); end; returnend;if nbits<1 error('Must specify at least 2 bits.');end;% Calculate number of buckets.nbuck=2^nbits; switch(lower(qtype)) case {'u','unsigned'} % ------------ unsigned case ----------------- xmax=max(xi(:)); xmin=min(xi(:)); xminabs=abs(xmin); % Totally there is nbuck buckets to put all values in, % but since they are centered around zero, there will be nbuck/2 % buckets for strictly positive values, and nbuck/2-1 buckets for % strictly negative values. This means, that if the coefficient with % the largest absolute value is negative, then the bucket size % must be slightly larger to cope with it. % % The messy code below deals with this phenomena. if specified if xmaxspecified<max(xmax,xminabs) error('Signal contains values higher than xmax.'); end; bucksize=xmaxspecified/(nbuck/2-1); else posbuck=xmax/(nbuck/2); negbuck=xminabs/(nbuck/2-1); bucksize=max(posbuck,negbuck); end; xo=round(xi/bucksize)*bucksize; case {'s','signed'} % ------------- signed case------------ xmax=max(xi(:)); xmin=0; xminabs=abs(xmin); if specified if xmaxspecified<max(xmax,xminabs) error('Signal contains values higher than xmax.'); end; xmax=xmaxspecified; end; bucksize=xmax/(nbuck-.51); % Thresh all negative values to zero. xi=xi.*(xi>0); xo=round(xi/bucksize)*bucksize; otherwise error(['Unknown quantization type: ',qtype]);end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -