TExcelTAD
Home › Pre-Calculus, Geometry & Trigonometry › Waves, Sound and Signals

Waves, Sound and Signals

A sine wave is the graph of sin⁡\sin turned into something that moves in time: sound, vibration, a light that pulses, a character that bobs up and down. Three numbers describe it (amplitude, frequency and phase), adding sines builds richer sounds, and sampling a wave at fixed times, as every computer must, brings a surprise called aliasing. This lesson covers all three with NumPy.

Amplitude, frequency and phase

A sinusoidal signal is

y(t)=A⋅sin⁡(2πft+φ)y(t) = A \cdot \sin(2\pi f t + \varphi)
Symbol Name Meaning Unit
AA amplitude the height of the peaks (loudness for sound) same as yy
ff frequency full cycles per second (pitch for sound) hertz (Hz)
T=1/fT = 1/f period the time for one cycle seconds
ω=2πf\omega = 2\pi f angular frequency radians per second rad/s
φ\varphi phase where in its cycle the wave starts radians

A phase φ\varphi shifts the wave left in time by φ/(2πf)\varphi/(2\pi f) seconds; φ=π/2\varphi = \pi/2 turns sin⁡\sin into cos⁡\cos. These are the trig graphs with time on the horizontal axis: AA stretches vertically, ff squeezes horizontally.

Adding waves

Sound waves that meet simply add. Three notes played together give a chord; the sum of the three sines is still periodic, but it no longer looks like a sine. Going the other way, splitting any sound into its sine waves, is the job of the Fourier transform (signals and Fourier).

When two sines have almost the same frequency, something audible happens: the sum swells and fades, a beat. The sum-to-product identity (sum and difference formulas) explains it:

sin⁡a+sin⁡b=2⋅sin⁡a+b2⋅cos⁡a−b2\sin a + \sin b = 2 \cdot \sin\frac{a + b}{2} \cdot \cos\frac{a - b}{2}

With a=2π⋅440⋅ta = 2\pi \cdot 440 \cdot t and b=2π⋅444⋅tb = 2\pi \cdot 444 \cdot t, the sum is a 442 Hz tone multiplied by a slow cos⁡(2π⋅2⋅t)\cos(2\pi \cdot 2 \cdot t). The loudness follows ∣cos⁡∣\lvert \cos \rvert, which peaks twice per cycle of that cos⁡\cos: 4 beats per second, the difference of the two frequencies. Musicians tune instruments by listening for the beats to disappear.

python · try it yourself
import numpy as np

fs = 8000                                  # samples per second
t = np.arange(0, 1, 1 / fs)
a = 0.5 * np.sin(2 * np.pi * 440 * t)
b = 0.5 * np.sin(2 * np.pi * 444 * t)
beat = a + b
product = 2 * 0.5 * np.sin(2 * np.pi * 442 * t) * np.cos(2 * np.pi * 2 * t)
print("identity holds:", bool(np.allclose(beat, product)))

for start in [0.0, 0.125, 0.25, 0.375, 0.5]:
    window = beat[int(start * fs): int(start * fs) + 80]
    print(f"t = {start:5.3f} s  loudest sample {float(np.max(np.abs(window))):.3f}")

chord = sum(np.sin(2 * np.pi * f * t) for f in (261.63, 329.63, 392.00))   # C, E, G
print(f"chord peak {float(np.max(chord)):.3f}, mean {float(np.mean(chord)):+.4f}")
identity holds: True
t = 0.000 s  loudest sample 0.994
t = 0.125 s  loudest sample 0.112
t = 0.250 s  loudest sample 0.994
t = 0.375 s  loudest sample 0.112
t = 0.500 s  loudest sample 0.994
chord peak 2.986, mean +0.0019

The loudness is near 1 at t=0t = 0, drops to about 0.1 in the 10 ms window starting at t=0.125 st = 0.125\ \text{s} (the envelope is exactly 0 at that instant), and is back near 1 at 0.25 s: four swells per second. The chord's peak is below 3, because the three peaks never line up exactly.

Note: fs = 8000 here is the sample rate: the wave is only known at 8000 instants per second. That is the next topic.

