Understanding Sampling and Aliasing in Digital Signal Processing (DSP) with MATLAB Examples

Digital Signal Processing (DSP) is a fundamental field in engineering that allows computers and embedded systems to analyze, manipulate, and store real-world analog signals in digital form. To work with digital signals effectively, understanding sampling and aliasing is essential.

This guide provides a detailed explanation of these concepts, practical MATLAB examples, and methods to prevent common signal distortions.


What is Sampling?

Sampling is the process of measuring the amplitude of a continuous analog signal at regular time intervals. This converts the continuous-time signal (x(t)) into a discrete-time signal (x[n]):

x[n] = x(nTs)

Where:

  • Ts = sampling period
  • fs = 1/Ts = sampling frequency
  • n = sample index

Sampling allows digital devices like Analog-to-Digital Converters (ADC), data acquisition systems, and digital measurement instruments to represent analog signals numerically.


The Nyquist-Shannon Sampling Theorem

The Nyquist-Shannon Sampling Theorem is a cornerstone of DSP. It states:

A signal must be sampled at least twice the maximum frequency component in order to be accurately reconstructed.

Mathematically:

fs >= 2 * fmax
  • fS= sampling frequency
  • fmax = maximum signal frequency

The Nyquist Rate (2*fmax) defines the minimum sampling frequency required. Sampling below this rate can result in aliasing, a form of distortion that makes high-frequency components appear as lower frequencies.


What is Aliasing?

Aliasing occurs when a signal is sampled below its Nyquist Rate. In this scenario:

  • High-frequency components fold back into the lower frequency range.
  • The reconstructed digital signal is distorted.
  • Some information from the original analog signal is permanently lost.

Aliasing is particularly critical in:

  • Audio processing – where distorted samples reduce sound quality
  • Wireless communications – where overlapping frequencies can cause errors
  • Image and video processing – leading to moiré patterns or visual artifacts
  • Biomedical signal monitoring – misrepresenting vital data from ECG or EEG signals

Methods to prevent aliasing:

  1. Increase the Sampling Rate – always meet or exceed 2*fmax.
  2. Anti-Aliasing Filters – low-pass filters remove high-frequency components before sampling.
  3. Oversampling – sampling at a much higher rate than Nyquist reduces quantization noise and simplifies filtering.

MATLAB Examples

1. Sampling a Sine Wave

clc; clear; close all;

% Continuous signal
f = 5;                % signal frequency (Hz)
t = 0:0.001:1;        % time vector
x = sin(2*pi*f*t);    % continuous signal

% Sampling
fs = 20;              % sampling frequency
ts = 0:1/fs:1;
xs = sin(2*pi*f*ts);  % sampled signal

% Plot
figure;
plot(t,x,'b','LineWidth',1.5)
hold on
stem(ts,xs,'r','filled')
title('Sampling of a Sine Wave')
xlabel('Time (s)')
ylabel('Amplitude')
legend('Continuous Signal','Sampled Signal')
grid on;

Explanation: This example shows a 5 Hz sine wave sampled at 20 Hz, which meets the Nyquist criterion. The sampled signal aligns accurately with the continuous waveform.


2. Aliasing Demonstration

clc; clear; close all;

% Signal parameters
f = 10;                % signal frequency
fs = 12;               % sampling frequency below Nyquist

t = 0:0.001:1;
ts = 0:1/fs:1;

x = sin(2*pi*f*t);     
xs = sin(2*pi*f*ts);   

% Plot
figure;
plot(t,x,'b','LineWidth',1.5)
hold on
stem(ts,xs,'r','filled')
title('Aliasing Demonstration')
xlabel('Time (s)')
ylabel('Amplitude')
legend('Original Signal','Sampled Signal')
grid on;

Explanation: A 10 Hz signal sampled at 12 Hz (less than Nyquist rate of 20 Hz) exhibits aliasing. The sampled points no longer represent the true waveform.


3. Oversampling a Signal

clc; clear; close all;

f = 5;
t = 0:0.001:1;
x = sin(2*pi*f*t);

fs = 100;                 % significantly higher than Nyquist
ts = 0:1/fs:1;
xs = sin(2*pi*f*ts);

