%Función Reflexiones Tempranas (earlyreflect)
%---------------------------------
%y salida,  x entrada, ed=Early Reflection Delays, eg=Early Reflection Gain, s=size
function y=earlyreflect(x,fs,s,ed,eg)
%Head Delay: Retardo de la funcion de transferencia de la cabeza
hd=int32(0.0008*fs); 
% Early delay calculado para el algoritmo
ED_trans=[ed(6);ed(6)-ed(1:5)];
ED=int32(ED_trans*fs);
ER=zeros(s+ED(1),1);
SER1=zeros(s+ED(1),1);
SER2=zeros(s+ED(1),1);
y=zeros(s,2);
for n=1:s
  ER(n+ED(1))=0.91*x(n);
  SER1(n)=eg(1)*ER(n+ED(2))+eg(3)*ER(n+ED(4))+eg(5)*ER(n+ED(6)); %Sum of Early Reflections EG1, EG3, EG5
  SER2(n)=eg(2)*ER(n+ED(3))+eg(4)*ER(n+ED(5))+eg(6)*ER(n); %Sum of Early Reflections EG2, EG4, EG6
end

SER1A=zeros(s,1); SER1A((hd+1):end)=SER1(1:end-hd-ED(1));
SER2A=zeros(s,1); SER2A((hd+1):end)=SER2(1:end-hd-ED(1));

SER1AF=lowpass(SER1A,s,exp(-(1/fs)*2*pi*2000));
SER2AF=lowpass(SER2A,s,exp(-(1/fs)*2*pi*2000));

y(:,1)=SER1AF + SER2(1:end-ED(1));  
y(:,2)=SER2AF + SER1(1:end-ED(1));