PCM Implementation and SNR Analysis
This document details the implementation of Pulse Code Modulation (PCM) and analyzes the Signal-to-Noise Ratio (SNR) for different quantization levels.
PCM Code
clc;
clear all;
close all; % Closing any opened figures
% Plotting the offset sinusoidal signal
time = 0:.0005:.05;
freq_msg = 100; % Waveform frequency
dc_ofst = 2; % Signal offset
signal = sin(2*pi*freq_msg*time) + dc_ofst; % Generating the signal
% Plotting the signal
figure;
plot(time, signal);
xlabel('Time');
ylabel('Amplitude');
title('Signal');
% Sampling the signal
freq_sample = 15 * freq_msg; % Sampling frequency
samp_time = 0:1/freq_sample:0.05; % Sampling time
samp_signal = dc_ofst + sin(2*pi*freq_msg*samp_time); % Generating the sampled signal
hold on
plot(samp_time, samp_signal, 'rx') % Plotting the sampled signal
title('Sampled Signal')
legend('Original signal', 'Sampled signal');
% Uniform Quantizer
L = 8; % Number of Quantization levels
smin = round(min(signal));
smax = round(max(signal));
Quant_levl = linspace(smin, smax, L); % Length L, to represent L+1 intervals
codebook = linspace(0, smax, L+1); % Length L+1, one entry for each interval
[index, quants] = quantiz(samp_signal, Quant_levl, codebook); % Quantize.
figure;
plot(samp_time, samp_signal, 'x', samp_time, quants, '.-') % Plotting sampled signal and quantization level
title('Quantized Signal')
legend('Original signal', 'Quantized signal');
figure;
plot(samp_time, index, '.-') % Plotting quantization levels of input signal
title('Encoded Signal');
% Binary coding
for i = 1:length(index)
bincode_sig{i} = dec2bin(round(index(i)), 3);
end
disp('Binary encoded signal');
disp(bincode_sig)
% SNR ratio calculation
noise = quants - samp_signal; % Calculating noise
figure;
plot(samp_time, noise, '.-'); % Plotting figure
title('Noise');
r = snr(index, noise); % SNR
snr1 = ['SNR :', num2str(r)];
disp(snr1)
% Function for plotting Quant_level vs SNR
function [r] = IMPL_Quant(l, b)
% Plotting the offset sinusoidal signal
time = 0:.0005:.05;
freq_msg = 100; % Waveform frequency
dc_ofst = 2; % Signal offset
signal = sin(2*pi*freq_msg*time) + dc_ofst; % Generating the signal
% Sampling the signal
freq_sample = 15 * freq_msg; % Sampling frequency
samp_time = 0:1/freq_sample:0.05; % Sampling time
samp_signal = dc_ofst + sin(2*pi*freq_msg*samp_time); % Generating the sampled signal
% Uniform Quantizer
L = l; % Number of Quantization levels
smin = round(min(signal));
smax = round(max(signal));
Quant_levl = linspace(smin, smax, L); % Length L, to represent L+1 intervals
codebook = linspace(0.7, smax, L+1); % Length L+1, one entry for each interval
[index, quants] = quantiz(samp_signal, Quant_levl, codebook);
% Quantize.
% Binary coding
for i = 1:length(quants)
bincode_sig{i} = dec2bin(round(quants(i)), b);
end
% SNR ratio calculation
noise = quants - samp_signal; % Calculating noise
r = snr(index, noise); % SNR
end
% PCM SNR PLOT
% Program for plotting quantization level vs SNR
clc;
clear all;
close all;
l = [8, 16, 32, 64, 128]; % Defining different levels
b = [3, 4, 5, 6, 7];
for i = 1:length(l)
r(i) = IMPL_Quant(l(i), b(i)); % Calling the function
end
% Plotting
figure;
plot(l, r);
xlabel('L');
ylabel('SNR');
title('L vs SNR');