Quantization On Matlab
Quantization On Matlab
Roll No. 26
Lab Session 2
1. Effects of Quantization with variable precision levels
Simulate a DTCV sampled composite signal of 𝑓𝑑1=125 samples/sec and 𝑓𝑑2=150 samples/sec with
length of the signal be 250 samples. Take the desired number of significant digits from user as an
input. Then choose the method of Quantization (round-off, floor & ceil) and apply to the signal
generated above. Compute the quantization error signals and SQNR.
Matlab Code:
clear all; close all; clc;
N = 250;
fd = 1/125;
n = 0:249;
q = input('No. of Digits after decimal points to be retained (0-9): ');
x = cos(2*pi*fd*n);
Px = sum(abs(x).^2)/N;
a = input('Select the method of quantization, press 1 for round-off, 2 for
floor, and 3 for ceil: ');
if a == 1
xq = round(x*10^q)/10^q;
elseif a == 2
xq = floor(x*10^q)/10^q;
elseif a == 3
xq = ceil(x*10^q)/10^q;
end
xe = xq - x;
Pe = sum(abs(xe).^2)/N;
SQNR = 10*log10(Px/Pe);
disp(['The Signal to Quantization Noise Ratio is: ' num2str(SQNR) ' dB.']);
figure;
subplot(2,1,1);
stem(n, x, 'filled');
hold on;
stem(n, xq, 'r', 'filled');
grid;
xlabel('indices');
ylabel('Amp');
xlim([0 49]);
ylim([-2.1 2.1]);
legend('DTCV', 'DTDV');
subplot(2,1,2);
plot(n, xe, 'k', 'Linewidth', 2);
xlabel('indices');
ylabel('Error');
xlim([0 49]);
DSP LAB TASKS
Roll No. 26
Select the method of quantization, press 1 for round-off, 2 for floor, and 3 for ceil: 3
Select the method of quantization, press 1 for round-off, 2 for floor, and 3 for ceil: 2
Comments: When a signal is quantized, three methods of floor, round-off and ceil are employed
for approximation. Due to which, some errors are generated. Here in this code we have asked the user
for the number of decimal places to approximate the values and the method for approximation.
It is observed that the SQNR is higher for round-off as it generates the least error. SQNR varies inversely
with the power of error signal.
Comments: It can be observed as the bit depth is increased, the approximation of the values got
quite a lot better. A very minimal amount of error occurs at higher bit depths such as 7 or 8.
Comments: The voice recorded at 44.1k Hz sampling rate is quantized at different number of
bits.
The voice quality stopped improving at higher number of bit depths such as 8 and so on.