Rodney Dunning's Home Page | Research and Scholarship | VPython | Doppler Effect


Introduction

The Doppler effect reveals itself in the changing pitch of a horn as it approaches or recedes from your position. As the horn approaches you, you hear a higher pitch than a person riding with the horn. As the horn recedes from you, you hear a lower pitch than a person riding with the horn.

Description

In the animation, you will see a source of sound--a red sphere--and several rings that represent sound waves produced by the source. Also shown is a text box that presents the x and y coordinates of the mouse.

When you execute the program, you will be prompted for the speed of the source in m/s. Enter any value between 0 and 1020.

Screen Shot

Suggested Use

I've had success with the following steps in my physical science course for education majors:

  1. Enter 0 for the speed. Using the mouse cursor, have the students count off the number of wave fronts that pass the cursor. Tell the students to make a mental note of the frequency at which the wave fronts pass the cursor.
  2. Close and restart the program. Enter 200 for the speed. Placing the mouse cursor in front of the source, have the class count off the number of wave fronts that pass the cursor. Now place the cursor behind the source and have the students count off the number of wave fronts that pass the cursor.
  3. Ask the students to compare the frequency observed when the cursor is in front of the source to the frequency observed when the cursor is behind the source, and to compare both of these frequencies to that observed when the source was stationary. (Ask for a "greater than, same as, less than" response.)
Hints:

Source Code

The source code appears between the bars below. Copy and paste the code into the Python IDLE environment, and hit F5 to run the code.

When running the code, you will be prompted to enter the speed of the object.


from visual import *
from __future__ import division

"""
    Doppler effect animation.

    Rodney Dunning
    Longwood University
    Fall 2006


"""

#--------------------------------------------------
# Prompt user for velocity of the source
#--------------------------------------------------

finished = 0
while not finished:
    print "The speed of sound in this medium is 340 m/s."
    speed = input("Enter the speed of the source,\n"
                  "between 0 and 1020: ")
    speed /= 170
##    print "v-param= ",speed
    finished = speed <= 6


#--------------------------------------------------
# Scene attributes and functions
#--------------------------------------------------

scene.title = "VPython"
scene.x = 0
scene.y = 0
scene.width = 800
scene.height = 600
scene.range = (200,200,200)
scene.autoscale = 0 ##0 means autoscaling is OFF
scene.userzoom = 1 ##0 means user cannot zoom
scene.userspin = 1 ##0 means user cannot spin
scene.lights = [vector(0,0,1)]
scene.ambient = 0.5
scene.label = label(visible=1,
                    pos=(0,0,0),
                    xoffset = 0,
                    yoffset = 0,
                    text = "The Doppler Effect.")
scene.mouse_label = label(visible=1,
                          pos=(0,100,0),
                          text=("Mouse position"))

def return_mouse_pos(scene):
    scene.mouse_label.text = ("mouse\n x: %3.2f   y: %3.2f"
                              %(scene.mouse.pos.x, scene.mouse.pos.y))

def check_for_pause(scene): #checks for pause request
    if scene.mouse.clicked:
        scene.mouse.getclick()
        pause(scene)

def pause(scene): #pauses the animation
    while 1:
        return_mouse_pos(scene)
        if scene.mouse.clicked:
            scene.mouse.getclick()
            break

#------------------------------------------------------
# Create a source of sound-- a sphere
#------------------------------------------------------

my_source = sphere(radius = 3.0,
                   color = color.red,
                   pos = vector(-100,0,0),
                   vel = vector(speed,0,0))

#------------------------------------------------------
# Create a series of sound waves-- an array of rings
#------------------------------------------------------

""" In the main animation loop, we'll create a sound
    wave periodically.  Each new ring will be added
    to the array below.  The index number will increment
    allowing us to easily access the attributes for
    a particular ring, if necessary.
"""

SW = [] # initially, the array is empty
SWN = 1 # index number for the sound waves

#------------------------------------------------------
# Create time variables to keep up with the time
#------------------------------------------------------

t = 0 # time (clock reading)
dt = 0.05 # time step
t_max = 100 # max time
freq = 5 # sound-wave frequency

#-------------------------------------------------------
# The main animation loop
#-------------------------------------------------------

ready = 0
while not ready :
    ready = scene.mouse.getclick()
    scene.label.visible = 0
    scene.label.text = "Finished."

while t < t_max:
    rate(100)
    check_for_pause(scene)
    return_mouse_pos(scene)

    t += dt # the clock ticks

    my_source.pos += my_source.vel * dt

    if(t > SWN * freq):
        SW.append(ring(pos = my_source.pos,
                       radius = 0.0,
                       axis = (0,0,1),
                       thickness = 0.1,
                       color = color.white,
                       rate = 0.1))

        """
            The radius grows at a rate of 0.1 units
            per 0.05 time units.  So the speed of
            sound in this medium is 0.1 / 0.05 = 2
            distance units / time unit.
        """

        SWN += 1 #increase the index number for the sound waves

    for x in SW:
        x.radius += x.rate
        if x.radius > 100.0: x.visible = 0

scene.label.visible = 1