0% found this document useful (0 votes)
60 views5 pages

Quantization On Matlab

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

Quantization On Matlab

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

DSP LAB TASKS

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

For fd = 1/125 cycles/sample:


No. of Digits after decimal points to be retained (0-9): 4

Select the method of quantization, press 1 for round-off, 2 for floor, and 3 for ceil: 3

The Signal to Quantization Noise Ratio is: 81.8205 dB.

For fd = 1/150 cycles/sample:


No. of Digits after decimal points to be retained (0-9): 4

Select the method of quantization, press 1 for round-off, 2 for floor, and 3 for ceil: 2

The Signal to Quantization Noise Ratio is: 81.7293 dB.


DSP LAB TASKS
Roll No. 26

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.

2. Simple sinusoid quantized to various bits per sample


Generate a 100 Hz sinusoid sampled at 10000 samples/sec and quantized at 1_bit/sample.
Now increase the bit depth for various numbers of bits per sample (2, 3, 4, 5, 6, 7, 8) and
attach plots. You can use two column format for plotting (but the diagrams should be
visible).
Matlab Code:

Method used for Quantization: Round off


No. of bits = 1 No. of bits = 2

No. of bits = 3 No. of bits = 4

No. of bits = 5 No. of bits = 6

No. of bits = 7 No. of bits = 8


DSP LAB TASKS
Roll No. 26

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.

3. Audio signal quantization to various bits per sample


Use your recorded voice in last session and quantize it at 1 bit /sample. Change bit depth
to 2, 3, 4 and then listen and take notes of your observations. Decide no. of bits for audio
until quality stops improving.
Matlab Code:
clear all, close all, clc;

file_path = 'D:\quick access\5th semester\DSP\output44.1k.wav'; [x,


Fs] = audioread(file_path);
bit_depths = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

% Listening and note-taking


for i = 1:length(bit_depths)
% Quantize the audio signal
x_quantized = quantize_audio(x, bit_depths(i));

% Save quantized audio to a file


output_file = sprintf('quantized_audio_%dbits.wav', bit_depths(i));
audiowrite(output_file, x_quantized, Fs);
end

function x_quantized = quantize_audio(x, num_bits)


% Scale the audio signal to the range [-1, 1]
x_scaled = x / max(abs(x));

% Quantize to the specified number of levels


num_levels = 2^num_bits;
x_quantized = round(x_scaled * (num_levels - 1)) / (num_levels - 1);

% Rescale the signal back to its original range


x_quantized = x_quantized * max(abs(x));
end
DSP LAB TASKS
Roll No. 26

Comments: The voice recorded at 44.1k Hz sampling rate is quantized at different number of
bits.

Bit Depth Remarks


1 Voice totally lost. Only a spark was heard.
2 60% of the voice captured. Remaining lost
3 Whole voice is captured. But still a lot of Noise.
4 The Noise starts to reduce now.

The voice quality stopped improving at higher number of bit depths such as 8 and so on.

You might also like