P1 - 16 and Quantization... DC

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

ECE-S352-701 DSP Problem 1.

16

Solution for Problem 1.16 and some commentary on quantization and Matlab

Prior to solving the problem we will first define two functions, one that performs
quantization with truncation and the other with rounding.

function [q_value, l_value] = t_quantize(max, min, L, c_value)


% This function returns the equivalent analog value (q_value) for the
% qunatization by truncation as well as the bin sequence number (l-value).
% Input data is as follows:
% max := the maximum value of the continuous signal
% min := the minimum value of the continuous signal
% L = # of qunatization levels
Delta = (max-min)/(L-1); % the quantization step
q_value = c_value - mod(c_value, Delta);
l_value = q_value/Delta;
end

function [q_value, l_value] = r_quantize(max, min, L, c_value)


% This function returns the equivalent analog value (q_value) for the
% qunatization by rounding as well as the bin sequence number (l-value).
% Input data is as follows:
% max := the maximum value of the continuous signal
% min := the minimum value of the continuous signal
% L = # of qunatization levels
Delta = (max-min)/(L-1); % the quantization step
q_value = (c_value+Delta/2) - mod((c_value+Delta/2), Delta);
l_value = q_value/Delta;
end

Note that the above functions return two variables and have 4 input variables. The key to
both of these functions is the use of the "mod" function. Mod returns the remainder of
the first argument (continuous amplitude value) divided by the second argument
(quantization step). Mod doesn't tell us the quotient only the remainder. By subtracting
the remainder (i.e., the value returned by mod) from the continuous analog value we
obtain a value corresponding to one of the amplitude lines separated by the quantization
steps. For quantization with truncation, we just do as described. For quantization with
rounding, we add half of the quantization step to both the value used in the mod function
and that of the c_value from which it is subtracted. Note also we compute l_value which
is the logical level (-n 0 +n) for quantization. This is not used in this problem.

The results of trying these functions on L = 11, xmin = 0, xmax = 1 on the value 0.57
results in the following:
EDU [q_value, l_value] = r_quantize(1,0,11,.57) Rounding
q_value = 0.6000
l_value = 6.0000

EDU [q_value, l_value] = t_quantize(1,0,11,.57) Truncating


q_value =0.5000
l_value = 5
which is the expected result for rounding and truncation.

Can you find an alternative method to perform both of these quantization methods?

DSP Problem 1.16 page 1 of 8


Tom Chmielewski 6/25/01
ECE-S352-701 DSP Problem 1.16

An interesting result is to show how each of the quantization methods map continuous
numbers to their corresponding quantized values. The following file will generate a
transfer plot which shows the relationship for both quantization types.
% file: quant_xfer.m
% This file generates quantization transfer curves for
% both truncation and rounding
L = 11
xmax = 1
xmin = -1
Delta = (xmax-xmin)/(L-1)
xn = xmin:.01:xmax;
xqt = [];
% now quantize the data set with truncation
N = max(size(xn))
for i = 1:N
[q_value,l_value] = t_quantize(xmax, xmin, L, xn(i));
xqt = [xqt, q_value];
end
xqr = [];
% now quantize the data set with rounding
for i = 1:N
[q_value,l_value] = r_quantize(xmax, xmin, L, xn(i));
xqr = [xqr, q_value];
end
plot(xn,xqt,'b')
axis square
axis ([-1, 1, -1, 1])
hold
plot(xn,xqr,'r')
text(-.9,.8,'red = rounded quatization')
text(-.9,.7, 'blue = truncated')
dd= sprintf('Delta = %d',Delta);
text(-.9,.6,dd)
xlabel('continuous value input')
ylabel('quantized value output')
% add marker from 0 to .1
for i = -1:.01:1
plot(0,i,'m')
plot(0.1,i,'c')
end

DSP Problem 1.16 page 2 of 8


Tom Chmielewski 6/25/01
ECE-S352-701 DSP Problem 1.16

Note that in this example, we have used a value of Delta = 0.2. The truncated (blue)
graph shows a quantized value of 0 corresponding to continuous values from 0. to 0.2
(on the horizontal axis) while the rounded (red) graph slows a change to the value 0.2
when the continuous value exceeds 0.1. For reference, the two vertical markers mark
continuous values in the interval [0 , .1].

Now, on to the solution of the problem...

To solve this problem we have written two m-files. The first computes x(n), xqt(n),
xqr(n), the corresponding errors (eqt(n), eqr(n)) for each and SQNR theoretical and for
each quantization method. The input to this program is L the number of levels. The
second program (following later) performs the plotting from variables that are in the
workspace.
% problem 1.16
fo = 1/50;
N = 200;
A = 1
n = 0:1:N-1;
xn = A*sin(2*pi*fo*n); % unquantized sine wave N samples

% define the qunatization levels


L = 64
bits = log(L)/log(2); % get number of bits log is natural log
xmax = 1;
xmin = -1;
Delta = (xmax-xmin)/(L-1)
xqt = []; % accumulator for truncated quantization values
xqr = []; % accumulator for rounded quantization values
% now quantize the data set
for i = 1:N
[qt_value,l_value] = t_quantize(xmax, xmin, L, xn(i));
xqt = [xqt, qt_value];
[qr_value,l_value] = r_quantize(xmax, xmin, L, xn(i));
xqr = [xqr, qr_value];
end
eqt = xqt - xn;
eqr = xqr - xn;

% now compute SQNR

% theoretical (eq. 1.4.32)


bits,
2^bits,
SNQR = 1.76 + 6.02*bits

% truncated quantization
Pqe = sum(eqt.*eqt)/N; % quantized error power
Px = A^2/2; % power of unquantized signal
SQNR_T = 10*log10(Px/Pqe)

% rounded quantization
Pqe = sum(eqr.*eqr)/N; % quantized error power
Px = A^2/2; % power of unquantized signal

SQNR_R = 10*log10(Px/Pqe)

DSP Problem 1.16 page 3 of 8


Tom Chmielewski 6/25/01
ECE-S352-701 DSP Problem 1.16

The results of running this program for the values L = {64, 128, 256} are as follows:
L = 64 L =128 L = 256
Delta = 0.0317 Delta = 0.0157 Delta =0.0078
bits = 6 bits = 7 bits =8
SNQR =37.8800 SNQR =43.9000 SNQR =49.9200
SQNR_T =31.7425 SQNR_T =38.1688 SQNR_T =43.7747
SQNR_R = 36.6821 SQNR_R =42.3257 SQNR_R =50.7701

Thus we may address part d and note that the theoretical values from 64 to 128 to 256
differ by 6.02 dB (each bit adds more signal). For each part the rounded value is greater
than that of the truncated version since the truncated version introduces more error. This
is evident when looking at the e(n) curves, the rounded is symmetrical about zero while
the truncated is not. Except for case 256, (generally negligible) the theoretical value is
the maximum and indicates the best possible condition. Note that the theoretical value
was derived for quantization with rounding.

The second program listing follows:


% plot data
nn = 1:50; % use only first 50 points for clarity
disp_str1 = sprintf('L = %d',L);
disp_str2 = sprintf('Quantization step = %d',Delta);
% plot the waveforms
subplot(3,1,1)
stairs(nn,xn(nn))
title('x(n), discrete time, analog amplitude')
subplot(3,1,2)
stairs(nn,xqt(nn))
title('xqt(n), discrete time, truncated quantized amplitude')
subplot(3,1,3)
stairs(nn,xqt(nn))
title('xqr(n), discrete time, rounded quantized amplitude')
% plot xq(n) - x(n) = eq(n)
figure
subplot(2,1,1)
stem(eqt(nn),'filled')
title('quantization error for truncation')
subplot(2,1,2)
stem(eqr(nn),'filled')
title('quantization error for rounding')
xx = 20; % marker point
figure
plot(nn,xn(nn),'.-k')
axis([1 50 -1 1]);
hold
stairs(nn,xqt(nn),'b')
stairs(nn,xqr(nn), 'r')
for i = -1:0.01:1
plot(xx,i, 'c', xx+1,i,'c'); % marker for comparison
end
title('sine wave: actual, quantized truncated and quantized rounded ')
text(5,-.6, disp_str1)
text(5,-.8, disp_str2)

Note the use of "sprintf" to write variable to a character string placed in a variable for
later display in the plot.

DSP Problem 1.16 page 4 of 8


Tom Chmielewski 6/25/01
ECE-S352-701 DSP Problem 1.16

It became evident that to see anything you only need to plot the first 50 points since the
sine wave repeats, this greatly clarifies the graphs. Pages 6, 7, and 8 show graphs of x(n),
xqt(n), xqr(n), and the corresponding errors eqt(n) and eqr(n) for each method.

In the error plots, note as L goes from 64 ->128->256, the magnitude of the errors
decrease, indicating less error due to smaller quantization levels. Here we used the
"stem" plot command to emphasize the discrete time nature of the signal. Additionally
note that the truncation error is a unipolar signal (0 to negative) while the rounded error is
bipolar (centered around zero).

For comparison purposes the first graph of each set uses the "stairs" plot command for
each value, this allows one to see difference between the original signal and the two
quantization approaches.

Finally the last graph is an overlay of all the signals which clearly shows the differences.
In this case x(n) is plotted as if it is an analog signal. Once again two vertical markers
are shown to aid in analyzing the data.

DSP Problem 1.16 page 5 of 8


Tom Chmielewski 6/25/01
ECE-S352-701 DSP Problem 1.16

L = 64

DSP Problem 1.16 page 6 of 8


Tom Chmielewski 6/25/01
ECE-S352-701 DSP Problem 1.16

L = 128

DSP Problem 1.16 page 7 of 8


Tom Chmielewski 6/25/01
ECE-S352-701 DSP Problem 1.16

L= 256

DSP Problem 1.16 page 8 of 8


Tom Chmielewski 6/25/01

You might also like