📄 thresh.m
字号:
function [xo,N]=thresh(ttype,xi,lambda,mtype);%THRESH Threshold (hard/soft)% Usage: x=thresh(ttype,x,lambda);% [x,N]=thresh(ttype,x,lambda);% x=thresh(ttype,x,lambda,mtype);% [x,N]=thresh(ttype,x,lambda,mtype);%% THRESH('hard',x,lambda) or THRESH(1,x,lambda) will perform% hard thresholding on x, i.e. all element with absolute value% less than lambda will be set to zero.%% THRESH('soft',x,lambda) or THRESH(2,x,lambda) will perform% soft thresholding on x, i.e. lambda will be subtracted from% the absolute value of every element of x.%% [x,N]=THRESH(ttype,x,lambda) additionally returns a number N specifying% how many numbers where kept.%% THRESH(ttype,x,lambda,'full') returns the output as a full matrix. This% is the default.%% THRESH(ttype,x,lambda,'sparse') returns the output as a sparse matrix.%% The function WTHRESH in the Matlab Wavelet toolbox implements the same% functionality.%% 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(3,4,nargin));if (prod(size(lambda))~=1 || ~isnumeric(lambda)) error('lambda must be a scalar.');end;if ischar(ttype) ttype=lower(ttype);end;dosparse=0;if nargin==4 switch(lower(mtype)) case {'full'} case {'sparse'} if ndims(xi)>2 error('Sparse output is only supported for 1D/2D input. This is a limitation of Matlab/Octave.'); end; dosparse=1; otherwise error('The output type (last argument) must be either "full" or "sparse".'); end;end;signifmap=find(abs(xi)>=lambda);if dosparse xo=sparse(size(xi,1),size(xi,2));else xo=zeros(size(xi));end;switch ttype case {'hard',1} xo(signifmap)=xi(signifmap); case {'soft',2} xo(signifmap)=xi(signifmap) - sign(signifmap)*lambda; otherwise error('Unknown thresholding type.');end;if nargout==2 N=sum(signifmap(:));end;% Another way to do hard threshholding.% xo=xi.*(abs(xi)>=lambda);% Another way to do soft threshholding%xa=abs(xi)-lambda;% In the following line, the +0 is significant: It turns% -0 into +0, oh! the joy of numerics.%xo=((xa>0).*xa+0).*sign(xi);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -