Doppler Shift¶
McEwen, © 2025 | All Rights Reserved | Distribution Prohibited¶
version 1.0, 2025¶
This is a first version. It is a work in progress. Please report any errors or typos.
What is the Doppler shift?¶
You most likely have experienced the Doppler shift when an ambulance passes by you with the siren on. As the ambulance approaches you, you hear a constant high-pitched siren. When the ambulance passes you and moves away from you, you hear a constant lower-pitched siren.
The Doppler shift is the alteration of the observed frequency due to either the source moving, or the observer moving, or both source and observer moving. The observed shift in frequency is dependent on the relative velocity of the source and observer.
Problem 1
What is the quality of the wave that is associated with the pitch of a sound wave?
Solution
The frequency of the wave is associated with the pitch of a sound wave. Higher frequencies correspond to higher-pitched sounds, while lower frequencies correspond to lower-pitched sounds.
Problem 2
Juan stands at the 35 yard line and throws a football at 25 mph to Melissa who is running towards Juan at 10 mph. What is the velocity of the ball in Melissa's frame of reference?
Solution
In Melissa's frame of reference, she is stationary and everything else is moving relative to her. Since Melissa is running towards Juan at 10 mph, and the ball is thrown at 25 mph towards her, the relative velocity of the ball in Melissa's frame is:
$$v_{\text{ball, Melissa}} = 25 \text{ mph} + 10 \text{ mph} = 35 \text{ mph}$$
The ball appears to approach Melissa at 35 mph.
Problem 3
Huy runs to the right at a constant 20 mph. Robert runs to the right at a constant 23 mph. What is the relative velocity between Huy and Robert?
Solution
Since both are running in the same direction (to the right), the relative velocity is the difference in their speeds:
$$v_{\text{relative}} = 23 \text{ mph} - 20 \text{ mph} = 3 \text{ mph}$$
Robert is moving 3 mph faster than Huy (to the right in Huy's frame), or equivalently, Huy is moving 3 mph slower than Robert (to the left in Robert's frame).
Problem 4
Beth runs to the left at a constant 20 mph. Abdul runs to the right at a constant 23 mph. What is the relative velocity between Beth and Abdul?
Solution
Since Beth and Abdul are running in opposite directions, their relative velocity is the sum of their speeds:
$$v_{\text{relative}} = 20 \text{ mph} + 23 \text{ mph} = 43 \text{ mph}$$
In Beth's frame, Abdul is approaching at 43 mph. In Abdul's frame, Beth is approaching at 43 mph.
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML
from wave_animations import animate_wavefront, animate_wavefront_doppler
Example of stationary source and stationary observer¶
Suppose we have a wave source at $x=0~\text{m}$. Suppose Maria stands at $x=10~\text{m}$. Maria will measure the frequency of the wave by measuring the time it takes for the wave to repeat itself. Maria can mark any point on the wave to observe, but a natural point on the wave for Maria to observe is the wave crest. We will call the point that Maria is marking the "blip".
The animation below illustrates the situation. The top panel shows a wave traveling to the right. The location of two wave crests (what we are now calling the blip) is marked with a blue and red vertical line. The source of the wave is at $x=0$, while Maria stands at $x=10~\text{m}$. The second panel (bottom) removes the wave and leaves only the two blips (wave crests). We remove the wave because the wave is continuous and it is beneficial to just focus on the blips. Maria is marked with a red diamond and the source is marked with a green diamond. Maria will start her stopwatch when the first blip, wave crest marked in blue passes her. She will stop her stopwatch when the second blip, wave crest marked in red passes her. The time she records will be the period. She will calculate the frequency as $f_o=1/T$, where the $o$ subscript stands for observer.
When will Maria make her recordings?
Suppose the first blip, wave crest marked in blue, is emitted from the source at $t=0$. Maria will observe this wave crest at $d/v$ where $d=10~\text{m}$ and $v$ is the wave speed; this is the time it takes the signal to travel from the source to the observer. The second blip, wave crest marked in red, will be emitted one period after the first, so the wave crest in red is emitted at $t=T$. Maria will record the second wave crest at $T + d/v$.
In general, the time that an observer measures a signal from a moving source is $t + d/v$, where $d$ is the distance from the source to the observer and $v$ is the speed of the wave.
Problem 5
A wave moves to the right with wave parameters: $A=1~\text{mm}$, $\lambda=10~\text{cm}$, $T=0.5~\text{s}$. The wave is sourced at $x=0~\text{m}$. Yu stands at $x=2~\text{m}$ from the source. The first wave crest is emitted at $t=0$. At what time will Yu observe the first and second wave crests?
Solution
First, we need to find the wave speed: $$v = \frac{\lambda}{T} = \frac{10~\text{cm}}{0.5~\text{s}} = \frac{0.1~\text{m}}{0.5~\text{s}} = 0.2~\text{m/s}$$
The first wave crest is emitted at $t=0$ and travels a distance $d=2~\text{m}$ to reach Yu. The time when Yu observes the first crest is: $$t_1 = \frac{d}{v} = \frac{2~\text{m}}{0.2~\text{m/s}} = 10~\text{s}$$
The second wave crest is emitted at $t=T=0.5~\text{s}$. Yu will observe it at: $$t_2 = T + \frac{d}{v} = 0.5~\text{s} + 10~\text{s} = 10.5~\text{s}$$
Problem 7
In this notebook, we are considering sound waves. The animations in the notebook show traveling transverse waves. This is typical in a textbook to display sound waves like this. What kind of wave is a sound wave? What do the oscillations in the plots or animations represent?
Solution
Sound waves are longitudinal waves, not transverse waves.
In a longitudinal wave, the oscillations of the medium (air molecules) occur parallel to the direction of wave propagation. Air molecules oscillate back and forth along the direction the sound travels, creating regions of compression (high pressure) and rarefaction (low pressure).
The transverse wave animations in this notebook are a visual representation of the sound wave, where:
- The vertical displacement in the animation represents the displacement of air molecules from their equilibrium positions (or equivalently, the pressure variation)
- The horizontal axis represents position along the direction of wave travel
- The wave crests correspond to compressions (regions where air molecules are pushed together)
- The wave troughs correspond to rarefactions (regions where air molecules are spread apart)
This transverse representation is used because:
- It's easier to visualize than trying to animate molecules moving back and forth
- The mathematics is identical—both longitudinal and transverse waves satisfy the same wave equation
- It clearly shows wavelength, frequency, and wave propagation
In reality: If you could see air molecules during sound propagation, you would see them oscillating horizontally (parallel to the wave direction), bunching together and spreading apart, rather than moving up and down.
anim=animate_wavefront()
HTML(anim.to_html5_video())
Source moving toward observer¶
Note: Our everyday experience and intuition inform us that the observed frequency is larger (higher pitch) than the source frequency. So, we should expect that result in our derivation (one way to check if we did it right).
Maria stands at a distance away from the source. The source is traveling toward Maria at a speed $v_s$. Maria measures the frequency of the observed wave by marking the time between observations of consecutive blips (wave crests). The experiment is set up so that when the source crosses $x=0$ the first wave crest is generated at time $t=0$.
- The first blip will travel a distance $d$ at a speed $v$ (the wave speed) and therefore Maria will log the first observation of the blip at $t_{1,o}= d/v$ (the time is marked with a numerical subscript to indicate first measurement of the wave and $o$ subscript to indicate observer).
- The second blip wave will be generated at time $t=T$, where $T$ is the period of the wave.
- At time $t=T$ the source has moved to location $x=v_s T$.
- The second wave crest will travel a distance $d-v_sT$ to reach Maria with a speed of $v$. The distance is shorter now because the source has moved. The time that Maria records the second observation is $t_{2,o}= T + (d-v_s T)/v$.
Maria will then take the difference in the two times to compute the observed period, which we will call $T_o$:
\begin{align} \begin{split} T_o & = t_{2,o} - t_{1,o} \\ & = T + \frac{d-v_s T}{v} - \frac{d}{v} \\ & = T\left( 1 - \frac{v_s}{v} \right) \\ & = T\left( \frac{v - v_s}{v} \right). \end{split} \end{align}
Maria computes the observed frequency by taking the reciprocal of the observed period
\begin{align} f_o = \frac{v}{v-v_s} f. \end{align}
Note that the observed frequency is greater than the source frequency $f$.
The situation is illustrated in the animation below. Again, the wave is removed and we only concentrate on the wave crests. The green square represents the moving source. The red diamond represents the stationary observer. The wave variables are the same as the animation above, i.e., same period, wavelength, and amplitude. You should observe that the time between arrivals of the red and blue wave crests is shorter than the animation above.
anim=animate_wavefront_doppler()
HTML(anim.to_html5_video())
Source moving away from the observer¶
Note: Our everyday experience and intuition inform us that the observed frequency is smaller (lower pitch) than the source frequency.
Maria stands at a distance away from the source. The source is traveling away from Maria at a speed $v_s$. Maria measures the frequency of the observed wave by marking the time between observations of consecutive blips (wave crests). The experiment is set up so that when the source crosses $x=0$ the first blip is generated at time $t=0$.
- The first blip will travel a distance $d$ at a speed $v$ (the wave speed) and therefore Maria will log the first blip observation at $t_{1,o}= d/v$ (the time is marked with a numerical subscript to indicate the first measurement of the wave and an $o$ subscript to indicate observer).
- The second blip will be generated at time $t=T$, where $T$ is the period of the wave.
- At time $t=T$ the source has moved to location $x=-v_s T$ (the source is moving to the left).
- The second blip will travel a distance $d + v_sT$ to reach Maria with a speed of $v$. Therefore, the time that Maria records the second observation is $t_{2,o}= T + (d + v_s T)/v$.
Maria will then take the difference in the two times to compute the observed period, which we will call $T_o$: \begin{align} \begin{split} T_o & = t_{2,o} - t_{1,o} \\ & = T + \frac{d+ v_s T}{v} - \frac{d}{v} \\ & = T\left( 1 + \frac{v_s}{v} \right) \\ & = T\left( \frac{v + v_s}{v} \right). \end{split} \end{align}
Maria computes the observed frequency by taking the reciprocal of the observed period: \begin{align} f_o = \frac{v}{v+v_s} f. \end{align}
Stationary source, observer moving toward source¶
In this experiment the source is stationary, but Maria is moving toward the source with velocity $v_o$. The observed frequency will be larger than the source frequency—Maria will hear a higher pitch.
When the source is stationary, it emits blips separated by wavelength $\lambda = vT$, where $v$ is the wave speed and $T$ is the period.
Maria moves toward the source at speed $v_o$. From her perspective, she encounters the oncoming wave crests at a speed of $v + v_o$ (the wave approaches at $v$, and she moves toward it at $v_o$).
Similar to our earlier thought experiments, the time that it takes the first blip to travel to Maria is $t_{1,o}=d/(v + v_o)$. The time that it takes the second blip to reach Maria is $t_{2,o}= T + \frac{d - v_oT}{v+ v_o}$. This time is composed of two parts. One, the period $T$, the time between consecutive blips made by the source. Two, the travel time. The travel time includes the fact that Maria has moved a distance $v_oT$ closer to the source in time $T$.
Maria computes the observed period as: \begin{align} \begin{split} T_o & = t_{2,o} - t_{1,o} \\ & = T + \frac{d - v_o T}{v + v_o} - \frac{d}{v + v_o} \\ & = T\left( \frac{v + v_o - v_o}{v + v_o} \right) \\ & = T\left( \frac{v}{v + v_o} \right). \end{split} \end{align}
Maria computes the observed frequency by taking the reciprocal of the observed period: \begin{align} f_o = \frac{v + v_o}{v} f. \end{align}
As expected, the observed frequency $f_o$ is greater than the source frequency $f$ when Maria moves toward the source.
The Role of the Medium in Sound Doppler Effect¶
The fact that sound propagates through a medium has some important consequences. If the source is moving toward the observer, the wave crests get bunched up, and the wavelength decreases: $\lambda \to (v - v_s)T$. If the source is moving away, the wavelength increases: $\lambda \to (v + v_s)T$.
If the observer is moving, then they observe a different wave speed: $v \to v + v_o$ if the observer is moving toward the source, and $v \to v - v_o$ if the observer is moving away from the source.
Because sound propagates through a medium, it is not the relative velocity between source and observer that matters, but the velocities with respect to the medium. This means you don't get the same frequency change if the source is moving toward a stationary observer with speed $v_{experiment}$ as you would if an observer is moving toward a stationary source with speed $v_{experiment}$.
Example:
Consider $v_{experiment} = 10$ m/s and the speed of sound $v = 340$ m/s (about 760 mph). Suppose the source emits a frequency $f = 1000$ Hz.
Source moving toward stationary observer: \begin{equation} f_o = \frac{v}{v - v_s} f = \frac{340}{340 - 10} (1000) = \frac{340}{330} (1000) \approx 1030.3 \text{ Hz} \end{equation}
Observer moving toward stationary source: \begin{equation} f_o = \frac{v + v_o}{v} f = \frac{340 + 10}{340} (1000) = \frac{350}{340} (1000) \approx 1029.4 \text{ Hz} \end{equation}
The observed frequencies are different. This asymmetry arises because sound propagates through a medium (air).
This is unlike the Doppler effect for light (electromagnetic waves). For electromagnetic waves, the symmetry between source and observer velocity is maintained. Electromagnetic waves don't propagate in a medium and therefore do not have this asymmetry.
Challenge Problem
Phooey on you, Joe! That numerical experiment might show the results are different, but they are pretty close. After all, the speed of sound is 760 mph. For everyday velocities, we are never going to notice this asymmetry.
Yes, you are right, but can you walk the talk and put your math where your mouth is? Show that to first order the results are the same. Hint: use the binomial expansion or a Taylor expansion.
Click to reveal solution
Solution¶
We want to show that for small velocities ($v_s, v_o \ll v$), both cases give the same result to first order.
Binomial Expansion Reminder: For small $x$ (i.e., $|x| \ll 1$): \begin{equation} (1 + x)^n \approx 1 + nx + O(x^2) \end{equation}
Case 1: Source moving toward stationary observer \begin{equation} \begin{split} f_o &= \frac{v}{v - v_s} f \\ &= \frac{1}{1 - v_s/v} f \\ &= \left(1 - \frac{v_s}{v}\right)^{-1} f \end{split} \end{equation}
Using the binomial expansion with $x = -v_s/v$ and $n = -1$: \begin{equation} \left(1 - \frac{v_s}{v}\right)^{-1} \approx 1 + \frac{v_s}{v} \end{equation}
Therefore \begin{equation} f_o \approx \left(1 + \frac{v_s}{v}\right) f. \end{equation}
Case 2: Observer moving toward stationary source \begin{equation} \begin{split} f_o &= \frac{v + v_o}{v} f \\ &= \left(1 + \frac{v_o}{v}\right) f \end{split} \end{equation}
This is already in first-order form—no expansion needed!
When $v_s = v_o = v_{experiment}$, both cases give to first order \begin{equation} f_o \approx \left(1 + \frac{v_{experiment}}{v}\right) f. \end{equation}
The difference only appears in the second-order terms (proportional to $(v_{experiment}/v)^2$), which are negligible for everyday speeds!
Problem 8
There are a few more examples we could work out. For these problems, think carefully about:
- Whether the observer is moving toward or away from the wave crests (affecting the relative speed at which crests are encountered)
- Whether the source is moving, which affects the spacing between wave crests
- Whether the observed frequency should be higher or lower than the source frequency
Suppose the source is stationary and Maria is moving away from the source at $v_o$. Show that the observed frequency is \begin{align} f_o = \frac{v - v_o}{v} f. \end{align}
Suppose the source is moving toward Maria with velocity $v_s$ and Maria is moving toward the source at $v_o$. Show that the observed frequency is \begin{align} f_o = \frac{v + v_o}{v-v_s} f. \end{align}
Suppose the source is moving toward Maria with velocity $v_s$ and Maria is moving away from the source at $v_o$. Show that the observed frequency is \begin{align} f_o = \frac{v - v_o}{v-v_s} f. \end{align}
We still have not exhausted all possible scenarios. What are some other scenarios and what are the formulas for the observed frequency?
Hint for parts 1-3
General approach for combined motion:
When both source and observer move, we need to account for two effects separately:
Source motion changes the wavelength of the wave:
- If the source moves toward the observer at $v_s$, successive crests are emitted closer together
- The wavelength becomes $\lambda' = (v - v_s)T$ (from our earlier derivation)
- If the source moves away at $v_s$, then $\lambda' = (v + v_s)T$
Observer motion changes how quickly the observer encounters wave crests:
- If the observer moves at $v_o$ toward the source, crests are encountered at relative speed $v + v_o$
- If the observer moves away, the relative speed is $v - v_o$
Combine these: $T_o = \frac{\lambda'}{\text{relative speed}}$, then $f_o = 1/T_o$.
Solution
Part 1: Source stationary, Maria moving away at $v_o$
The source is stationary, so the wavelength is $\lambda = vT$.
Maria moves away from the source at $v_o$. The wave crests approach her at speed $v$, but she's moving away at $v_o$, so the relative speed at which she encounters crests is $v - v_o$.
The observed period is: $$T_o = \frac{\lambda}{v - v_o} = \frac{vT}{v - v_o}$$
The observed frequency is: $$f_o = \frac{1}{T_o} = \frac{v - v_o}{vT} = \frac{v - v_o}{v} f$$
The observed frequency is less than the source frequency, as expected.
Part 2: Source moving toward Maria at $v_s$, Maria moving toward source at $v_o$
From our earlier derivation, when the source moves toward the observer at $v_s$, the wavelength is: $$\lambda' = (v - v_s)T$$
Maria moves toward the source at $v_o$, so she encounters wave crests at relative speed $v + v_o$.
The observed period is: $$T_o = \frac{\lambda'}{v + v_o} = \frac{(v - v_s)T}{v + v_o}$$
The observed frequency is: $$f_o = \frac{1}{T_o} = \frac{v + v_o}{(v - v_s)T} = \frac{v + v_o}{v - v_s} f$$
The observed frequency is greater than the source frequency (both motions increase the frequency).
Part 3: Source moving toward Maria at $v_s$, Maria moving away from source at $v_o$
The source moves toward Maria at $v_s$, so the wavelength is: $$\lambda' = (v - v_s)T$$
Maria moves away from the source at $v_o$, so she encounters wave crests at relative speed $v - v_o$.
The observed period is: $$T_o = \frac{\lambda'}{v - v_o} = \frac{(v - v_s)T}{v - v_o}$$
The observed frequency is: $$f_o = \frac{1}{T_o} = \frac{v - v_o}{(v - v_s)T} = \frac{v - v_o}{v - v_s} f$$
Whether $f_o > f$ or $f_o < f$ depends on whether $v_o < v_s$ or $v_o > v_s$. The source motion increases frequency (decreases wavelength) while the observer motion decreases frequency (reduces encounter rate).
Part 4: Other scenarios
The two remaining scenarios are:
Scenario A: Source moving away from Maria at $v_s$, Maria moving toward the source at $v_o$
The wavelength is $\lambda' = (v + v_s)T$ (source moving away increases wavelength).
Maria encounters crests at relative speed $v + v_o$ (moving toward the source).
$$f_o = \frac{v + v_o}{v + v_s} f$$
Scenario B: Source moving away from Maria at $v_s$, Maria moving away from the source at $v_o$
The wavelength is $\lambda' = (v + v_s)T$.
Maria encounters crests at relative speed $v - v_o$ (moving away from the source).
$$f_o = \frac{v - v_o}{v + v_s} f$$
General formula: The Doppler shift formula can be written as: $$f_o = \frac{v \pm v_o}{v \mp v_s} f$$
Summary of Doppler Shift Formulas¶
| Source Motion | Observer Motion | Observed Frequency | Notes |
|---|---|---|---|
| Stationary | Stationary | $f_o = f$ | No Doppler shift |
| Stationary | Toward at $v_o$ | $f_o = \frac{v + v_o}{v} f$ | Higher pitch |
| Stationary | Away at $v_o$ | $f_o = \frac{v - v_o}{v} f$ | Lower pitch |
| Toward at $v_s$ | Stationary | $f_o = \frac{v}{v - v_s} f$ | Higher pitch |
| Away at $v_s$ | Stationary | $f_o = \frac{v}{v + v_s} f$ | Lower pitch |
| Toward at $v_s$ | Toward at $v_o$ | $f_o = \frac{v + v_o}{v - v_s} f$ | Higher pitch |
| Toward at $v_s$ | Away at $v_o$ | $f_o = \frac{v - v_o}{v - v_s} f$ | Competing effects |
| Away at $v_s$ | Toward at $v_o$ | $f_o = \frac{v + v_o}{v + v_s} f$ | Competing effects |
| Away at $v_s$ | Away at $v_o$ | $f_o = \frac{v - v_o}{v + v_s} f$ | Lower pitch |
General Formula: $$f_o = \frac{v \pm v_o}{v \mp v_s} f$$
Problem 9
A stationary siren is emitting sound at a constant frequency of 1200 Hz. You are moving past the siren along a path that takes you directly by it. At a certain instant, you observe a frequency of 1180 Hz. The speed of sound in air is 340 m/s.
a) Are you moving toward or away from the siren?
b) What is your speed?
Hint
Since the siren is stationary and only you are moving, you need to use the formula for a stationary source and moving observer. The observed frequency is lower than the source frequency—what does this tell you about your direction of motion?
Solution
Part a) You are running away from the siren.
Part b) Finding your speed:
Since the source is stationary and you (the observer) are moving away, we use: \begin{equation} f_o = \frac{v - v_o}{v} f \end{equation}
Solving for $v_o$ algebraically: \begin{equation} \frac{f_o}{f} = \frac{v - v_o}{v} \end{equation}
\begin{equation} v \cdot \frac{f_o}{f} = v - v_o \end{equation}
\begin{equation} v_o = v - v \cdot \frac{f_o}{f} = v\left(1 - \frac{f_o}{f}\right) \end{equation}
Now substituting the known values: $v = 340$ m/s, $f = 1200$ Hz, $f_o = 1180$ Hz: \begin{equation} v_o = 340 \left(1 - \frac{1180}{1200}\right) = 340 \left(1 - 0.9833\right) = 340(0.0167) = 5.7 \text{ m/s} \end{equation}
Problem 10 (This problem needs physical verfiction, but still good for a problem :) . ) The human ear can typically detect frequency changes of about 1-3%, though this varies with age, musical training, and individual differences. For example, most people can hear the difference between 435 Hz and 444 Hz (about a 2% change), while some trained musicians might detect changes as small as 0.5%.
a) Estimate how fast you would need to walk toward a stationary sound source (such as someone speaking at 500 Hz) to produce a 2% frequency shift that would be noticeable to most people. Use $v = 340$ m/s for the speed of sound.
b) Does this explain why you don't notice Doppler effects during normal conversation when walking toward or away from someone?
Hint
For small velocities where $v_o \ll v$, you can approximate the Doppler formula. Also, think about typical walking speeds (roughly 1-2 m/s).
Solution
Part a) Estimating the required speed
For a stationary source and observer moving toward the source: \begin{equation} f_o = \frac{v + v_o}{v} f = \left(1 + \frac{v_o}{v}\right) f \end{equation}
The fractional change in frequency is: \begin{equation} \frac{\Delta f}{f} = \frac{f_o - f}{f} = \frac{v_o}{v} \end{equation}
Solving for $v_o$ in terms of the fractional change: \begin{equation} v_o = v \cdot \frac{\Delta f}{f} \end{equation}
For a 2% change (noticeable to most people), $\frac{\Delta f}{f} = 0.02$: \begin{equation} v_o = v \times 0.02 \end{equation}
Substituting $v = 340$ m/s: \begin{equation} v_o = 340 \times 0.02 = 6.8 \text{ m/s} \end{equation}
You would need to move at approximately 6.8 m/s (about 24.5 km/h or 15 mph). This is faster than a jog; it's a running pace! I have never noticed a pitch change while sprinting, although, I am super slow...maybe just bad ears...
Interactive demonstration:
Can you hear the difference? Below is Python code to produce two notes, 440 Hz and 450 Hz. When you play each note, can you hear the difference? The code is provided in case you want to experiment yourself. Notice that to generate the tones, I used a sine wave. I also plotted each tone—now you know what a sine wave looks like!
import numpy as np
from IPython.display import Audio, display
import matplotlib.pyplot as plt
def generate_tone(frequency, duration=2.0, sample_rate=44100, amplitude=0.3):
"""
Generate a pure tone at the specified frequency.
Parameters:
- frequency: frequency in Hz
- duration: duration in seconds
- sample_rate: samples per second
- amplitude: amplitude (0 to 1, recommend 0.3 for comfortable listening)
Returns:
- numpy array of audio samples
"""
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
tone = amplitude * np.sin(2 * np.pi * frequency * t)
return tone, t
# Generate 440 Hz tone (Concert A)
tone_A, t = generate_tone(440)
# Note to students: We only plot the first 200 data points for clarity.
# The complete audio array contains 88,200 samples (2 seconds at 44,100 samples/second),
# which would be too dense to visualize clearly on a single plot.
# Feel free to experiment by changing the 200 to a different number (e.g., 100, 500, 1000)
# to see more or fewer oscillations of the sine wave.
fig, ax = plt.subplots(figsize=(12, 4)) # Makes the plot wider
ax.plot(t[:200], tone_A[:200], linewidth=1.5)
ax.set_xlabel("Time (s)", size=14)
ax.set_ylabel("Signal Amplitude", size=14)
ax.set_title("440 Hz Sine Wave (Concert A) - First 200 samples", size=16)
ax.grid(True, alpha=0.3) # Optional: adds a light grid for easier reading
plt.tight_layout() # Ensures labels don't get cut off
plt.show()
print("Listen to the 440 Hz tone:")
display(Audio(tone_A, rate=44100))
Listen to the 440 Hz tone:
# Generate 450 Hz t
tone_B, t = generate_tone(450)
# Note to students: We only plot the first 200 data points for clarity.
# The complete audio array contains 88,200 samples (2 seconds at 44,100 samples/second),
# which would be too dense to visualize clearly on a single plot.
# Feel free to experiment by changing the 200 to a different number (e.g., 100, 500, 1000)
# to see more or fewer oscillations of the sine wave.
fig, ax = plt.subplots(figsize=(12, 4)) # Makes the plot wider
ax.plot(t[:200], tone_B[:200], linewidth=1.5)
ax.set_xlabel("Time (s)", size=14)
ax.set_ylabel("Signal Amplitude", size=14)
ax.set_title("450 Hz Sine Wave - First 200 samples", size=16)
ax.grid(True, alpha=0.3) # Optional: adds a light grid for easier reading
plt.tight_layout() # Ensures labels don't get cut off
plt.show()
print("Listen to the 450 Hz tone:")
display(Audio(tone_B, rate=44100))
Listen to the 450 Hz tone:
Here is the beat formed by the two tones, audio and plot.
beat = tone_A - tone_B
print("Listen to the beat tone:")
display(Audio(beat, rate=44100))
Listen to the beat tone:
fig, ax = plt.subplots(figsize=(12, 4)) # Makes the plot wider
ax.plot(t[300:5500], beat[300:5500], linewidth=1.5)
ax.set_xlabel("Time (s)", size=14)
ax.set_ylabel("Signal Amplitude", size=14)
ax.set_title("The Fish Beat", size=16)
ax.grid(True, alpha=0.3) # Optional: adds a light grid for easier reading
plt.tight_layout() # Ensures labels don't get cut off
plt.show()
I added some guitar tones.
low_E, t = generate_tone(82.41)
print("Low E-string on guitar:")
display(Audio(low_E, rate=44100))
Low E-string on guitar:
high_E, t = generate_tone(329.63)
print("high E-string on guitar:")
display(Audio(high_E, rate=44100))
high E-string on guitar: