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 periodfs= 1/Ts= sampling frequencyn= 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 frequencyfmax= 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:
- Increase the Sampling Rate – always meet or exceed
2*fmax. - Anti-Aliasing Filters – low-pass filters remove high-frequency components before sampling.
- 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.
hello
Done sirrrrr, diko sure if ganito ba ang subplot sa matlab
Done
Done
Done
eto po yung akin sir
DONE!
done
hello, sir hihihi
Done sir
Done
done po sir
done
done po sir
DONE SIR
Done