Sampling and aliasing

A computer cannot store a continuous wave, only samples x[n]=y(n/fs)x[n] = y(n/f_s) taken fsf_s times per second. CD audio uses fs=44 100 Hzf_s = 44\,100\ \text{Hz}, games update physics perhaps 60 times per second, video shows 24 to 120 frames per second.

The Nyquist limit says that a sampled signal can only represent frequencies below fs/2f_s/2. A higher frequency does not vanish: it shows up disguised as a lower one, an alias. Sampled at fsf_s, frequencies ff and f−k⋅fsf - k \cdot f_s (for any whole kk) give exactly the same samples, because adding a whole number of turns to the angle changes nothing:

sin⁡(2πfn/fs)=sin⁡(2π(f−fs)n/fs+2πn)=sin⁡(2π(f−fs)n/fs)\sin(2\pi f n/f_s) = \sin(2\pi (f - f_s) n/f_s + 2\pi n) = \sin(2\pi (f - f_s) n/f_s)

So a 7 Hz wave sampled at 10 Hz gives the same samples as a −3 Hz wave, which is a 3 Hz wave flipped upside down: sin⁡(−x)=−sin⁡x\sin(-x) = -\sin x.

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

fs, f = 10, 7
n = np.arange(0, 11)
samples_7 = np.sin(2 * np.pi * f * n / fs)
samples_3 = -np.sin(2 * np.pi * 3 * n / fs)
print("7 Hz samples:", np.round(samples_7, 3).tolist())
print("same as -3 Hz:", bool(np.allclose(samples_7, samples_3)))

def alias(f, fs):
    r = f % fs
    return min(r, fs - r)                   # the frequency you actually observe

for f_in, rate in [(3, 10), (7, 10), (12, 10), (19, 10), (43100, 44100)]:
    print(f"{f_in} Hz sampled at {rate} Hz looks like {alias(f_in, rate)} Hz")

tt = np.linspace(0, 1, 1000)
fig, ax = plt.subplots(figsize=(6, 3))
ax.plot(tt, np.sin(2 * np.pi * 7 * tt), color="tab:blue", alpha=0.6, label="7 Hz")
ax.plot(tt, -np.sin(2 * np.pi * 3 * tt), "--", color="tab:orange", label="3 Hz (flipped)")
ax.plot(n / fs, samples_7, "ko", label="samples at 10 Hz")
ax.set_ylim(-1.15, 1.7); ax.set_xlabel("time (s)")
ax.legend(loc="upper center", ncol=3, fontsize=8); fig.tight_layout()
7 Hz samples: [0.0, -0.951, 0.588, 0.588, -0.951, 0.0, 0.951, -0.588, -0.588, 0.951, -0.0]
same as -3 Hz: True
3 Hz sampled at 10 Hz looks like 3 Hz
7 Hz sampled at 10 Hz looks like 3 Hz
12 Hz sampled at 10 Hz looks like 2 Hz
19 Hz sampled at 10 Hz looks like 1 Hz
43100 Hz sampled at 44100 Hz looks like 1000 Hz

The black dots lie on both curves: from the samples alone, nobody can tell which wave was recorded. That is why audio hardware filters out everything above fs/2f_s/2 before sampling, and why 44.1 kHz was chosen for CDs: it is a bit more than twice the 20 kHz limit of human hearing.

In software

  • Audio synthesis: oscillators are sines (and squares, saws: sums of sines). A game's engine sound is often a few sines whose frequencies follow the RPM.
  • Animation: a bobbing pickup uses y=y0+Asin⁡(2πft)y = y_0 + A\sin(2\pi f t); giving each copy a different phase φ\varphi keeps a row of coins from moving in lockstep.
  • The wagon-wheel effect: a spinning wheel filmed at 24 frames per second can seem to turn slowly backwards. It is aliasing in time.
  • Graphics: jagged edges and moiré patterns on fine textures are aliasing in space; mipmaps and anti-aliasing filter detail above the pixel "Nyquist limit".

Exercises

Exercise 1: Read the wave

