You are on page 1of 12

Slides

Example: 2.1
Doppler Effect - Calculating the Speed of a
Passing Train by Fourier Analysis of a Sound
File
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 Effect
You are likely familiar with the Doppler effect. You might have
experienced it as a change in pitch of a car horn, ambulance siren or
train whistle as the car/ambulance/train moved past you. In scientic
terms, this change in pitch is described as a shift in the frequency of
the sound.
The equation for the observed sound frequency f , as seen by a
receiver moving at a speed v
r
relative to the air, is
f =
c +v
r
c +v
s
f
0
, (1)
where the sound is originally emitted at a frequency f
0
by a source
that moves at a speed v
s
relative to the air. Here, c is the speed of
sound.
We focus on motion along a straight line where we dene v
r
as
positive when the receiver moves toward the source and v
s
as
positive 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 Effect
A classical application of (1) is the calculation of the speed of a train
moving past a stationary receiver. Let the observed frequency of the
train whistle as the train moves towards the receiver be f
1
and the
observed frequency as the train moves away from the receiver be f
2
.
In this case, we can use (1) to obtain expressions for f
1
and f
2
:
f
1
=
c
c v
f
0
, (2)
f
2
=
c
c +v
f
0
, (3)
where v is now the speed of the train. We use these two equations to
eliminate the unknown f
0
and solve the resulting equation for v
v =
f
1
f
2
f
1
+f
2
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 experimental
data in the form of a sound recording of a passing train to compute f
1
and f
2
. Subsequently, this will yield v.
To do this, we will use MATLABs implementation of a powerful tool
known as the fast Fourier transform (FFT). Essentially, this is nothing
but a computationally efcient way of calculating the discrete Fourier
transform (DFT), a discrete approximation of the continuous Fourier
transform.
A NumFys Example www.numfys.net
Example 2.1: Doppler Effect - The Speed of a Passing Train
Slides
p.4 The Discrete Fourier Transform
A sound le is represented in MATLAB as a vector

x with N elements,
where each element is the sound amplitude sampled at time intervals
t . 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 element
x
n
in

x by applying the formula
x
n
=
1
N
N1

k=0
X
k
e
i 2
k
Nt
nt
(5)
Let us look closer at this expression and try to gure out what it
means. What it tells us is quite simply that

x is a superposition of
exponential functions with different frequencies f
k
=
k
Nt
and
amplitudes X
k
. Therefore, we can view the magnitude of amplitudes
|X
k
|
2
as a measure of the "weight of the frequency f
k
" 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 Transform
Q: How do we calculate

X to begin with?
A: We could apply the formula for the discrete Fourier transform,
X
k
=
N1

n=0
x
n
e
i 2
k
Nt
nt
. (6)
This requires O(N
2
) operations. In contrast, the FFT is a more
computationally efcient way to calculate

X, requiring only O(N lnN)
operations. There are several FFT algorithms and many make use of
the fact that the exponentials can all be written as

2i
N

kn
. (7)
In MATLAB, you can calculate

x from

X by using x=ifft(X), or the
other 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 MATLAB
Assume now that we store the sound of the train as it moves towards
the observer, in the vector sample1. Likewise, we store the sound of
the train as it moves away from the observer, in sample2.
We calculate the FFTs of these signals and store them in the vectors
p1 and p2 respectively.
p1=fft(sample1);
p2=fft(sample2);
To obtain a measure of the magnitude of the amplitudes, we calculate
their absolute values squared, element by element:
P1=p1.
*
conj(p1);
P2=p2.
*
conj(p2);
Finally, we calculate the frequency corresponding to each of the
elements 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 the
MATLAB script fftwrapper.m. A few more technical details are
required about how to import the audio le etc. but this will not be
discussed here. However, you are encouraged to look at the code in
fftwrapper.m and see if you can make sense of it.
The main thing you need to know for now is that fftwrapper.m can
be called as a function with an argument that can be true or false.
This depends on whether you want MATLAB to play the sound le
and the samples sample1 and sample2 or not. The return values
are 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 elements
in 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 MATLAB
The produced plot looks like this.
0 100 200 300 400 500 600 700
0
0.2
0.4
0.6
0.8
1
f (Hz)
P
/
P
m
a
x


Sample 1
Sample 2
The two samples contain frequency peaks. The peaks in sample2
are shifted towards smaller frequencies in relation to the peaks in
sample1. This is consistent with what we hear when a train passes
by: the sound it makes as it moves away form us is more low-pitched
than 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 f
1
corresponding to the tallest peak in
sample1 in the plot on the previous page. We see that the
corresponding peak in sample2 is also the tallest and we denote its
frequency by f
2
. We locate f
1
and f
2
as follows.
f1=f(P1==max(P1));
f2=f(P2==max(P2));
Notice that we could not expect in advance that the tallest peak in
sample1 would also be the tallest in sample2. In principle, this is not
a given and we needed the plot on the previous page to conrm 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 f
1
and f
2
, we can calculate v by use of (4). We dene
the 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 the
sound le lwrhuntrd-ns197.wav
1
into the same directory. Then,
run the script on your own computer. Listen closely to the sound le.
Does is it sound reasonable that the train is moving at 38.77 km/h?
1
Courtesy of David Safdy and Greg Lavoie of fwarailfan.net
A NumFys Example www.numfys.net
Example 2.1: Doppler Effect - The Speed of a Passing Train

You might also like