Now that we’re familiar with notion of frequency, we can return to the question which was posed earlier in the section on aliasing. Namely, how does one choose a sampling rate (or period) which will avoid undersampling and oversampling, and still faithfully represent the signal being sampled? As it turns out, there is a theorem which tells us how to precisely calculate the proper sampling rate and it’s known as the Nyquist-Shannon Sampling Theorem.1 The theorem is stated as follows,

The Nyquist-Shannon Sampling Theorem

If a signal contains no frequencies higher than B hertz, it is completely determined by giving its ordinates at a series of points [samples] spaced 1/(2B) seconds apart.

For example, if we are attempting to sample a 300 Hz sine wave, the Nyquist-Shannon Theorem tells us that we must sample at a rate greater than 600 Hz to faithfully capture the incoming sinusoid. If you know what the highest frequency component in your signal is, you simply set your sampling rate to be greater than twice that frequency.

This formulation of the theorem is quite signal-specific. You can also choose to look at it from a more sampling-rate-centric viewpoint,

Given a sampling rate of FS hertz, you cannot accurately sample any signal containing frequencies greater than or equal to FS/2.

I sometimes find this interpretation easier to think about, since it can be confusing for the uninitiated to think about determining the "frequency content" of a signal without first understanding the DFT. This formulation is also helpful because it introduces the value of FS/2 (half the sampling rate) as being key. This value is so crucial to sampling theory that it’s often referred to using the special name of the Nyquist Limit or Nyquist Frequency.

Figure 1 shows a set of four continuous sine waves which are all being sampled at the same rate. You can adjust the sampling rate using the slider at the bottom of the visualization. Spend enough time with this visualization and you’ll notice some very strange properties of the sampling process. One such oddity will occur when you set the sampling rate to 4 Hz. In this case, the sampled version of the 3 Hz sinusoid will look exactly like the sampled version of the 1 Hz sinusoid except for being flipped over the x-axis. When the sampling rate is set to 2 Hz, all four sampled signals will look exactly the same.

Figure 1.  Playing with the Sampling Period

Recall for a moment the section on sound and perception. In that section we mentioned that the range of human hearing runs from about 20 Hz to 20,000 Hz. Since humans can’t actually hear frequency content above 20,000 Hz, frequencies above this limit are actively removed from most audio and music signals before they are sampled. Given what we now know about the Sampling Theorem, you won’t be surprised to hear that the most common sampling rate for audio and music signals is around 40,000 Hz, or twice the highest audible frequency.2

1. Claude Shannon is a really interesting guy. He wrote equations for juggling, invented the Most Useless Machine Ever, and even built a flame-throwing trumpet. Interestingly, he was also the thesis advisor to Ivan Sutherland, creator of the famous Sketchpad computing environment. When you’re sick of DSP, you should watch Claude Shannon demo Theseus and watch Alan Kay talk about Sketchpad.

Sampling is exceptionally tricky, and as with anything, there is more to the story than meets the eye. I recommend this article by Tim Wescott for a more in-depth and pragmatic approach to thinking about the Nyquist Limit and sampling in general.

2. The sampling rate is actually 44,100 Hz. This is for purely historical reasons which are largely related to broadcasting.