0% found this document useful (0 votes)
3 views16 pages

Lab 2

The document details a laboratory report on sampling, quantizing, and coding using MATLAB, performed by students Lê Tiến Phát and Nguyễn Ngọc Ngân. It includes a grading checklist, team contributions, a structured report with sections on fundamentals, experimental procedures, results, and a conclusion summarizing the work and challenges faced. The lab successfully demonstrated the analog-to-digital conversion process and the trade-offs involved in signal processing.

Uploaded by

ngannguyen999xxx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Lab 2

The document details a laboratory report on sampling, quantizing, and coding using MATLAB, performed by students Lê Tiến Phát and Nguyễn Ngọc Ngân. It includes a grading checklist, team contributions, a structured report with sections on fundamentals, experimental procedures, results, and a conclusion summarizing the work and challenges faced. The lab successfully demonstrated the analog-to-digital conversion process and the trade-offs involved in signal processing.

Uploaded by

ngannguyen999xxx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

International University

School of Electrical Engineering

Digital Signal Processing Laboratory


EE093IU

SAMPLING, QUANTIZING AND


CODING WITH MATLAB

Submitted by

Student name Student ID


Lê Tiến Phát ITITIU21273
Nguyễn Ngọc Ngân SESEIU21025

Date Performed: 28/03/2025


Date Submitted: 06/06/2025
Lab Section: Lab 2
Course Instructor: M. Eng Nguyen Minh Thien

International University EE093IU1 1


School of CSE
GRADING CHECKLIST

Number Content Satisfied? Score Comment


Format (max 9%)
- Font type Yes No
1
- Font size Yes No

- Lab title Yes No

- Page number Yes No

- Table of contents Yes No

- Header/Footer Yes No

- List of figures (if exists) Yes No

- List of tables (if exists) Yes No

- Lab report structure Yes No

2 English Grammar and Spelling (max 6%)


- Grammar Yes No

- Spelling Yes No

3 Data and Result Analysis (max 85%)

Total Score

Date:

Signature

International University EE093IU1 2


School of CSE
TEAM CONTRIBUTION
Task Le Tien Phat Nguyen Ngoc Ngan
Collaborated on all coding tasks, participated in problem-solving
Collaboration
discussions, and jointly tested MATLAB implementations
Lab preparation
Answer in-class questions
and experiment
Data analysis Jointly analyzed & verified the results for all problems
Write fundamental background & Write experiment procedure &
Report writing
Conclusion Results

International University EE093IU1 3


School of CSE
Table of Contents
DISCUSSION OF FUNDAMENTALS ......................................................................................... 5
EXPERIMENTAL PROCEDURE ................................................................................................. 7
EXPERIMENTAL RESULTS........................................................................................................ 8
CONCLUSION ............................................................................................................................. 16

List of Figures
Figure 1. Console output of Problem 1 9

Figure 2. Result of the program in Problem 1. 9

Figure 3. Console Output of the Problem 2 Program 11

Figure 4. Plotting of Problem 2 12

Figure 5. Sample Results logging for input of Problem 3 14

Figure 6. Output of Problem 3 Program 15

International University EE093IU1 4


School of CSE
Discussion of Fundamentals
Analog-to-Digital Conversion Process
The conversion of analog signals to digital form involves three essential sequential operations:
sampling, quantization, and coding. These processes form the foundation of all Analog-to-
Digital Converter (ADC) systems and determine the fidelity of digital signal representation.
Sampling Theory
Sampling converts a continuous-time signal x(t) into a discrete-time sequence by taking values at
regular intervals:
𝑥[𝑛] = 𝑥(𝑛𝑇𝑠 ) where 𝑛 = 0,1,2, . ..
Key parameters include:
• Sampling period: 𝑇𝑠 (time between samples)
1
• Sampling frequency: 𝑓𝑠 = 𝑇 (samples per second)
𝑠

Nyquist-Shannon Sampling Theorem: For perfect reconstruction of a bandlimited signal


containing frequencies up to 𝑓𝑚𝑎𝑥 :
𝑓𝑠 ≥ 2𝑓𝑚𝑎𝑥
When this condition is violated, aliasing occurs, where high-frequency components appear as
lower frequencies in the sampled signal.
Quantization Process
Quantization maps continuous amplitude values to a finite set of discrete levels:
Key Parameters:
• Number of levels: 𝐿 = 2𝐵 (where 𝐵 is the number of bits)
• Full-scale range: 𝑅 = 𝑥𝑚𝑎𝑥 − 𝑥𝑚𝑖𝑛
𝑅
• Quantization step: Δ = 𝐿

Quantization Error: 𝜀[𝑛] = 𝑥[𝑛] − 𝑥𝑞 [𝑛]


For uniform quantization:
Δ Δ
• Error bounds: − 2 ≤ 𝜀[𝑛] ≤ 2
Δ2
• Error variance: 𝜎𝜀2 = 12
𝑅
Signal-to-Quantization-Noise Ratio: For a sinusoidal input with peak amplitude 𝐴 = 2 :

𝑆𝑄𝑁𝑅 = 6.02𝐵 + 1.76 dB


