%% newdoppler.m   
% written by Vishal Paul Joy
%[resultwav] = newdoppler(inputsig);
% 28th May 2013
% This program accepts one audio wave and uses a pitch marker function to
% obtain some frequencies.These frequencies are stored in an array and
% shifted according to doppler principle. 
% The following program is not a doppler processor but a doppler
% synthesizer.
%you do not have to use a wavread while giving the input wav. you can use
%just mention the file name in between ''.
                        %example newdoppler('anechoic_voice.wav');
%See Also: WAVREAD and SOUND
function [resultwav] = newdoppler(inputsig)
% written by Vishal Paul Joy
%[resultwav] = newdoppler(inputsig);
% 28th May 2013

% This program accepts one audio wave and uses a pitch marker function to
% obtain some frequencies.These frequencies are stored in an array and
% shifted according to doppler principle. 
% The following program is not a doppler processor but a doppler
% synthesizer.
%you do not have to use a wavread while giving the input wav. you can use
%just mention the file name in between ''.
                        %example newdoppler('anechoic_voice.wav');
              
%See Also: WAVREAD and SOUND
%% 
[inputsignal fs]=wavread(inputsig);
%fs- sampling rate



rec=pitchmarker(inputsignal);   %Callinfg the pitchmarker function.The input signal is sent to the pitch marker function in order to obtain 
                                %an array of frequencies.
%% obtaining the values                                 
answer=input('do you want to use the default set values (y/n) ','s'); % an option for the user to input valu
if (answer== 'y' || answer== 'Y') % user can use the default set of values 
    
lengthrec=length(rec);
wavcom=1;
c = 340;						% speed of sound in m/s
					
A = 1;							% source amplitude
T = 10;							% total time in seconds
l = 100;						% path length in meters
Vs = l/T;						% speed of source - m/s
y = 5;							% distance from observer to sound path in metets
t = -T/2:1/fs:T/2;			    % array of time
x = t .* Vs;					% array of source position
d = sqrt(x.^2 + y.^2);			% array of source distance in meters
else 
    A=input('enter source amplitude');          % user defined source amplitude
    T=input('enter total time in seconds');     % user defined time in seconds 
    l=input('enter path length in meters');     % user defined length in meters
    Vs=l/T;                                     % speed of source - m/s
    y=input('enter the distance from observer to sound path in meters'); %user efined distance 
    c = input('enter the speed of sound in m/s');						% user defined speed of sound in m/s
    t = -T/2:1/fs:T/2;					% array of time
    x = t .* Vs;			            % array of source position
    d = sqrt(x.^2 + y.^2);				% array of source distance in meters
end

%%    
for i=1:lengthrec               % For loop for doppler shift.Each frequency is individually shifted accordingly using a loop.
f0 = rec(i);					% frequency to be shifted
ln=length(t);                   % findin the length of the array t.           
Va=linspace(-Vs, Vs, ln);       % linspace defines velocity a perticular instatnt compared to the observer.It is an the array of apparent velocity
d(d<1) = 1;						% prevent divide by zero
env = 1./d;						% array of amplitude envelope
Fa = f0.*((c+Va)./c);			% array of apparent frequency in hz
Wa = 2*pi*Fa;					% array of apparent frequncy in rad/s


wav = env.*cos( Wa.* t);					% array of waveform samples


wavcom=wavcom + wav;            % adding various waveforms together.

end
%%  graph plot 
hold off;
subplot(1,1,1); plot(t, env); title('Apparent Amplitude of Source');
xlabel('time (s)'); ylabel('amplitude');
saveas(gcf,'dopplershift.jpg');	
%% normalising and wave write 
ma = max(abs(wavcom));
wavcom = wavcom./ma;
wavcom=wavcom.*0.01;   
soundsc(wavcom,fs);  
wavwrite(wavcom, fs, 'out_doppler.wav'); % saving the file as an audio wav file.
%% additional effects reverb and wah wah effects 
wavco=wavcom';
answer2=input('Do you want to add reverberation to the sound file (y/n) ? ','s');
answer3=input('Do you want to add wah wah function to the sound file (y/n) ','s');
if((answer2== 'y' || answer2=='Y')&&(answer3 =='y' || answer3 =='Y'))
    delayedwave=delay_ (wavco,fs);  % calling the delay_function in order to get the effect revberation added to the wav file.
                                    % The result from the function is
                                    % stored in the delayedwave variable.
    soundsc(delayedwave,fs);        % play back of audio with reveberation effect.
   resultwav= wah_wah(delayedwave,fs);        % calling the wah_wah function in order to get the effect wah wah added to the wav file.
                                    
else
    if  (answer2== 'y' || answer2=='Y')
        delayedwave=delay_ (wavco,fs);  % calling the delay_function in order to get the effect revberation added to the wav file.
                                        % The result from the function is
                                        % stored in the delayedwave variable.
        resultwav=delayedwave;
        soundsc(delayedwave,fs);        % play back of audio with reveberation effect.
    else
    
        if (answer3 =='y' || answer3 =='Y')
           resultwav= wah_wah(wavcom,fs);         % calling the wah_wah function in order to get the effect wah wah added to the wav file
        
        else
           resultwav=wavco;
        soundsc(wavco,fs);              % play back of audio 
        end
    end
end


