fs = 8000;
fo = 440;
f5 = fo*3;
f6 = fo*5;

t = -2+1/fs:1/fs:2;
xdistance = 10.*t; %distance on x axis is 0 at t=0
mindistance = 2; %minimum distance = 2 m
distance = ((xdistance.^2)+(mindistance^2)).^(1/2);
angle = asin(abs(xdistance)./distance);

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

fa = fo.*(340./(340 - vs.*(sin(angle)))); %fa = apparent frequency
f5a = f5.*(340./(340 - vs.*(sin(angle))));
f6a = f6.*(340./(340 - vs.*(sin(angle))));

signal = amplitude.*(cos(2*pi*fa.*t)+cos(2*pi*f5a.*t)+cos(2*pi*f6a.*t));
signal = 0.95*signal/max(signal);

soundsc(signal, fs);
wavwrite(signal,fs,'Project1_3');

subplot(2,2,1), plot(t, vs);
title('Source Velocity Relative to Observer');
xlabel('Time (seconds)');
ylabel('Velocity (meters/second)');

subplot(2,2,2), plot(t, signal);
title('Signal Envelope');
xlabel('Time (seconds)');
ylabel('Amplitude');

subplot(2,2,3), plot(t, fa);
title('Apparent Frequency');
xlabel('Time (seconds)');
ylabel('Frequency (Hertz)');