EXPERIMENT #3
EXAMINATION OF SAMPLING, QUANTIZATION, AND PCM
ENCODING IN MATLAB
OBJECTIVE:
In this lab, you will explore the concepts of sampling, quantization, and PCM (Pulse Code Modulation)
encoding. MATLAB will be used to perform the experiments and analyze the results. This lab is designed
to help you understand the practical aspects of digital signal processing.
Matlab code:
t = 0:0.001:2;
x_t = sin(2*pi*40*t) + 0.5*cos(2*pi*60*t);
figure;
plot(t, x_t, 'LineWidth', 1.2);
grid on;
xlabel('Time (s)'); ylabel('x(t)');
title('Continuous-Time Signal x(t) = sin(2\pit) + 0.5cos(2\pit)');
Plot:
Matlab code:
Fs = 8000;
n = 0:1/Fs:2;
x_n = sin(2*pi*1000*n) + 0.5*cos(2*pi*2000*n);
figure;
stem(n, x_n, 'filled');
grid on;
xlabel('n (sample index)');
ylabel('x[n]');
title('Discrete-Time Signal x[n] Sampled at Fs = 8000 Hz');
xlim([0 0.02]);
Plot:
Matlab code:
clc; clear; close all
t = 0:0.001:2;
x_t = sin(2*pi*1000*t) + 0.5*cos(2*pi*2000*t);
Fs_values = [4000 2000 1000];
figure;
for i = 1:length(Fs_values)
Fs = Fs_values(i);
n = 0:1/Fs:2;
x_n = sin(2*pi*1000*n) + 0.5*cos(2*pi*2000*n);
subplot(3,1,i);
stem(n, x_n, 'filled');
grid on;
xlabel('n (sample index)');
ylabel('x[n]');
title(['Discrete-Time Signal at Fs = ' num2str(Fs)
' Hz']);
xlim([0 0.02]);
end
Plot
Comments:
Part 2: QUANTIZATION
Matlab code:
B = 4;
Fs = 4000;
n = 0:1/Fs:2;
x_n = sin(2*pi*1000*n) + 0.5*cos(2*pi*2000*n);
x_q = quantiz(x_n, -1:2/(2^B):1);
figure;
stem(n, x_q, 'filled');
grid on;
xlabel('Sample Index (n)');
ylabel('Amplitude');
title('Quantized Signal x_q[n]');
xlim([0 0.02]);
Plot:
Matlab code:
clc; clear; close all
Fs = 8000;
n = 0:1/Fs:2;
x_n = sin(2*pi*1000*n) + 0.5*cos(2*pi*2000*n);
B_values = [2 6 8];
figure;
for i = 1:length(B_values)
B = B_values(i);
x_q = quantiz(x_n, -1:2/(2^B):1);
% Plot
subplot(3,1,i);
stem(n, x_q, 'filled');
grid on;
xlabel('Sample Index (n)');
ylabel('Amplitude');
title(['Quantized Signal x_q[n] with B = ' num2str(B) '
bits']);
xlim([0 0.02]);
end
Plot:
Comments:
Part 3: PCM ENCODING
Matlab code:
clc;
clear;
close all;
N = 40;
Fs = 20;
n = (0:N-1)/Fs;
x_n = sin(2*pi*1*n) + 0.5*cos(2*pi*2*n);
B = 4;
x_q = quantiz(x_n, -1:2/(2^B-1):1);
x_q_norm = x_q / max(abs(x_q));
L = 16;
x_q_pcm = quantiz(x_q_norm, -1:2/(L-1):1);
figure;
stairs(n,x_q_pcm);
xlabel('Sample Index (n)');
ylabel('PCM Level');
title('PCM Encoded Signal (L = 16)');
grid on;
plot:
Matlab code:
clc;
clear;
close all;
N = 40;
Fs = 20;
n = (0:N-1)/Fs;
x_n = sin(2*pi*1*n) + 0.5*cos(2*pi*2*n);
B = 4;
x_q = quantiz(x_n, -1:2/(2^B-1):1);
x_q_norm = x_q / max(abs(x_q));
L_values = [8 32 64];
figure;
for i = 1:length(L_values)
L = L_values(i);
x_q_pcm = quantiz(x_q_norm, -1:2/(L-1):1);
subplot(3,1,i);
stairs(n, x_q_pcm);
xlabel('Sample Index (n)');
ylabel('PCM Level');
title(['PCM Encoded Signal (L = ' num2str(L) ')']);
grid on;
end
Plot:
Comments:
TASK
FOR AUDIO SIGNAL
SAMPLING
load('train') % load the variables for the 'Train whistle' audio file
% this loads the sample frequency and the sample values
t = 0:1/Fs:length(y)/Fs-1/Fs; % time index
M = 5; % downsample factor
y_down = y(1:M:end); % keep every M-th sample
t3 = 0:M/Fs:length(y)/Fs-1/Fs;
subplot(2,1,1);
plot(t, y);
hold on;
subplot(2,1,2);
plot(t3, y_down);
hold on;
xlabel('Time (s)');
ylabel('y');
sound(y) % Original sound
sound(y_down) % Sound after sampling
QUANTIZATION:
load('train');
levels1 = 4; % Number of quantization levels (Level 1)
levels2 = 8; % Number of quantization levels (Level 2)
% Normalize the signal
y_norm = y / max(abs(y));
% Quantize the normalized signal using different levels
y_quantized1 = round(y_norm * (levels1 - 1)) / (levels1 - 1);
y_quantized2 = round(y_norm * (levels2 - 1)) / (levels2 - 1);
% Plot the original signal and the quantized signals
t = 0:1/Fs:length(y)/Fs-1/Fs;
subplot(3,1,1);
plot(t, y);
title('Original Audio Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3,1,2);
plot(t, y_quantized1);
title(sprintf('Quantized Signal (Levels: %d)', levels1));
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3,1,3);
plot(t, y_quantized2);
title(sprintf('Quantized Signal (Levels: %d)', levels2));
xlabel('Time (s)');
ylabel('Amplitude');
PCM Encoding:
load('train');
bits1 = 2; % Number of bits for quantization (Level 1)
bits2 = 4; % Number of bits for quantization (Level 2)
bits3 = 6; % Number of bits for quantization (Level 3)
y_norm = y / max(abs(y));
y_quantized1 = round(y_norm * (2^bits1 - 1)) / (2^bits1 - 1);
y_quantized2 = round(y_norm * (2^bits2 - 1)) / (2^bits2 - 1);
y_quantized3 = round(y_norm * (2^bits3 - 1)) / (2^bits3 - 1);
% Perform PCM encoding
pcm_codes1 = round(y_quantized1 * (2^bits1 - 1));
pcm_codes2 = round(y_quantized2 * (2^bits2 - 1));
pcm_codes3 = round(y_quantized3 * (2^bits3 - 1));
% Plot the original signal and PCM encoded signals
t = 0:1/Fs:length(y)/Fs-1/Fs;
subplot(3,1,1);
plot(t, y);
title('Original Signal');
subplot(3,1,2);
plot(t, pcm_codes1);
title('PCM Encoded (2 bits)');
subplot(3,1,3);
plot(t, pcm_codes2);
title('PCM Encoded (4 bits)');