function [signal] = project_01_part01(f0, fs, secs, velocity)
%comment out f0, fs, secs, and velocity to specify values from command line c = 340; %speed of sound velocity = 10; %velocity of sound source f0 = 440; %center frequency of sound fs = 8000; %sampling frequency secs = 10; %duration of sound
time = -secs/2:1/fs:secs/2; dist = velocity*time; env = 1./abs(dist);
%since 1/x has vertical asymptotes, limit envelope y-values to 1 for i = 1:length(env) if env(i) > 1 env(i) = 1; end end
v = zeros(1,length(time)); for i = 1:length(v) if dist(i) < 0 v(i) = -velocity; else v(i) = velocity; end end
f = zeros(1,length(time)); for i = 1:length(time) f(i) = f0*(c/(c+v(i))); end
signal = env.*sin(2*pi*f.*time);
subplot(411), plot(time,v,'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(412), plot(time,f,'g'), grid on; title('Frequency shift'), xlabel('time (s)'), ylabel('frequency (Hz)'); subplot(413), plot(time,env,'b'), grid on; title('Amplitude envelope'), xlabel('time (s)'), ylabel('amplitude'); subplot(414), plot(time,signal,'Color',[0.98,.68,0]), grid on; title('Doppler effect on signal'), xlabel('time (s)'), ylabel('amplitude');
signal = signal/max(abs(signal)); %normalizes input
%print project_01_part01 -dpng -r100; %wavwrite(signal,fs,'project_01_part01');
soundsc(signal,fs); |