Calculate Quantization error in MATLAB

5k views Asked by At

iI was given this solution to a problem in my course material.

Problem:

a signal x(t) sampled at 10 sample/sec. consider the first 10 samples of x(t)

x(t) = 0.3 cos(2*pi*t);

using a 8-bit quantiser find the quantisation error.

solution:

(256 quantisation levels)
t=1:10;
x=(0.3)*cos(2*pi*(t-1)/10);
mx=max(abs(x));
q256=mx*(1/128)*floor(128*(x/mx));
stem(q256)
e256=(1/10)*sum(abs(x-q256))
Error: e256 = 9.3750e-04

There was no explanation on this, can you explain how this was calculated in detail?

1

There are 1 answers

2
Rash On BEST ANSWER

For the first two code lines I prefer,

Fs = 10;
L = 10;
t = (0 : L - 1) / Fs;
x = 0.3 * cos(2 * pi * t);

where Fs is sampling frequency, L number of samples and t shows the time.

Note that x is sinusoidal with frequency of Fx = 1 Hz or we can say that it's periodic with Tx = 1 sec.

For 8-bit quantization we have 256 levels. Since L / Fs = [10 sample] / [10 sample/sec] = 1 sec is equal to Tx (a whole period of x) we can work with positive samples.

mx = max(abs(x));

mx is defined because in order to use floor we need to scale the x.

q256 = mx*(1/128)*floor(128*(x/mx));

mx shows the maximum value for x so x / mx will take values over [-1 1] and 128*x/mx over [-128 128] will cover all 256 levels.

So we will quantize it with floor and scale it back (mx*1/128).

e256 = (1/L)*sum(abs(x-q256))

e256 simply shows the mean error over 10 samples.

Note that if L / Fs < Tx then this quantization won't be the optimum one.

Have in mind

The answer that you are given has some problems!

suppose x = [-1 -.2 0 .7 1]; and we want to quantize it with 2 bits.

mx = max(abs(x));
q4 = mx * (1/2) * floor(2*(x/mx));

Will give q4 = [-1 -0.5 0 0.5 1] which has 5 levels (instead of 2^2 = 4).

It might not be a big problem, you can delete the level x=1 and have q4 = [-1 -0.5 0 0.5 0.5], still the code needs some improvements and of course the error will increase.

A simple solution is to add

[~,ind] = max(x);
x(ind) = x(ind) - 1e-10;

after definition of mx so the maximum values of x will be quantized in one level lower.

The error will increase to 0.0012.