0
$\begingroup$

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

Ebook link - https://www.academia.edu/36135973/Book_Modern_Digital_And_Analog_Communication_Systems_4th_edition_by_Lathi

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$ Commented Mar 13, 2020 at 11:23
  • $\begingroup$ @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? $\endgroup$
    – A Q
    Commented Mar 13, 2020 at 11:31

2 Answers 2

1
$\begingroup$

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) enter image description here

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.

$\endgroup$
2
  • $\begingroup$ Can you explain what is the need of adding 0.5? $\endgroup$
    – A Q
    Commented Mar 15, 2020 at 10:22
  • 1
    $\begingroup$ If you round sigp value less than 0.5, it will round to 0 which is not a valid index is MATLAB $\endgroup$
    – DSP Novice
    Commented Mar 15, 2020 at 10:49
1
$\begingroup$
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.