This relationship shows that each additional bit improves SQNR by approximately 6 dB.

International University EE093IU1 5


School of CSE
Binary Coding
Binary coding assigns unique bit patterns to each quantization level. Common schemes include:
• Natural Binary: Direct binary representation
• Gray Code: Adjacent levels differ by one bit (reduces error impact)
• Two's Complement: For signed signal representation

International University EE093IU1 6


School of CSE
Experimental Procedure
Problem 1: 8-Level Quantization Implementation
Objective: Implement custom quantization for signal x(t) = 8cos(4πt)
Parameters:
• Sampling period: 𝑇𝑠 = 0.01 seconds
• Number of quantization levels: 𝐿 = 8
• Signal analysis: 𝑓 = 2 Hz, 𝑓𝑠 = 100 Hz (satisfies Nyquist criterion)
Procedure:
4𝜋𝑛
1. Generate sampled signal: 𝑥𝑠 (𝑛𝑇𝑠 ) = 8cos( 100 )
2. Implement custom quantization function with 8 levels
3. Create visualization showing original, sampled, and quantized signals
4. Analyze quantization effects and level utilization
Problem 2: Quantization Error Analysis and Binary Coding
Objective: Analyze quantization error characteristics and implement 3-bit binary coding
Tasks:
1. Calculate quantization error: 𝜀[𝑛] = 𝑥[𝑛] − 𝑥𝑞 [𝑛]
2. Analyze error statistics and distribution
Implement custom binary encoder using specified 3-bit codes
Procedure:
1. Plot original signal and aliased signal on the same graph
2. Add sample points to verify aliasing at discrete instants
3. Demonstrate that sample values are identical despite different continuous signals
4. Generate and analyze binary code sequence

Problem 3: Mathematical Proof of Sinusoid Aliasing


Objective: Develop flexible ADC simulator with user-defined parameters
Function Specification: 𝑦 = quantized_example(𝑥, 𝑅, 𝐵)
• 𝑥: Input signal vector
• 𝑅: Full-scale range (volts)
• 𝐵: Number of bits
• 𝑦: Quantized output
Test Cases:
1. Low Resolution: 𝑅 = 10V, 𝐵 = 3 bits (coarse quantization)

International University EE093IU1 7


School of CSE
2. Medium Resolution: 𝑅 = 10V, 𝐵 = 8 bits (practical performance)
3. High Resolution: 𝑅 = 10V, 𝐵 = 12 bits (precision applications)
Analysis Requirements:
• Compare theoretical vs. measured SQNR
• Evaluate performance scaling with bit depth
• Analyze input range effects and clipping behavior

Experimental Results

Problem 1:
Ts = 0.01;
n_levels = 8;
max_val = 8;

t = linspace(0, 1, 500);
n = 0:100;
x_original = 8*cos(4*pi*t);
x_sampled = 8*cos(4*pi*n/100);

[x_quantized, quant_levels] = quantize_signal(x_sampled, n_levels, max_val);

figure;
subplot(2,1,1);
plot(t, x_original); hold on;
stem(n*Ts, x_sampled, 'r');
title('Original and Sampled Signals');

subplot(2,1,2);
plot(t, x_original); hold on;
stem(n*Ts, x_quantized, 'g');
for i = 1:length(quant_levels)
line([0 1], [quant_levels(i) quant_levels(i)], 'LineStyle', '--', 'Color', 'k');
end
title('Original and Quantized Signals');

function [x_q, levels] = quantize_signal(x, n_levels, max_val)


step = (2 * max_val) / n_levels;
levels = -max_val:step:(max_val-step);
x_q = zeros(size(x));
for i = 1:length(x)
[~, idx] = min(abs(x(i) - levels));
x_q(i) = levels(idx);
end
end

Result

International University EE093IU1 8


School of CSE
Figure 1. Console output of Problem 1

Figure 2. Result of the program in Problem 1.

International University EE093IU1 9


School of CSE
Problem 2:
function lab2_problem2
Ts = 0.01;
n_levels = 8;
max_val = 8;

t = linspace(0, 0.5, 500);


n = 0:50;
x_original = 8*cos(4*pi*t);
x_sampled = 8*cos(4*pi*n/100);

[x_quantized, quant_levels] = quantize_signal(x_sampled, max_val, n_levels);


quant_error = x_sampled - x_quantized;
binary_codes = generate_binary_codes(x_quantized, quant_levels);

figure;
subplot(2, 1, 1);
plot(t, x_original, 'b-', 'LineWidth', 1.5); hold on;
stem(n*Ts, x_sampled, 'r.', 'MarkerSize', 10);
stem(n*Ts, x_quantized, 'go', 'MarkerSize', 6);
title('Original, Sampled, and Quantized Signals');
legend('Original x(t)', 'Sampled x[n]', 'Quantized x_q[n]');

subplot(2, 1, 2);
stem(n*Ts, quant_error, 'k.', 'MarkerSize', 10);
title('Quantization Error: \epsilon[n] = x[n] - x_q[n]');

step_size = (2 * max_val) / n_levels;


fprintf('Quantization step size (Δ): %.3f\n', step_size);
end

