% Limiter.m 

close all;
clear all;

%Create a sine wave with amplitude 
% reduced for half its duration

anzahl=220;
for n=1:anzahl,
   x(n)=0.2*sin(n/5);
end;
for n=anzahl+1:2*anzahl;
   x(n)=sin(n/5);
end;

% do Limiter

slope=1;
tresh=0.5;
rt=0.01;
at=0.4;

xd(1)=0; % Records Peaks in x
for n=2:2*anzahl;
   a=abs(x(n))-xd(n-1);
   if a<0, a=0; end;
   xd(n)=xd(n-1)*(1-rt)+at*a;
   if xd(n)>tresh,
      f(n)=10^(-slope*(log10(xd(n))-log10(tresh))); 
      % linear calculation of f=10^(-LS*(X-LT))
   else f(n)=1; 
   end;
      y(n)=x(n)*f(n);
end;


figure(1)
plot(x)
title('Input Signal x(n)');

figure(2)
plot(y)
title('Output Signal y(n)');


figure(3)
plot(f)
title('Gain Signal f(n)');

figure(4)
plot(xd)
title('Input Peak Signal xd(n)');



