function y = holtcomp(x, CT, CS, at, rt)
%HOLTCOMP is derived from M-file 4.2 from DAFX Second Edition
% original code by M. Holters
% edited by McLean Pierce

tav=0.01; % time averaging variable

% attack_time=0.03;
% release_time=0.003;
delay=150;

xrms=0;
g=1;
buffer=zeros(1,delay);

for n=1:length(x)
    xrms=(1-tav)*xrms+tav*x(n)^2;
    X=10*log10(xrms);
    G=min([0,CS*(CT-X)]); % sets the gain reduction to 0 or something smaller
    f=10^(G/20); % gain reduction factor
    if f < g
        coeff=at;
    else
        coeff=rt;
    end;
    
    
    g=(1-coeff)*g+coeff*f;
    y(n)=g*buffer(end);
    buffer=[x(n) buffer(1:end-1)];
    


end

