% Generating stimuli
% Using amplitude-modulated stimuli as in Trahiotis and
% Stern (1994)

w0 = 2*pi*f0;
wm = 2*pi*fm;

% Generating component amplitudes after auditory filtering

Ax = hm(As,f0,fc);
modenvp = 1+modindex*cos(wm*t);
modenvm = 1-modindex*cos(wm*t);
modenvx = zeros(3,numt-1);

if stimset == 1
	modsign = [1 1 1];  % value 1 => +; 0 => -
	modenvx = modsign'*modenvp + (1-modsign)'*modenvm;

end
if stimset ==2
 	modsign = [0 1 0];
	modenvx = modsign'*modenvp + (1-modsign)'*modenvm;
end
	
% subplot(3,1,1), plot(t,modenvx(1,:))
% subplot(3,1,2), plot(t,modenvx(2,:))
% subplot(3,1,3), plot(t,modenvx(3,:))
	
xl = Ax*(modenvx.*(cos(w0'*t)));
xr = Ax*(modenvx.*(cos(w0'*(t-ts))));

% [Begin half-square rect]
% Half-square law rectifier:

% rl = ((g/RMSpl)*max(xl,0)).^2;  % OLD FORMULA
% rr = ((g/RMSpr)*max(xr,0)).^2;  % OLD FORMULA

% 	The following is the current power law rect:
% rl = max(xl,0).^2;	% Power 3.25 gives best least-square fit of CC functions
% rr = max(xr,0).^2;
% [End half-square rect]



% [Begin exponential rect]
% Exponential rect from old program:
% Normalizing 
xl = xl/max(abs(xl));
xr = xr/max(abs(xr));

rl = exp(g*xl);
rr = exp(g*xr);

% [End exponential rect]

al = 200/mean(rl);
ar = 200/mean(rr);

rl = al*rl;
rr = ar*rr;

% subplot(2,1,1), plot(t,rl)
% subplot(2,1,1), plot(t,xl)


% Computing first-level coincidence response

for m = 1:numtau,
for n = 1:numt-1,

rrLttau(n,m) = rr(indexrr(n,m));
rlLttau(n,m) = rl(indexrl(n,m));

end
end
