This is the ninth post in the blinkdagger signal processing series.

Matlab Logo There is often confusion and misunderstanding when it comes to the Sampling Theorem. In this post, we aim to “blinkdagger” the topic and make it easy to undersand for the masses. The basic idea of the sampling theorem states that:

An analog signal can be reconstructed perfectly if it is sampled at a rate that is at least twice the frequency of the highest frequency content within the analog signal.

Whew, that was a mouthful. Read on to learn more about this theorem and the consequences when it is not obeyed.

Contents

What is Sampling?

What is sampling? We hear the term thrown around all the time, but it may be advantageous to define it here. Sampling basically means that we are taking an analog signal (continuous) and converting it into a digital signal (discrete). When we “take data” or “measure a signal”, we are in essense taking an analog signal and digitizing it. An analog signal has an infinite number of data points, wherein a digital signal has a set number of data points that is based on the sampling rate used and the duration of the data collection.

Kicking Off with an Example

Let’s start with a sine wave that has two fundamental frequencies at f1 = 10 Hz and f2 = 20 Hz (shown below):

Sine Wave Equation

f1 = 10;   %first fundamental frequency
f2 = 20;   %second fundamental frequency

FsBig = 1000; %a sampling rate of 1000 Hz will make the signal smooth
TsSmall = 1/FsBig; %this is the sampling period
tLong = 0:TsSmall:0.6-TsSmall; %this is the time vector
yLong = 2*sin(2*pi*f1*tLong) + 2*sin(2*pi*f2*tLong); %this is the signal vector

%plot the curve in the time domain
sineWave = figure;
plot(tLong,yLong)

Sine Wave with Frequency at 10 Hz and 20 Hz

Now, let’s also take the fft of this signal and check out the amplitude in the frequency spectrum. Just as we would expect, we see peaks at +/- 10 Hz and at +/- 20 Hz (If you don’t understand how the frequency spectrum is obtained, please refer to the Introductory FFT tutorial). Nothing too crazy as you can see.

%plot frequency spectrum (amplitude)
fftSineWave = figure;

%centeredFFT is a custom function. available for download at end of tutorial
[Y,freqLong] = centeredFFT(yLong,FsBig);
stem(freqLong,abs(Y),'MarkerFaceColor',[0.25 0.75 0.25])
xlim([-25,25]) %set the x-axis limit

Frequency Spectrum of Sine Wave

Example 2: Sampling at Fs = 50 Hz

In this example, we are going to sample the above signal at Fs = 50 Hz. First, note that Fs = 50 Hz is greater that twice the largest frequency inherent in the example sine wave. To refresh your memory, the sample sine wave has two frequencies, f1 = 10 Hz and f2 = 20 Hz. Fs is twice as large as 2*20 Hz. So according to the sampling theorem, I am safe! Let’s see what happens anyways.

As you can see, the little squares represent a sampled point. The resulting curve looks a lot more jagged doesn’t it? Are we sure we can reconstruct the blue curve (the original curve) using this sampling rate?

%plot the curve in the time domain
%overlap with sampled signal

Fs1 = 50; %sampling rate, it's greater than 20*2!
Ts1 = 1/Fs1; %sampling period
t1 = 0:Ts1:0.6 - Ts1; %time vector
y1 = 2*sin(2*pi*f1*t1) + 2*sin(2*pi*f2*t1); %sampled signal

sampling1 = figure;
plot(tLong,yLong) %plot the original signal
hold on
plot(t1,y1,'--rx') %plot the sampled signal

Sine Wave Sampled at 50 Hz

Looking at the frequency spectrum gives us a good idea that everything is intact. If you compare the frequency spectrums from above with the one shown directly below, you will notice that they are identical! So far, so good!

%plot frequency spectrum (amplitude)
fftSampling1 = figure;
[Y1,freq1] = centeredFFT(y1,Fs1);
stem(freq1,abs(Y1),'MarkerFaceColor',[0.25 0.75 0.25])
xlim([-25,25]) %set the x-axis limits

Frequency Spectrum of Sine Wave sampled at 50 Hz

Example 3: Sampling at Fs = 25 Hz

In this example, we are going to sample the above signal at Fs = 25 Hz. First, note that Fs = 25 Hz is NOT greater that twice the largest frequency inherent in the example sine wave. Let’s see what happens when we sample at Fs = 25 Hz.

Compared to the previous sampled signal, we seemed to have lost a lot of information. The signal barely resembles the original signal!! You can begin to see that if you don’t sample at a high enough rate, bad things will happen.

Fs2 = 25; %sampling rate, notice it's less than 20*2!
Ts2 = 1/Fs2; %sampling period
t2 = 0:Ts2:0.6 - Ts2; %time vector
y2 = 2*sin(2*pi*f1*t2) + 2*sin(2*pi*f2*t2);%sampled signal

sampling2 = figure;
plot(tLong,yLong)%plot the original signal
hold on
plot(t2,y2,'--rx') %plot the sampled signal

Sine Wave Sampled at 25 Hz

Okay, so you might have thought that the previous sampled signal didn’t really resemble our original curve either. Maybe there’s a slight chance that this ragged collection of points will give us the same frequency spectrum. As you can see from the picture below, the frequency spectrum is not the same. Instead of seeing peaks at +/- 10 Hz and at +/- 20 Hz, we see peaks at +/- 5 Hz and at +/- 10 Hz. In fact, if you look at the frequency axis more closely, you will notice that the data doesn’t even extend past 15 Hz (12.5 Hz to be exact)! Since our sampling rate for this signal was 25 Hz, the maximum frequency band shown is -Fs/2 to Fs/2, or -12.5 Hz to 12.5 Hz.

%plot frequency spectrum (amplitude)
fftSampling2 = figure;
[Y2,fre2] = centeredFFT(y2,Fs2);
stem(fre2,abs(Y2),'MarkerFaceColor',[0.25 0.75 0.25])
xlim([-25,25])

Frequency Spectrum of Sine Wave sampled at 25 Hz - Aliasing

Conclusion - Aliasing is NOT Your Friend

The phenomemon witnessed above is known as aliasing. This happens when the nyquist/shannon sampling theorem is not satisfied. So remember to use a sampling rate that is twice as large as the highest frequency content in your signal. When in doubt, use a sampling rate that is higher than you need. Data acquisition and data processing may take longer, so make sure that you choose a reasonable sampling rate that will prevent aliasing but will keep the data size manageable.

Download Source Files

Download the Source Files Here!