fs = 8000;
fo = 440;
t = -2+1/fs:1/fs:2;
xdistance = 10.*t;     %distance on x axis is 0 at t=0
Rmindistance = 2;            %minimum distance from left ear = 2 m
Rdistance = ((xdistance.^2)+(Rmindistance^2)).^(1/2);%dist from R ear
Rangle = asin(abs(xdistance)./Rdistance);              %angle from R ear
 
amplitude = abs(1./xdistance);
 
for i= 1:32000
    if amplitude(i) > 1;   %amplitude approaches infinity
        amplitude(i) = 1;  %set infinite amplitudes to max amplitude
    end
end
 
vs = ones(1,32000);
for j= 1:1:32000
    if t(j) < 0
        vs(j) = 10;     %vs positive when moving toward observer
    else
        vs(j) = -10;    %vs negative when moving away from observer
    end
end
 
Rfa = fo.*(340./(340 - vs.*(sin(Rangle))));  %Rfa = apparent frequency R ear
Rsignal = amplitude.*cos(2*pi*Rfa.*t);
Rsignal = 0.95*Rsignal/max(Rsignal);

Lmindistance = 2.17;    %Left ear is 0.17 meters further than right
Ldistance = ((xdistance.^2)+(Lmindistance^2)).^(1/2);%dist from L ear
Langle = asin(abs(xdistance)./Ldistance);           %angle from L ear
Lfa = fo.*(340./(340 - vs.*(sin(Langle))));  %Lfa = apparent frequency L ear
Lsignal = amplitude.*cos(2*pi*Lfa.*t);
Lsignal = 0.95*Lsignal/max(Lsignal); 

signal = [Lsignal; Rsignal];
signal = signal.';
soundsc(signal, fs);
wavwrite(signal,fs,'Project1_4-2');
 

 subplot(2,2,1), plot(t, Rsignal);
title('Right Ear Signal Envelope');
xlabel('Time (seconds)');
ylabel('Amplitude');

subplot(2,2,2), plot(t, Lsignal);
title('Left Ear Signal Envelope');
xlabel('Time (seconds)');
ylabel('Amplitude');
 
subplot(2,2,3), plot(t, Rfa);
title('Right Ear Apparent Frequency');
xlabel('Time (seconds)');
ylabel('Frequency (Hertz)');

subplot(2,2,4), plot(t, Lfa);
title('Left Ear Apparent Frequency');
xlabel('Time (seconds)');
ylabel('Frequency (Hertz)');

