%% symclip.m

function y = symclip(x,threshold)

% "Overdrive" simulation with symmetrical clipping.
% Modified from the original symclip.m code [DAFXbook, 2nd ed., Chapter 4]

% x = input

N=length(x);
y=x;
t = threshold/500; 
th = 1/t; % threshold for symmetrical soft clipping 
        % by Schetzen Formula
        
for i=1:1:N,
   if abs(x(i))< th, y(i)=2*x(i);end;
   if abs(x(i))>=th, 
      if x(i)> 0, y(i)=(3-(2-x(i)*3).^2)/3; end;
      if x(i)< 0, y(i)=-(3-(2-abs(x(i))*3).^2)/3; end;
   end;   
   if abs(x(i))>2*th, 
      if x(i)> 0, y(i)=1;end;
      if x(i)< 0, y(i)=-1;end;
   end;
end;