# Explanation for the quantization matlab function

I found this code on quantization on page 314 in the book Modern Digital and Analog Communication Systems by B.P.Lathi and Zhi Ding. I can understand only half of the code. Please, someone, help in explaining the code.

Code:-

function [q_out, Delta] = uniquan(sig_in, L)
% L = number of uniform quantization levels
% sig_in - input signal vector
% Function Output
%     q_out - quantized output
%     Delta - quantization interval

sig_pmax = max(sig_in);              % finding the positive peak
sig_nmax = min(sig_in);              % finding the negative peak
Delta = (sig_pmax - sig_nmax)/L;     % quantization interval
q_level = sig_nmax + Delta/2 : Delta : sig_pmax - Delta/2;  %defining q-levels

% Till here I understand the code. I am not able to understand from here.
% Mainly not able to understand what is sigp doing

sigp = (sig_in - sig_nmax)/Delta + 1/2;   % convert into 1/2 to L+1/2 range
qindex = round(sigp);                     % round to 1,2,3..... L levels
qindex = min(qindex,L);                   % Eliminate L+1 as rare possibility
q_out = q_level(qindex);                  % use index vector to generate output

end

• Please concretize your question. What about the code do you not understand? Is that really a signal processing problem or rather a general matlab programming problem? – Marcus Müller Mar 13 at 11:23
• @MarcusMüller What's the best to ask this type of question other than dsp.stackexchange.com? Also, what is happening from sigp in the code? – A Q Mar 13 at 11:31

In sigp, you are measuring how much steps of Delta it takes to go from sig_nmax till your input signal at the point (sig_in) Suppose it gives you the value, 5.7 as shown in figure, you add 0.5 to it and round it to nearest integer, 6.

Since the max value that integer can take is 7, you need to limit it to 7 in the next step.

Last step qlevel(qindex) will give you the quantized voltage value. The voltage values, say 1V, must have been quantized to 1/7V steps. The 6th value in this case is 6/7V corresponding to sig_in.

• Can you explain what is the need of adding 0.5? – A Q Mar 15 at 10:22
• If you round sigp value less than 0.5, it will round to 0 which is not a valid index is MATLAB – DSP Novice Mar 15 at 10:49
sigp = (sig_in - sig_nmax)/Delta + 1/2;   % convert into 1/2 to L+1/2 range
qindex = round(sigp);                     % round to 1,2,3..... L levels
qindex = min(qindex,L);                   % Eliminate L+1 as rare possibility
q_out = q_level(qindex);                  % use index vector to generate output
clear
end


sigp is a mid-tread uniform quantizer which is defined using the quantization step size Delta whose details you can find here: https://en.wikipedia.org/wiki/Quantization_(signal_processing)#Mid-riser_and_mid-tread_uniform_quantizers

sigp uses sig_in - sig_nmax instead of sig_in so that the signal can lie between 0 and 2 instead of -1 and 1 since indexing in MATLAB is always greater than 0

1/2 is added to round the first index to 1.

qindex = min(qindex,L);


The highest level can be L so if qindex goes over L, it is truncated to L

q_out = q_level(qindex);


q_out gives the quantization level corresponding to the qindex.