Which command should be used for quantization of a signal in MATLAB?

I want to convert analog signal to digital form using Matlab. I know that this will need two steps (sampling and quantization) but I am confused which command should be used for quantization? quant or quantiz?

I have read online official documentation of both commands quant & quantiz.

The documentation for quantiz hasan example of quantization of a sine wave but there is no such example in the quant documentation

Which command should I use? quant or quantiz?

quant is a function in the neural network toolbox, while quantize is a function in the fixedpoint toolbox. You can use help to get brief documentation on what the functions do.

The quantize function works with fixed-point numbers, which is probably not what you want.

The quant function can be used to round floating point numbers to the nearest integer, but you can just as easily do that with the round function.

In practice, you would measure an analog signal and quantize it using an analog-to-digital converter (ADC) which is a hardware device. It isn't something you'd do inside MATLAB.

Every signal that's represented as a vector or a matrix in Matlab is already quantized. You can't really represent an analog signal in Matlab: once it's list of numbers, it's discrete in time and in amplitude.

This being said, Matlab's default data type (double) has an extremely low quantization error (-300 dB or thereabouts) so for most practical applications this is "good enough".

If you want to quantize to a different data type you can simply scale and round. For example:

%% 1kHz sine wave quantized to 16 bits
% Buid the sine wave
fs = 48000; % sample rate
nx = 1024; % length in samples;
x = sin(2*pi*1000/fs*(0:nx-1)');

% quantize to 16-bits
fullScale = 2^(16-1)-1;
xq = int16(round(fullScale*x));

% calculate the quantization error
xerr = double(xq)/fullScale-x;
fprintf('Quantization Error = %6.2fdB\n',10*log10(mean(xerr.^2)./mean(x.^2)));


You can use build-in functions for this, but this can easily be done manually and this way you learn more about the process and can adapt it too your needs. In this example you can calculate the quantization error directly.

This example uses a uniform quantizer which is by far the most common type of quantization. However there are non-uniform quantizers (like optimal quantizers) as well and that's where functions like quantiz` come in handy.

• "You can use build-in functions" can you please elaborate which built in function? Feb 28, 2023 at 4:33
• I suggest reading the documentation and decide which one is best for your specific application. Feb 28, 2023 at 5:53