Faster way to find the quantization level of a sample Given a sequence of real numbers that represent a sound signal i.e [1.8, 2.2, 2.2, 1.9, -1.5, -0.7], we must quantize this sequence by dividing the range [-4, 4] to 32 equal parts.

If we had to do this with the traditional way we would need to write 32 different ranges

[-4   , -3.75]

[-3.75, 3.5]

.
.
.

and check in which one the real value is in.

Is there an another way to achieve this? Maybe with a normalization function or what?
As hinted by Florian, to quantise any $x[n] \in \mathbb{R}$ to a $y[n] \in \mathbb{Z}$ with $K$ levels you can apply the function:

$$y[n] = round\left( \frac{x[n]-min(x)}{range(x)} (K-1)  \right)$$

Where $n \in [0,1,2..|x|]$ and $|.|$ denotes the length of the sequence. 

This will map your $x$ from its $min(x)..max(x)$ range to a $[0..K)$ discrete steps range.

For example (in Octave):

Fs = 100; #Sampling frequency (in Hz)
f = 4; # Frequency of a simple sinusoidal signal (in Hz)
T = 1; # Timeframe length (in seconds)
K = 5; # Number of levels to quantise to
t = 0:(1./Fs):(T-(1./Fs));
p = 2.0 * pi * t;
x = sin(f*p);
y = round(((x-min(x))/2.0) * (K-1));
plot(y);
xlabel("Discrete time (sample)");
ylabel("Amplitude");
grid on;


Which produces:



You might also like to see uencode

Hope this helps.