% Doppler Shift 
% Calculates the perceived frequency of a passing sound source
 
function[]= Ella_Doppler(va,ft)

clc
%==========================================================================
 
% Input data for relative velocity calculation
 
p  = 100;          % perpendicular distance to y-axis
% va = 300;        % actual velocity of travelling source
dt = 0.01;         % time increment
L  = 100;          % parrallel distance between source and receiver at point 0
 
%--------------------------------------------------------------------------
 
% Input Data for Doppler Shift calculation
 
% ft =  (100.0);   % transmitted frequency 
c  =  340;         % speed of sound in air
 
%--------------------------------------------------------------------------
 
% For Loop calculates all of the pitches according to the relative velocity
% at 5m intervals
 
n = 150;
d = zeros(1,n+1);
d(1) = L;
fr = zeros(1,n+1);
 
for i = 1:n; 
    d(i+1) = d(i) - va*dt;
    vr = (((d(i)^2+p^2)^0.5) - abs((d(i+1)^2 + p^2)^0.5))*1/dt; % relative velocity between source and receiver at discrete time intervals m/s
    fr(i) = ((2*vr*ft)/c) + ft;                                 % perceived frequency by receiver
end 
 
%--------------------------------------------------------------------------
 
% Generate time values from 0 to T seconds at the desired rate.
         
fs = 44100;
T  = 0.1;       % sample duration
t  = 0:(1/fs):T;
 
%--------------------------------------------------------------------------
 
% Generate and play Doppler Shift 

y = zeros(1,(4411*100)); 
c=1;

for k = 1:4411:4411*100;  
    f = fr(c);
    a = 0.8;
    y(1,k:k+4411-1) = a*sin(2*pi*f*t); 
    c=c+1;   
end

sound(y, fs);

audiowrite('perceived_doppler_shift.wav', y,fs);

end