For y(t)=3sin⁡(10πt+π/2)y(t) = 3\sin(10\pi t + \pi/2), give the amplitude, frequency, period and the time shift.

Answer

A=3A = 3, f=5 Hzf = 5\ \text{Hz}, T=0.2 sT = 0.2\ \text{s}, shifted left by 0.05 s (it is 3cos⁡(10πt)3\cos(10\pi t)).

Solution
2πf=10π  ⟹  f=5 Hz,T=1/5=0.2 sshift=φ/(2πf)=(π/2)/(10π)=1/20=0.05 s to the leftsin⁡(x+π/2)=cos⁡x  ⟹  y=3cos⁡(10πt)\begin{aligned} &2\pi f = 10\pi \;\Longrightarrow\; f = 5\ \text{Hz}, \quad T = 1/5 = 0.2\ \text{s} \\ &\text{shift} = \varphi/(2\pi f) = (\pi/2)/(10\pi) = 1/20 = 0.05\ \text{s to the left} \\ &\sin(x + \pi/2) = \cos x \;\Longrightarrow\; y = 3\cos(10\pi t) \end{aligned}

Exercise 2: Beat frequency

Two guitar strings play 196 Hz and 199 Hz. How many beats per second do you hear, and at what pitch?

Answer

3 beats per second, at a pitch of 197.5 Hz.

Solution

By sin⁡a+sin⁡b=2sin⁡a+b2cos⁡a−b2\sin a + \sin b = 2\sin\frac{a + b}{2}\cos\frac{a - b}{2}, the tone is the average (196+199)/2=197.5 Hz(196 + 199)/2 = 197.5\ \text{Hz} and the loudness swells at the difference, 199−196=3 Hz199 - 196 = 3\ \text{Hz}.

Exercise 3: Find the alias (code)

A 900 Hz tone is sampled at 1000 Hz. What frequency do the samples show? Check by comparing the samples with a wave of that frequency.

Answer

100 Hz (the samples equal those of a 100 Hz sine, flipped in sign).

Solution

900−1000=−100900 - 1000 = -100, so the alias is a 100 Hz wave with the opposite sign: sin⁡(2π⋅900⋅n/1000)=−sin⁡(2π⋅100⋅n/1000)\sin(2\pi \cdot 900 \cdot n/1000) = -\sin(2\pi \cdot 100 \cdot n/1000).

python · try it yourself
import numpy as np
n = np.arange(20)
s900 = np.sin(2 * np.pi * 900 * n / 1000)
s100 = np.sin(2 * np.pi * 100 * n / 1000)
print(bool(np.allclose(s900, -s100)))
True

Exercise 4: Choose a sample rate

A sensor signal contains frequencies up to 1.5 kHz. What is the minimum sample rate that avoids aliasing? Why do engineers use more in practice?

Answer

More than 3 kHz (2×1.5 kHz2 \times 1.5\ \text{kHz}). In practice a margin is added (for example 4 kHz), because real anti-aliasing filters cannot cut off sharply at exactly fs/2f_s/2.

Solution

The Nyquist limit is fs/2f_s/2, so we need fs/2>1500 Hzf_s/2 > 1500\ \text{Hz}, that is fs>3000 Hzf_s > 3000\ \text{Hz}. A filter that removes everything above 1.5 kHz needs a transition band before fs/2f_s/2, which is why CD audio uses 44.1 kHz for a 20 kHz band, not exactly 40 kHz.

Test yourself

What is the period of y=sin⁡(2π⋅50⋅t)y = \sin(2\pi \cdot 50 \cdot t)?

  1. 50 s
  2. 0.02 s
  3. 2π/502\pi/50 s
  4. 0.5 s

Signals are sampled at 48 000 Hz. What is the highest frequency that can be represented without aliasing?

  1. 48 000 Hz
  2. 96 000 Hz
  3. Just under 24 000 Hz
  4. 12 000 Hz

Two tones of 500 Hz and 505 Hz play together. What do you hear?

  1. A 1005 Hz tone
  2. Two separate tones with no interaction
  3. A 5 Hz tone
  4. A 502.5 Hz tone whose loudness pulses 5 times per second