part 03:

Same as part 02, simulate a sound source moving on a straight line, allowing the user to specify a minimum distance that the observer is from the sound trajectory, but make the sound source a 3-tone signal.

code:

function [signal] = project_01_part03(f0, fs, secs, velocity, dist_from_track)

%comment out f0, fs, secs, velocity, and dist_from_track to specify values
%from command line
c = 340;                %speed of sound
velocity = 10;          %velocity of sound source
f1 = 466.16;            %basis of 3-tone signal
f2 = 523.25;            %basis of 3-tone signal
f3 = 783.99;            %basis of 3-tone signal
fs = 8000;              %sampling frequency
secs = 10;              %duration of sound
dist_from_track = 5;    %shortest distance from observer to track

time = -secs/2:1/fs:secs/2;
x = velocity*time;      %x-position of sound source on track

hypo = zeros(1,length(time));
%change in hypotenuse equals relative distance from sound source to observer
for i = 1:length(hypo)
    hypo(i) = sqrt((x(i))^2 + dist_from_track^2);
end

env = 1./hypo;
%velocity relative to observer is orig velocity vector * adj/hypo (cosine)
vel_rel_observer = velocity*x./hypo;

f1v = zeros(1,length(time));
f2v = zeros(1,length(time));
f3v = zeros(1,length(time));
for i = 1:length(time)
    f1v(i) = f1*(c/(c+vel_rel_observer(i)));
    f2v(i) = f2*(c/(c+vel_rel_observer(i)));
    f3v(i) = f3*(c/(c+vel_rel_observer(i)));
end

signal_1f = env.*sin(2*pi*f1v.*time);
signal_2f = env.*sin(2*pi*f2v.*time);
signal_3f = env.*sin(2*pi*f3v.*time);
signal = signal_1f+signal_2f+signal_3f;

subplot(321), plot(time,vel_rel_observer,'Color',[1,0.12,0.12]), axis([min(time),max(time),-1.5*velocity,1.5*velocity]), grid on;
title('Velocity of sound source'), xlabel('time (s)'), ylabel('velocity (m/s)');
subplot(323), plot(time,env,'g'), grid on;
title('Amplitude envelope'), xlabel('time (s)'), ylabel('amplitude');
subplot(325), plot(time,signal,'b'), grid on;
title('Doppler effect on signal'), xlabel('time (s)'), ylabel('amplitude');

subplot(322), plot(time,f1v,'Color',[0.98,0.68,0]), grid on;
title('Frequency shift (f1)'), xlabel('time (s)'), ylabel('frequency (Hz)');
subplot(324), plot(time,f2v,'Color',[0.10,0.48,0.01]), grid on;
title('Frequency shift (f2)'), xlabel('time (s)'), ylabel('frequency (Hz)');
subplot(326), plot(time,f3v,'Color',[0.53,0.81,0.98]), grid on;
title('Frequency shift (f3)'), xlabel('time (s)'), ylabel('frequency (Hz)');

signal = signal/max(abs(signal)); %normalizes input

%print project_01_part03 -dpng -r100;
%wavwrite(signal,fs,'project_01_part03');

soundsc(signal,fs);
    

graphs:

project_01_part03.png

files:

project_01_part03.m
project_01_part03.wav