figure;
plot(t,x,'b','LineWidth',1.5)
hold on
stem(ts,xs,'r','filled')
title('Oversampling of a Sine Wave')
xlabel('Time (s)')
ylabel('Amplitude')
legend('Continuous Signal','Oversampled Signal')
grid on;

Explanation: Sampling well above the Nyquist rate produces a very accurate digital representation and reduces quantization noise.


4. Anti-Aliasing Filter Example

clc; clear; close all;

f1 = 5; f2 = 25;
t = 0:0.001:1;
x = sin(2*pi*f1*t) + sin(2*pi*f2*t);

fs = 20;
ts = 0:1/fs:1;

fc = fs/2;                
[b,a] = butter(5, fc/(fs*0.5));  
x_filtered = filter(b,a,x);

xs_original = x(1:round(1/ts(2)):end);   
xs_filtered = x_filtered(1:round(1/ts(2)):end); 

figure;
subplot(2,1,1)
stem(ts, xs_original,'r','filled')
title('Aliased Signal Without Filter')
xlabel('Time (s)'); ylabel('Amplitude'); grid on;

subplot(2,1,2)
stem(ts, xs_filtered,'b','filled')
title('Signal Sampled After Anti-Aliasing Filter')
xlabel('Time (s)'); ylabel('Amplitude'); grid on;

Explanation: The high-frequency component (25 Hz) is removed using a low-pass filter before sampling. This prevents aliasing and preserves signal integrity.


5. Frequency Domain Visualization (FFT)

clc; clear; close all;

f = 12;
fs = 20;
t = 0:0.001:1;
x = sin(2*pi*f*t);
xs = sin(2*pi*f*(0:1/fs:1));

N = length(xs);
Xf = fft(xs);
f_axis = (0:N-1)*(fs/N);

figure;
stem(f_axis, abs(Xf))
title('Frequency Spectrum of Sampled Signal')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
grid on;

Explanation: The FFT shows how aliasing introduces incorrect frequency components when the sampling frequency is below Nyquist.


Practical Applications

  • Digital audio systems – CDs, streaming audio
  • Wireless communications – Wi-Fi, 4G/5G
  • Radar and sonar – detecting moving objects
  • Medical monitoring – ECG, EEG, MRI signal sampling
  • Image and video acquisition – cameras, CCTV systems

Conclusion

  • Sampling converts analog signals into digital sequences for processing.
  • Aliasing occurs when the sampling rate is too low, causing distortion and loss of information.
  • Using the Nyquist rate, anti-aliasing filters, and oversampling ensures accurate digital representation.
  • MATLAB simulations provide a practical way to visualize these DSP concepts and experiment with sampling strategies.

By understanding and applying these principles, engineers can design systems that accurately capture and process real-world signals in the digital domain.

5 1 vote
Article Rating
Subscribe
Notify of
16 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ronald Florendo jr.
18 days ago

hello

Lyka Barruga
18 days ago

Done sirrrrr, diko sure if ganito ba ang subplot sa matlab

IMG_0464
Kenshin Maraggun
18 days ago

Done

1000007596
Nico Lim
18 days ago

Done

MATLAB-03-13-2026_05_31_PM
Psalm Perez
18 days ago

Done

Screenshot-2026-03-13-173231
renz santillan
18 days ago

eto po yung akin sir

att.r-Kq0IgVZeHOsC2IKvRzguOEO46E2L-yj0bQRJd-FMQ
Gemini Almoite
18 days ago

DONE!

5ec7a93b-3678-416e-826e-6e2528b08afc
Nimrod Reuel Taloza
18 days ago

done

Screenshot-2026-03-13-180439
Lester Datuin
18 days ago

hello, sir hihihi

dsp
Darlene Joyce Taguibao
18 days ago

Done sir

Screenshot-64
Edmar CARAANG
16 days ago

Done

inbound6149421023795493261
Carl Aiscee Daquioag
16 days ago

done po sir

7684
Charlyn Mae Padre
16 days ago

done

inbound4318845930920547364
Ronald Florendo jr.
16 days ago

done po sir

Screenshot_15-3-2026_223658_matlab.mathworks.com
John Andrei Irorita
11 days ago

DONE SIR

inbound6626061530540605670
Agra
10 days ago

Done

1000010302
0
Would love your thoughts, please comment.x
()
x
Update cookies preferences