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


Ebook link - https://www.academia.edu/36135973/Book_Modern_Digital_And_Analog_Communication_Systems_4th_edition_by_Lathi
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.