function [x_q, levels] = quantize_signal(x, max_val, n_levels)


step = (2 * max_val) / n_levels;
levels = (-max_val):step:(max_val-step);
x_q = zeros(size(x));

for i = 1:length(x)
if x(i) < -max_val
x(i) = -max_val;
elseif x(i) > max_val - step
x(i) = max_val - step;
end
[~, idx] = min(abs(x(i) - levels));
x_q(i) = levels(idx);
end
end

function binary_codes = generate_binary_codes(quant_signal, quant_levels)


binary_codes = cell(size(quant_signal));
for i = 1:length(quant_signal)
binary_codes{i} = get_binary_code(quant_signal(i), quant_levels);
end
end

International University EE093IU1 10


School of CSE
function code = get_binary_code(value, quant_levels)
[~, idx] = min(abs(value - quant_levels));
codes = {'100', '111', '110', '101', '000', '001', '010', '011'};
code = codes{idx};
end
Result

Figure 3. Console Output of the Problem 2 Program

International University EE093IU1 11


School of CSE
Figure 4. Plotting of Problem 2

International University EE093IU1 12


School of CSE
Problem 3:
function lab2_problem3
R = input('Enter the full-scale range (R) in Volts: ');
B = input('Enter the number of bits (B): ');

func_type = input('Function type (1-sine, 2-cosine, 3-sum): ');

switch func_type
case 1
A = input('Enter amplitude A: ');
w = input('Enter angular frequency w: ');
func_handle = @(t) A*sin(w*t);
case 2
A = input('Enter amplitude A: ');
w = input('Enter angular frequency w: ');
func_handle = @(t) A*cos(w*t);
case 3
num_terms = input('How many terms to add? ');
amplitudes = zeros(1, num_terms);
frequencies = zeros(1, num_terms);
term_types = zeros(1, num_terms);

for i = 1:num_terms
term_types(i) = input('Type (1-sine, 2-cosine): ') - 1;
amplitudes(i) = input('Enter amplitude A: ');
frequencies(i) = input('Enter angular frequency w: ');
end
func_handle = @(t) evaluate_sum(t, amplitudes, frequencies, term_types);
end

t_start = input('Enter start time: ');


t_end = input('Enter end time: ');
num_samples = input('Enter number of samples: ');

t_values = linspace(t_start, t_end, num_samples);


x_values = arrayfun(func_handle, t_values);
y_values = quantized_example(x_values, R, B);

figure;
plot(t_values, x_values, 'b-', 'LineWidth', 1.5); hold on;
stem(t_values, y_values, 'r.');
title(['Quantized Signal with R = ', num2str(R), ' V, B = ', num2str(B), ' bits']);
legend('Input Signal', 'Quantized Signal');

step_size = R / (2^B);
fprintf('Step size: %.6f Volts\n', step_size);
end

function y = quantized_example(x, R, B)
step = R / (2^B);
levels = (-R/2):step:(R/2-step);
y = zeros(size(x));

for i = 1:length(x)

International University EE093IU1 13


School of CSE
x(i) = max(-R/2, min(R/2, x(i)));
[~, idx] = min(abs(x(i) - levels));
y(i) = levels(idx);
end
end

function result = evaluate_sum(t, amplitudes, frequencies, term_types)


result = 0;
for i = 1:length(amplitudes)
if term_types(i) == 0
result = result + amplitudes(i) * sin(frequencies(i) * t);
else
result = result + amplitudes(i) * cos(frequencies(i) * t);
end
end
end
Result

Figure 5. Sample Results logging for input of Problem 3

International University EE093IU1 14


School of CSE
Figure 6. Output of Problem 3 Program

International University EE093IU1 15


School of CSE
Conclusion
Summary of Work
This lab successfully explored the complete analog-to-digital conversion process through three
comprehensive problems. We implemented custom quantization algorithms, analyzed
quantization error characteristics, and developed a general ADC simulator with configurable
parameters. The experimental work demonstrated the fundamental trade-offs between bit depth,
signal quality, and system complexity.
Difficulties and Solutions

The main challenges were signal range alignment and custom binary code mapping. Ensuring
the 8V cosine signal properly mapped to 8 quantization levels required careful step size
calculation. The custom binary coding scheme with non-sequential codes needed a direct lookup
table approach.

We solved these by implementing a get_binary_code() function with predefined code arrays


and using min(abs()) for nearest-level quantization, ensuring accurate level assignment and
code mapping.

Key Points

The lab confirmed that the 6.02 dB per bit relationship for SQNR is fundamental to quantization
system design. Quantization error behaves as approximately white noise for properly designed
systems, enabling predictable performance analysis. The choice of binary coding scheme affects
error resilience, with Gray coding providing superior single-bit error protection compared to
natural binary coding.
These concepts are essential for understanding ADC specifications and designing digital signal
processing systems. The relationship between resolution, dynamic range, and power
consumption guides practical ADC selection for applications ranging from audio processing to
precision measurement systems.

The collaborative approach enhanced our understanding of both theoretical quantization


principles and practical MATLAB implementation techniques essential for engineering practice.

THE END

International University EE093IU1 16


School of CSE

You might also like