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?
For the first two code lines I prefer,
where
Fs
is sampling frequency,L
number of samples andt
shows the time.Note that
x
is sinusoidal with frequency ofFx = 1 Hz
or we can say that it's periodic withTx = 1 sec
.For
8-bit
quantization we have256
levels. SinceL / Fs = [10 sample] / [10 sample/sec] = 1 sec
is equal toTx
(a whole period ofx
) we can work with positive samples.mx
is defined because in order to usefloor
we need to scale thex
.mx
shows the maximum value forx
sox / mx
will take values over[-1 1]
and128*x/mx
over[-128 128]
will cover all256
levels.So we will quantize it with
floor
and scale it back (mx*1/128
).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 with2
bits.Will give
q4 = [-1 -0.5 0 0.5 1]
which has5
levels (instead of2^2 = 4
).It might not be a big problem, you can delete the level
x=1
and haveq4 = [-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
after definition of
mx
so the maximum values ofx
will be quantized in one level lower.The error will increase to
0.0012
.