function W = huber(res, scale, param)
% function W = huber(res, scale, param)
%
% computes Huber's weight function for robust regression:
% min( 1, param/(|res|/scale) )
% 
% arguments:
%		res:	vector of residuals
%		scale:	robust estimate of scale, such as MAD
%		param:	parameter of the Huber function; the
%			default value is 2.5.
%
% returns:
%		W:	the vector of Huber weights
%
%
% P.B. Stark  stark@stat.berkeley.edu
% 9 July 1997.

p = 2.5;		% default parameter
if (nargin == 3),
	if (param > 0),
		p = param;
	else
		error('parameter must be positive')
	end
end

unit = ones(size(res));
res(res == 0 ) = p*scale*unit(res==0);
W = min(unit, scale*p*(abs(res)).^(-1));
return;