function y = uquant(x,delta,num_levels)
% Uniform scalar quantizer with step size = delta
% num_levels is the number of quantization output
% levels, and the output levels are mid-points
% of the quantization regions
if(num_levels < 1)
  error('num_levels must be positive integer')
end
if mod(num_levels,2) == 0
  s=1;
  if x<0
    s=-1;
  end
  i=floor(abs(x)/delta);
  i=min(i,(num_levels/2)-1);
  y=s*(i+0.5)*delta;
else
  i=round(x/delta);
  i=max(min(i,(num_levels-1)/2),-(num_levels-1)/2);
  y=i*delta;
end
