doppler effect - calculating the speed of a

Upload: renato-coelho

Post on 10-Oct-2015

83 views

Category:

Documents


0 download

TRANSCRIPT

  • Slides

    Example: 2.1Doppler Effect - Calculating the Speed of a

    Passing Train by Fourier Analysis of a SoundFile

    A NumFys Example

    http://www.numfys.net

    Fall 2012

    A NumFys Example www.numfys.net

    Example 2.1: Doppler Effect - The Speed of a Passing Train

  • Slides

    p.1 The Doppler EffectYou are likely familiar with the Doppler effect. You might haveexperienced it as a change in pitch of a car horn, ambulance siren ortrain whistle as the car/ambulance/train moved past you. In scientificterms, this change in pitch is described as a shift in the frequency ofthe sound.

    The equation for the observed sound frequency f , as seen by areceiver moving at a speed vr relative to the air, is

    f =c + vrc + vs

    f0, (1)

    where the sound is originally emitted at a frequency f0 by a sourcethat moves at a speed vs relative to the air. Here, c is the speed ofsound.

    We focus on motion along a straight line where we define vr aspositive when the receiver moves toward the source and vs aspositive when the source is moving away from the observer.

    A NumFys Example www.numfys.net

    Example 2.1: Doppler Effect - The Speed of a Passing Train

  • Slides

    p.2 The Doppler EffectA classical application of (1) is the calculation of the speed of a trainmoving past a stationary receiver. Let the observed frequency of thetrain whistle as the train moves towards the receiver be f1 and theobserved frequency as the train moves away from the receiver be f2 .

    In this case, we can use (1) to obtain expressions for f1 and f2:

    f1 =c

    c v f0, (2)

    f2 =c

    c + vf0, (3)

    where v is now the speed of the train. We use these two equations toeliminate the unknown f0 and solve the resulting equation for v

    v =f1 f2f1 + f2

    c. (4)

    A NumFys Example www.numfys.net

    Example 2.1: Doppler Effect - The Speed of a Passing Train

  • Slides

    p.3 The Problem

    We can now determine the speed of a train v , using (4). However,there will be a slight twist. We will analyse numerically experimentaldata in the form of a sound recording of a passing train to compute f1and f2. Subsequently, this will yield v .

    To do this, we will use MATLABs implementation of a powerful toolknown as the fast Fourier transform (FFT). Essentially, this is nothingbut a computationally efficient way of calculating the discrete Fouriertransform (DFT), a discrete approximation of the continuous Fouriertransform.

    A NumFys Example www.numfys.net

    Example 2.1: Doppler Effect - The Speed of a Passing Train

  • Slides

    p.4 The Discrete Fourier TransformA sound file is represented in MATLAB as a vector ~x with N elements,where each element is the sound amplitude sampled at time intervalst . The DFT of ~x is then also a vector with N elements. We call it ~X .

    Suppose now that we know ~X . Then we can compute each elementxn in ~x by applying the formula

    xn =1N

    N1k=0

    Xkei2pik

    Nt nt (5)

    Let us look closer at this expression and try to figure out what itmeans. What it tells us is quite simply that ~x is a superposition ofexponential functions with different frequencies fk = kNt andamplitudes Xk . Therefore, we can view the magnitude of amplitudes|Xk |2 as a measure of the "weight of the frequency fk " in ~x !

    A NumFys Example www.numfys.net

    Example 2.1: Doppler Effect - The Speed of a Passing Train

  • Slides

    p.5 The Discrete Fourier TransformQ: How do we calculate ~X to begin with?A: We could apply the formula for the discrete Fourier transform,

    Xk =N1n=0

    xnei2pik

    Nt nt . (6)

    This requires O(N2) operations. In contrast, the FFT is a morecomputationally efficient way to calculate ~X , requiring only O(N lnN)operations. There are several FFT algorithms and many make use ofthe fact that the exponentials can all be written as(

    e2piiN

    )kn. (7)

    In MATLAB, you can calculate ~x from ~X by using x=ifft(X), or theother way round, using X=fft(x).

    A NumFys Example www.numfys.net

    Example 2.1: Doppler Effect - The Speed of a Passing Train

  • Slides

    p.6 Solving the Problem using MATLABAssume now that we store the sound of the train as it moves towardsthe observer, in the vector sample1. Likewise, we store the sound ofthe train as it moves away from the observer, in sample2.

    We calculate the FFTs of these signals and store them in the vectorsp1 and p2 respectively.

    p1=fft(sample1);p2=fft(sample2);

    To obtain a measure of the magnitude of the amplitudes, we calculatetheir absolute values squared, element by element:

    P1=p1.*conj(p1);P2=p2.*conj(p2);

    Finally, we calculate the frequency corresponding to each of theelements in P1 and P2.

    f=linspace(0,N-1,N)./(N*dt);

    A NumFys Example www.numfys.net

    Example 2.1: Doppler Effect - The Speed of a Passing Train

  • Slides

    p.7 Solving the Problem using MATLAB

    All the details discussed on the previous slide are implemented in theMATLAB script fftwrapper.m. A few more technical details arerequired about how to import the audio file etc. but this will not bediscussed here. However, you are encouraged to look at the code infftwrapper.m and see if you can make sense of it.

    The main thing you need to know for now is that fftwrapper.m canbe called as a function with an argument that can be true or false.This depends on whether you want MATLAB to play the sound fileand the samples sample1 and sample2 or not. The return valuesare P1, P2 and f.

    A NumFys Example www.numfys.net

    Example 2.1: Doppler Effect - The Speed of a Passing Train

  • Slides

    p.8 Solving the Problem using MATLAB

    We start our solution script doppler.m by a call to fftwrapper().

    [P1,P2,f]=fftwrapper(true);

    Then we plot P1 and P2, normalised such that the biggest elementsin the plotted vectors are 1.

    plot(f,P1./max(P1),f,P2./max(P2));xlabel(f (Hz));ylabel(P/P_{max})legend(Sample 1,Sample 2,Location,NorthWest);

    A NumFys Example www.numfys.net

    Example 2.1: Doppler Effect - The Speed of a Passing Train

  • Slides

    p.9 Solving the Problem using MATLABThe produced plot looks like this.

    0 100 200 300 400 500 600 7000

    0.2

    0.4

    0.6

    0.8

    1

    f (Hz)

    P/P m

    ax

    Sample 1Sample 2

    The two samples contain frequency peaks. The peaks in sample2are shifted towards smaller frequencies in relation to the peaks insample1. This is consistent with what we hear when a train passesby: the sound it makes as it moves away form us is more low-pitchedthan that of when it moves towards us.

    A NumFys Example www.numfys.net

    Example 2.1: Doppler Effect - The Speed of a Passing Train

  • Slides

    p.10 Solving the Problem using MATLAB

    We choose the frequency f1 corresponding to the tallest peak insample1 in the plot on the previous page. We see that thecorresponding peak in sample2 is also the tallest and we denote itsfrequency by f2. We locate f1 and f2 as follows.

    f1=f(P1==max(P1));f2=f(P2==max(P2));

    Notice that we could not expect in advance that the tallest peak insample1 would also be the tallest in sample2. In principle, this is nota given and we needed the plot on the previous page to confirm this.

    A NumFys Example www.numfys.net

    Example 2.1: Doppler Effect - The Speed of a Passing Train

  • Slides

    p.11 Solving the Problem using MATLAB

    Having found f1 and f2, we can calculate v by use of (4). We definethe speed of sound c to be 340.29 m/s and do the following:

    c=340.29; % Speed of sound [m/s]v=(f1-f2)/(f1+f2)*c; % Speed of train [m/s]v=v*3.6; % Speed of of train [km/h]

    This calculation gives the speed of the train as 38.77 km/h.

    Download the scripts fftwrapper.m and doppler.m along with thesound file lwrhuntrd-ns197.wav1 into the same directory. Then,run the script on your own computer. Listen closely to the sound file.Does is it sound reasonable that the train is moving at 38.77 km/h?

    1Courtesy of David Safdy and Greg Lavoie of fwarailfan.netA NumFys Example www.numfys.net

    Example 2.1: Doppler Effect - The Speed of a Passing Train

    Slides