Documente Academic
Documente Profesional
Documente Cultură
Signal Quantization
[DATE]
CAL POLY POMONA
3801 W Temple Ave, Pomona CA 91768
ECE 3101L- EXPERIMENT 6
Signal Quantization
Z. Aliyazicioglu.
Contents
OBJECTIVES ................................................................................................................................................... 2
LIST OF EQUIPMENTS AND PARTS ................................................................................................................ 2
1 Introduction ............................................................................................................................................... 2
2 Review of number representations ........................................................................................................... 3
2.1 Sign-magnitude representation .......................................................................................................... 3
2.2 One’s-complement ............................................................................................................................. 3
2.3 Two’s-complement ............................................................................................................................. 3
2.4 Floating Point ...................................................................................................................................... 4
3 Quantization............................................................................................................................................... 5
3.1 Introduction ........................................................................................................................................ 5
3.2 Quantization and Compression........................................................................................................... 5
3.3 Audio Quantization ............................................................................................................................. 6
To create an audio recorder object ...................................................................................................... 6
To read audio Files ................................................................................................................................ 6
To play audio Files ................................................................................................................................. 6
To write audio Files ............................................................................................................................... 7
Quantization in MATLAB ....................................................................................................................... 8
Prelab ............................................................................................................................................................ 9
Lab ............................................................................................................................................................... 10
1. Image Quantization......................................................................................................................... 10
2. Audio Quantization ......................................................................................................................... 11
3. Error Analysis .................................................................................................................................. 12
4. Signal to Noise Ratio ....................................................................................................................... 13
OBJECTIVES
1. Analyze an audio signal in MATLAB
2. Change the quantization of an audio and video signal in MATLAB
3. Analyze signal to noise ratio
1 Introduction
This lab presents two important concepts for working with digital signals.
2.1.2 One’s-complement
To represent a negative number, the complement of the bits for the positive number with
the same magnitude are computed.
The positive number 27 in one’s-complement form would be written as
00011011
2.1.3 Two’s-complement
The problem with both these notations is that two different values represent zero.
Two’s complement notation is a revision to one’s-complement that solves this problem.
To form negative numbers, the positive number is subtracted from a certain binary number.
This number has a one in the most significant bit (MSB), followed by as many zeros as there
are bits in an integer.
If 27 was represented by an eight-bit integer, -27 would be represented as:
100000000
-00011011
=11100101
Notice that this result is one plus the one’s-complement representation for -27 (modulo-2
addition).
What about the second value of 0?
That representation is
1 0 0 0 0 0 0 0.
𝐹 = 𝑀 ∗ 2𝐸
The range of values for this number is (−1.18 ¤ 10−38, 3.40 ¤ 1038).
To add two floating point numbers, the exponents must be the same.
If the exponents are different, the mantissa is adjusted until the exponents match.
If a very small number is added to a large one, the result may be the same as the large
number!
For instance, if 0.15600 · · · 0 ¤ 230 is added to 0.62500 · · · 0 ¤ 2−3, the second number
would be converted to 0.0000 · · · 0 ¤ 230 before addition.
Since the mantissa only holds 23 binary digits, the decimal digits 625 would be lost in the
conversion.
In short, the second number is rounded down to zero.
For multiplication, the two exponents are added and the mantissas multiplied.
3 Quantization
3.1 Introduction
Quantization is the act of rounding off the value of a signal or quantity to certain discrete
levels.
For example, digital scales may round off weight to the nearest gram.
Numbers need to be rounded off in order to be represented in a computer.
Analog voltage signals in a control system may be rounded off to the nearest volt before
they enter a digital controller.
y = audiorecorder returns a handle to an 8-kHz, 8-bit, mono audio recorder object. The
audio recorder object supports methods and properties that you can use to record audio
data.
Example 1
Using a microphone, record 3.5 seconds of 44.1-kHz, 16-bit, stereo data, and then return
the data to the MATLAB workspace as a double array.
Example 2
Using a microphone, record 3.5 seconds 8-bit, 22-kHz mono data, and return the data to
the MATLAB workspace as a uint8 array.
>> x = audioread('filename.au');
or
>> [x,Fs,bits] = audioread('aufile');
Using the following command to have some information about audio file
>> audioplayer(x,Fs,bits)
>> sound(x);
By default sound assumes a sampling rate of 8192Hz. A different sampling rate may be
specified with a second argument. For example, if the signal was sampled at a rate of 16
KHz, the following command could be used.
>> sound(x, 16000);
You may need to give vector x of samples at sampling rate fs, with sample size nbits, type:
soundsc() This command rescales the audio signal before playing it in order to place it
within the dynamic range of the hardware. The syntax is the same as for the sound ()
command.
Use sound() if you do not want to have it auto scaled.
To write audio Files
You can write the audio vector y to a MuLaw encoded File by typing the following MATLAB
function
>>audiowrite(y,'filename.au');
or
Note that you have to use the File-extension “.wav” or ”.au” in order to be able to load the
signal back using wav read , audioread or listen it with other sound player such as Windows
Media Player. (wavwrite and audiowrite is a built-in MATLAB function.)
Example
Let’s have an audio File speech.au which we want to load into Matlab. We use
To play the audio file within Matlab, you write the following command
>> sound(x);
>> plot(x);
Quantization in MATLAB
Produce a quantization index and a quantized output value
[index,quants] = quantiz(sig,partition,codebook)
[index,quants,distor] = quantiz(sig,partition,codebook)
Description
index = quantiz(sig,partition) returns the quantization levels in the real vector signal using
the parameter partition.
partition is a real vector whose entries are in strictly ascending order.
If partition has length n, then index is a column vector whose kth entry is
• 0 if sig(k) ≤ partition(1),
• m if partition(m) < sig(k) ≤ partition(m+1),
• n if partition(n) < sig(k)
quants is a row vector whose length is the same as the length of sig.
quants is related to codebook and index by:
quants(ii) = codebook(index(ii)+1);
where ii is an integer between 1 and length(sig)
Example:
>> partition = [-1:.2:1]; % Length 11, to represent 10 intervals
>> codebook = [-1.2:.2:1]; % Length 12, one entry for each interval
.. [index,quants] = quantiz(x,partition,codebook); % Quantize signal y
Prelab
Quantization level (resolution) is given
𝑀𝑎𝑥(𝑋) − 𝑀𝑖𝑛(𝑋)
𝛥=
𝑁−1
Where N is number of level and 𝑁 = 2𝑏 .
The ratio of the signal average power to the noise power is the signal-quantization noise
ratio (SQNR) gives
𝑆𝑄𝑁𝑅(𝑑𝐵) = 10 log10 𝑆𝑄𝑁𝑅 = 1.76 + 6.02𝑏
1
1. The discrete signal 𝑥(𝑛) = 2 cos (2𝜋 ( ) 𝑛) is quantized with a resolution 𝛥 = 0.1 and 𝛥 =
12
0.01
1a. How many bits are required in the A/D converted in each case?
1b. If you use 12 bits for quantization, what is the resolution?
• skull_image.tif
• moon_image.tif.
y = imread(’skull_image.tif’);
image(y);
colormap(gray(256));
axis(’image’);
The image array will initially be of type uint8, so you will need to convert the image matrix
to type double before performing any computation. Use the command
z=double(y)
𝑀𝑎𝑥(𝑋) − 𝑀𝑖𝑛(𝑋)
𝛥=
𝑁−1
Where X is the signal to be quantized, and N is the number of quantization levels. To force
the data to have a uniform quantization step of Δ,
If an audio signal is to be coded, either for compression or for digital transmission, it must
undergo some form of quantization.
Most often, a general technique known as vector quantization is employed for this task, but
this technique must be tailored to the specific application so it will not be addressed here.
In this exercise, we will observe the effect of uniformly quantizing the samples of four audio
signals.
• Down load the audio files
o gore_audio.au
o boygeorge_audio.au
o speech_audio.au
o music_audio.au
• Write MATLAB code to quantize each of these signals to 7, 4, 2 and 1 bits/sample.
• Listen to the original and quantized signals
• Check the sampling frequency
• Check the number of bits
• Describe the change in quality as the number of b/sample is reduced?
• Is there a point at which the signal quality deteriorates drastically?
• At what point (if any) does it become incomprehensible?
• Which signal’s quality deteriorates faster as the number of levels decreases?
• What b/sample do you think is acceptable for telephone systems?
Plot the four quantized audio signals separately for each of the audio files.
Compare the plots and comment
3. Error Analysis
𝐸 =𝑌−𝑋
Compute the error signal for the quantized speech for 7, 4, 2 and 1 b/sample.
When the spacing, Δ, between quantization levels is sufficiently small, a common statistical
𝛥 𝛥
model for the error is a uniform distribution from to – 2 to 2
• Use the command hist(E,20) to generate a 20-bin histogram for each of the four
error signals.
• Comment on how does the number of quantization levels affect the shape of the
distribution?
• Comment on why the error histograms might not be uniform?
[r,lags] = xcorr(E,200,’unbiased’);
plot(lags,r)
[c,lags] = xcorr(E,Y,200,’unbiased’);
plot(lags,c)
𝑃𝑦
𝑃𝑆𝑁𝑅 =
𝑃𝐸
𝐿
1
𝑃𝑥 = ( ) ∑ 𝑥 2 (𝑛)
𝐿
𝑛=1
• Compute the PSNR for the four quantized speech signals from the previous section.
1
𝑃𝑆𝑁𝑅
and determine the bit rate from the number of quantization levels and sampling rate.
For example, if the sampling rate is 8000 samples/sec, and we are using 7 bits/sample, the
bit rate is 56 kilobits/sec (kbps).