Lecture 1 · transcript
Sound, signals and sampling
What the professor says, from the lecture's own English (UK) captions. Each time opens the video at that moment on YouTube.
Welcome
0:00Welcome to today's lecture. I'm Professor Kettlewick, your AI professor.
0:05This is Signals and Sound, lecture one: sound, signals and sampling.
0:11Today we'll find out how a sound becomes a list of numbers,
0:17and the one rule you must never break.
What is a signal?
0:22Let's start with something you do every day without thinking: hearing.
0:27When a guitar string vibrates, it pushes the air next to it,
0:33squashing it together and then letting it spread apart.
0:38Those squashes and stretches travel outwards as a pressure wave,
0:43at about 340 metres a second, until they reach your ear.
0:50A microphone does what your eardrum does. A thin diaphragm moves with the pressure,
0:55and the microphone turns that movement into a voltage.
0:59More pressure, higher voltage; less pressure, lower voltage.
1:06That changing voltage is a signal:
1:08a quantity that changes over time and carries information.
1:14In this course, almost every signal starts life as a sound and ends life as a sound.
1:22The simplest signal of all is a sine wave.
1:27It's the sound of a tuning fork, and, as we'll see later in the course,
1:33every other sound can be built from sine waves added together.
1:39So let's learn to read one.
1:43The height of the wave from the middle line to a peak is its amplitude.
1:49A bigger amplitude means a louder sound.
1:55The time it takes to go through one complete cycle is the period,
2:01written T and measured in seconds.
2:04And the number of complete cycles in one second is the frequency,
2:09f, measured in hertz.
2:12Period and frequency are two views of the same thing: f equals one over T.
2:21Let's try one. The note A that orchestras tune to has a frequency of 440 Hz.
2:30What's its period? One over 440 is about 2.27 ms.
2:38440 complete squashes and stretches of the air, every second.
2:47Here it is.
2:49[A pure tone: 440 Hz]
2:51And now half the frequency, 220 Hz.
2:57[A pure tone: 220 Hz]
2:59Lower: in fact exactly one octave lower.
3:04Double the frequency and you go up an octave; halve it and you go down one.
3:11Young, healthy ears hear from about 20 Hz up to about 20,000 Hz: 20 kHz.
3:19The top end falls as we get older,
3:22and many adults hear little above 15 or 16 kHz.
3:29Hold on to that 20 kHz. It's going to matter a great deal in a few minutes.
3:37One more property: phase. Two sine waves can have the same frequency
3:44and amplitude but be at different points in their cycle.
3:49Phase tells us where in its cycle a wave is at time zero, in degrees or radians.
3:58You won't hear the phase of a single tone, but when signals are added together,
4:05phase decides whether they reinforce each other or cancel out.
Why go digital?
4:11So we have a signal: a voltage that varies smoothly and continuously.
4:17We call it analogue, because the voltage is an analogue,
4:22a likeness, of the pressure wave. Why not keep it that way?
4:28Because every time you copy, store or send an analogue signal,
4:34a little noise and distortion creep in, and they never come out again.
4:40Copy a tape, then copy the copy,
4:43and by the fifth generation you can hear the hiss.
4:47A digital signal is a list of numbers,
4:51and a number can be copied a million times without changing.
4:58Numbers can also be processed by a computer: filtered,
5:03mixed, compressed, and sent round the world.
5:07Turning a smooth voltage into numbers takes two steps. First,
5:13we measure it at regular moments in time. That's sampling, and it's today's subject.
5:20Second, we round each measurement to one of a fixed set of levels.
5:25That's quantisation, and it's next lecture.
5:29Sampling chops up time; quantisation chops up the voltage.
Sampling
5:34Imagine photographing the signal's voltage at regular intervals,
5:38and writing down each value. Each measurement is a sample.
5:43The number of samples we take each second is the sampling rate,
5:48or sampling frequency, fs, in hertz.
5:53The time between samples is the sampling period, Ts, which is one over fs.
6:01Once the signal has been sampled, the smooth wave is gone.
6:07All we keep are the dots.
6:10And that is the question for this whole lecture:
6:15from just these dots, can we get the original wave back?
6:21Compact discs use a sampling rate of 44.1 kHz: 44,100 samples every second.
6:28So the time between samples is one over 44,100, about 22.7 microseconds.
6:38How many samples does each cycle of a wave get?
6:41Divide the sampling rate by the signal's frequency.
6:44A 1 kHz tone, sampled at 44.1 kHz, gets 44.1 samples per cycle. Plenty.
6:56A 10 kHz tone gets 4.41. Fewer, but still enough, as we'll see.
7:05Your turn. Pause the video if you like.
7:09At 44.1 kHz, how many samples does each cycle of a 20 kHz tone get?
7:2244.1 divided by 20: 2.205 samples per cycle.
7:30Just over two. That number is no accident.
How fast is fast enough? The sampling theorem
7:34Here is the rule at the heart of digital audio.
7:39To capture a frequency, you need more than two samples per cycle.
7:46Put the other way round: the sampling rate must be more
7:50than twice the highest frequency in the signal. fs must be greater than two fmax.
8:00This is the sampling theorem, often called the Nyquist theorem after Harry Nyquist;
8:06Claude Shannon published the full proof in 1949.
8:11Half the sampling rate, fs over two, is called the Nyquist frequency:
8:17the highest frequency that a given sampling rate can capture.
8:22Why more than two, and not exactly two?
8:28Watch what happens at exactly two samples per cycle.
8:32If the samples happen to land on the peaks, we see the wave.
8:38But if they land on the zero crossings,
8:40every sample is zero, and the wave has vanished completely.
8:45Exactly two isn't safe. Just over two is enough.
8:51Now we can see why a CD samples at 44.1 kHz.
8:58Hearing tops out around 20 kHz, so we need a sampling rate above 40 kHz.
9:0544.1 gives a Nyquist frequency of 22.05 kHz, with a little room to spare,
9:17and we'll see what that room is for in a moment.
9:21The oddly precise number comes from history:
9:25the first digital recorders stored their samples on video tape,
9:30and 44,100 a second fitted the lines of a television picture exactly,
9:37in both the European and the American systems.
9:41Different jobs use different rates.
9:46Telephones sample at 8 kHz, so they can carry nothing above 4 kHz.
9:51That's enough to understand speech, but listen to what it does to music.
9:57[Music, at 44.1 kHz]
10:07[The same music, sampled at 8 kHz]
10:16The words would survive; the sparkle doesn't. Film and video use 48 kHz,
10:23and studios often record at 96 kHz, which gives them more room while they work.
10:30Suppose a bat's call reaches 100 kHz.
10:36What's the lowest sampling rate that could capture it?
10:44More than twice 100 kHz: anything above 200 kHz.
Aliasing and the anti-aliasing filter
10:51So what happens if we break the rule?
10:55Something strange, and you've almost certainly seen it. In films,
11:00the wheels of a speeding wagon or car sometimes seem to turn slowly backwards.
11:07A film camera samples the world 24 times a second.
11:13When a spoke moves almost a full spoke-spacing between one frame
11:18and the next, the camera can't tell that it went nearly all the way forwards;
11:23the nearest match is that it crept a little backwards.
11:31The fast motion is disguised as a slow one.
11:36Sound does exactly the same. Here's a 7 kHz tone, sampled at 8 kHz.
11:43That's far fewer than two samples per cycle. Now look at the dots.
11:50A gentle 1 kHz wave passes through every single one of them.
11:57From the samples alone, there's no way to tell which wave we started with,
12:04and when we play them back, we hear the 1 kHz tone.
12:10This impostor frequency is called an alias, and the effect is aliasing.
12:17Listen to it happen. This tone rises steadily from nothing to 8 kHz,
12:24sampled at 8 kHz with no protection.
12:27[A tone rising steadily, sampled at 8 kHz with no filter]
12:30[Above 4 kHz, the pitch we hear falls back down]
12:33It rises to 4 kHz, the Nyquist frequency,
12:38and then turns round and falls, even though the real tone kept rising.
12:46Everything above the Nyquist frequency folds back down,
12:50like a reflection in a mirror at fs over two.
12:54Here's the crucial point. Once aliasing has happened, it can't be undone.
13:01The alias sits among the real sound,
13:03and nothing done afterwards can tell them apart.
13:08So the only cure is prevention: before a signal is sampled, it goes
13:15through a low-pass filter that removes everything above the Nyquist frequency.
13:21It's called an anti-aliasing filter,
13:23and it's the first block in every analogue to digital system.
13:29If you're studying A Level Electronics,
13:32this is the transmitter in a pulse code modulation system:
13:38the low-pass filter, then the sampling gate driven by the sampling clock,
13:45then the analogue to digital converter, then a parallel-in
13:49serial-out shift register that sends the bits out one at a time.
13:54The low-pass filter comes first for exactly the reason we've just heard.
14:01And that's what the CD's spare room is for.
14:05A real filter can't cut off like a cliff edge;
14:10it needs a range of frequencies to go from letting sound through to blocking it.
14:18On a CD, that range is the gap between 20 kHz,
14:22the top of hearing, and 22.05 kHz, the Nyquist frequency.
A Level recap and three questions
14:32Let's pull that together.
14:34A signal is a quantity that changes over time; in audio,
14:40usually a voltage that follows a pressure wave.
14:44Sampling measures the signal at regular intervals. The sampling rate, fs,
14:51is the number of samples each second, and the sampling period is one over fs.
14:58To capture a frequency, sample at more than twice it: fs greater than two fmax.
15:06Half the sampling rate is the Nyquist frequency.
15:11Frequencies above the Nyquist frequency don't disappear.
15:16They fold back down as aliases, and they can't be removed afterwards,
15:21so an anti-aliasing low-pass filter comes before the sampler.
15:25Three questions to try. The answers are on the website, with full working.
15:33One: a signal contains frequencies up to 12 kHz. What is the minimum sampling rate?
15:41Two: a system samples at 32 kHz.
15:45What is its Nyquist frequency, and could it capture an 18 kHz tone?
15:52Three: in a pulse code modulation transmitter,
15:56why must the low-pass filter come before the sampling gate, and not after it?
16:02If you're studying for A Level, that's the core of today's lecture.
16:09Stay with me if you'd like to go further:
16:13next we'll look at the mathematics of aliasing,
16:16and at how the original wave is rebuilt from the samples.
First year: the mathematics of aliasing
16:23Let's write the sampled sine wave down.
16:28A continuous tone is x of t equals A cos of two pi f t, plus phi.
16:36We keep it only at the sample times, t equals n Ts,
16:41where n counts the samples: zero, one, two, and so on.
16:48So the samples are x of n equals A cos of two pi f n Ts, plus phi.
16:58And since Ts is one over fs, that's A cos of two pi n times f over fs, plus phi.
17:10Notice that the samples depend only on the ratio f over fs:
17:16the frequency measured in cycles per sample.
17:21Now replace f with f plus fs.
17:26The ratio becomes f over fs, plus one, so the angle gains an extra two pi n.
17:34But n is a whole number,
17:36and adding a whole number of complete turns to a cosine changes nothing.
17:42The samples are identical.
17:46A tone at f and a tone at f plus fs can't be told apart once they're sampled;
17:56nor can f plus two fs, or f minus fs, or f plus any whole number of fs.
18:07There's one more twin. Cosine is an even function:
18:11cos of minus theta equals cos of theta. So a tone at minus f gives the same samples
18:20as a tone at f, with its phase flipped.
18:25Put the two facts together, and the frequency fs minus f matches f as well.
18:32That's the mirror at the Nyquist frequency.
18:37So to find the alias of any frequency: shift it by whole multiples of fs
18:43until it lands between minus fs over two and plus fs over two, then take its size.
18:54In symbols, the alias is the size of f minus k fs,
19:00choosing the whole number k that makes it smallest.
19:05Let's practise at 48 kHz. 30 kHz: subtract 48, giving minus 18,
19:11so the alias is 18 kHz. 50 kHz: subtract 48, 2 kHz.
19:1747 kHz: 1 kHz. And 24 kHz, exactly the Nyquist frequency?
19:23It maps to itself, but it's the edge case we met earlier,
19:29where the samples can vanish.
19:32At 44.1 kHz, what does a 25 kHz tone alias to?
19:4525 minus 44.1 is minus 19.1, so 19.1 kHz: right in the range we can hear.
19:56Here's the same idea as a picture. Along the bottom,
20:01the frequency that goes in; up the side, the frequency that comes out.
20:06Up to the Nyquist frequency, out equals in: a straight line.
20:13Then the line folds back down, reaching zero at fs,
20:19rises again to the Nyquist frequency at one and a half fs, and so on: a zigzag.
20:28Every input frequency, however high, lands somewhere between zero and fs over two.
20:36This tells us how hard the anti-aliasing filter has to work.
20:42At 44.1 kHz, anything above 22.05 kHz folds back,
20:49and the fold reaches down into the band we hear: 24.1 kHz lands exactly on 20 kHz.
20:58So the filter must pass everything up to 20 kHz untouched,
21:03and all but silence everything above 24.1 kHz. That's a very steep filter.
21:12Modern converters solve it neatly: they sample far faster,
21:16often 64 times faster, so that a gentle analogue filter is enough,
21:21then filter digitally and throw away the extra samples.
21:27That's oversampling, and we'll meet it properly in lecture four.
First year: getting the wave back
21:33Now the other direction. We have the dots. How do we get a smooth wave back?
21:41You'll often see digital audio drawn as a staircase,
21:45the voltage holding each sample's value until the next one arrives.
21:50A digital to analogue converter can indeed produce a staircase first.
21:57But the sharp edges of those steps are made of frequencies above the Nyquist
22:04frequency: copies, called images, of our signal around fs, two fs, and so on.
22:12A low-pass filter after the converter,
22:15called the reconstruction or anti-imaging filter, removes them.
22:20What's left isn't an approximation of the original wave.
22:24Provided the original contained nothing above the Nyquist frequency,
22:29it is the original wave, apart from the rounding of each sample,
22:34which is next lecture's subject.
22:37There's a beautiful way to see why.
22:40Give every sample its own smooth bump, a shape called a sinc function,
22:48which is one at its own sample time and exactly zero at every other sample time.
22:55Scale each bump by its sample's value, and add them all up.
23:02The bumps fill in the gaps, and the sum passes through every sample
23:07while wiggling no faster than the Nyquist frequency allows.
23:12This is the Whittaker-Shannon interpolation formula,
23:15and it rebuilds a band-limited signal exactly from its samples.
23:21So here's a misconception to drop: digital audio is not steppy.
23:26Below the Nyquist frequency, sampling loses nothing at all.
23:31What it costs is everything above the Nyquist frequency,
23:37which is why we choose the rate to sit above the top of hearing.
23:42It also explains why more samples aren't automatically better.
23:48Once the sampling rate is safely above twice the highest frequency
23:53you care about, extra samples capture frequencies you can't hear.
23:58They make the filters easier to build, rather than the sound itself better.
Summary and what's next
24:05Today, we followed a sound into numbers and back.
24:10A signal is a voltage that follows a pressure wave.
24:15Sampling turns it into a list of numbers, taken fs times a second.
24:21Sample at more than twice the highest frequency,
24:25or frequencies fold back as aliases that can never be removed, so filter first.
24:32And with the right filter after the converter,
24:36the samples give back the original wave exactly.
24:41There's a practice set on the website, with worked answers, for both levels.
24:47Next time: bits and resolution.
24:51We'll round each sample to a fixed set of levels, find out what that costs,
24:57and see why 16 bits give a CD about 96 decibels of range. See you then.