FACULTY OF ENGINEERING MULTIMEDIA UNIVERSITY LAB SHEET

Similar documents
Principles of Communications

Chapter 10 Applications in Communications

7.1 Sampling and Reconstruction

Communication Engineering Prof. Surendra Prasad Department of Electrical Engineering Indian Institute of Technology, Delhi

Random Signal Transformations and Quantization

Lab 4: Quantization, Oversampling, and Noise Shaping

The information loss in quantization

EE4512 Analog and Digital Communications Chapter 4. Chapter 4 Receiver Design

Digital Signal Processing

Finite Word Length Effects and Quantisation Noise. Professors A G Constantinides & L R Arnaut

Pulse-Code Modulation (PCM) :

PCM Reference Chapter 12.1, Communication Systems, Carlson. PCM.1

Module 3. Quantization and Coding. Version 2, ECE IIT, Kharagpur

E303: Communication Systems

Source Coding. Scalar Quantization

CMPT 889: Lecture 3 Fundamentals of Digital Audio, Discrete-Time Signals

Analysis of methods for speech signals quantization

Multimedia Systems Giorgio Leonardi A.A Lecture 4 -> 6 : Quantization

Image Compression using DPCM with LMS Algorithm

Multimedia Communications. Scalar Quantization

MATLAB BASICS. Instructor: Prof. Shahrouk Ahmadi. TA: Kartik Bulusu

EE 521: Instrumentation and Measurements

Digital Signal Processing 2/ Advanced Digital Signal Processing Lecture 3, SNR, non-linear Quantisation Gerald Schuller, TU Ilmenau

Various signal sampling and reconstruction methods

2. SPECTRAL ANALYSIS APPLIED TO STOCHASTIC PROCESSES

Quantization 2.1 QUANTIZATION AND THE SOURCE ENCODER

2.004 Dynamics and Control II Spring 2008

Number Representation and Waveform Quantization

E2.5 Signals & Linear Systems. Tutorial Sheet 1 Introduction to Signals & Systems (Lectures 1 & 2)

ETSF15 Analog/Digital. Stefan Höst

Gaussian source Assumptions d = (x-y) 2, given D, find lower bound of I(X;Y)

encoding without prediction) (Server) Quantization: Initial Data 0, 1, 2, Quantized Data 0, 1, 2, 3, 4, 8, 16, 32, 64, 128, 256

Centre for Mathematical Sciences HT 2017 Mathematical Statistics

Scalar and Vector Quantization. National Chiao Tung University Chun-Jen Tsai 11/06/2014

arxiv: v1 [cs.it] 20 Jan 2018

Homework 1 Solutions

Module 2 LOSSLESS IMAGE COMPRESSION SYSTEMS. Version 2 ECE IIT, Kharagpur

EE123 Digital Signal Processing

DSP First. Laboratory Exercise #10. The z, n, and ˆω Domains

Coding for Digital Communication and Beyond Fall 2013 Anant Sahai MT 1

ES205 Analysis and Design of Engineering Systems: Lab 1: An Introductory Tutorial: Getting Started with SIMULINK

Class of waveform coders can be represented in this manner

Time-domain representations

Lecture 2: Introduction to Audio, Video & Image Coding Techniques (I) -- Fundaments

ECS 332: Principles of Communications 2012/1. HW 4 Due: Sep 7

EE 121: Introduction to Digital Communication Systems. 1. Consider the following discrete-time communication system. There are two equallly likely

A Nonuniform Quantization Scheme for High Speed SAR ADC Architecture

Lecture 2: Introduction to Audio, Video & Image Coding Techniques (I) -- Fundaments. Tutorial 1. Acknowledgement and References for lectures 1 to 5

INTRODUCTION TO DELTA-SIGMA ADCS

Signal types. Signal characteristics: RMS, power, db Probability Density Function (PDF). Analogue-to-Digital Conversion (ADC).

Computer Exercise 1 Estimation and Model Validation

This examination consists of 11 pages. Please check that you have a complete copy. Time: 2.5 hrs INSTRUCTIONS

SPEECH ANALYSIS AND SYNTHESIS

Analog Digital Sampling & Discrete Time Discrete Values & Noise Digital-to-Analog Conversion Analog-to-Digital Conversion

SV6: Polynomial Regression and Neural Networks

Centre for Mathematical Sciences HT 2017 Mathematical Statistics. Study chapters 6.1, 6.2 and in the course book.

State Feedback Controller for Position Control of a Flexible Link

6 The SVD Applied to Signal and Image Deblurring

Geology 554 -Wilson Environmental and Exploration Geophysics II Computer Lab - Seismic Resolution I

channel of communication noise Each codeword has length 2, and all digits are either 0 or 1. Such codes are called Binary Codes.

C.M. Liu Perceptual Signal Processing Lab College of Computer Science National Chiao-Tung University

Data Converter Fundamentals

ELEN E4810: Digital Signal Processing Topic 11: Continuous Signals. 1. Sampling and Reconstruction 2. Quantization

8 The SVD Applied to Signal and Image Deblurring

8 The SVD Applied to Signal and Image Deblurring

Digital Circuits, Binary Numbering, and Logic Gates Cornerstone Electronics Technology and Robotics II

Chemistry 14CL. Worksheet for the Molecular Modeling Workshop. (Revised FULL Version 2012 J.W. Pang) (Modified A. A. Russell)

Constellation Shaping for Communication Channels with Quantized Outputs

Laboratory 11 Control Systems Laboratory ECE3557. State Feedback Controller for Position Control of a Flexible Joint

Coding for Discrete Source

ESE 250: Digital Audio Basics. Week 4 February 5, The Frequency Domain. ESE Spring'13 DeHon, Kod, Kadric, Wilson-Shah

CS578- Speech Signal Processing

Digital Signal Processing

Principles of Communications

BASIC COMPRESSION TECHNIQUES

! Where are we on course map? ! What we did in lab last week. " How it relates to this week. ! Compression. " What is it, examples, classifications

Using a Microcontroller to Study the Time Distribution of Counts From a Radioactive Source

9 THEORY OF CODES. 9.0 Introduction. 9.1 Noise

Quantization and Compensation in Sampled Interleaved Multi-Channel Systems

EE-597 Notes Quantization

1 Ex. 1 Verify that the function H(p 1,..., p n ) = k p k log 2 p k satisfies all 8 axioms on H.

Analog to Digital Conversion

Lecture 27 Frequency Response 2

Higher-Order Σ Modulators and the Σ Toolbox

Fault Tolerance Technique in Huffman Coding applies to Baseline JPEG

Automatic Control II Computer exercise 3. LQG Design

Model-building and parameter estimation

Multimedia Communications. Differential Coding

Chapter 12 Variable Phase Interpolation

Coding theory: Applications

Chapter 9 Fundamental Limits in Information Theory

Project IV Fourier Series

Introduction to Computational Neuroscience

ω 0 = 2π/T 0 is called the fundamental angular frequency and ω 2 = 2ω 0 is called the

Linear Motion with Constant Acceleration

EXPERIMENT 2 Reaction Time Objectives Theory

MARKOV CHAINS A finite state Markov chain is a sequence of discrete cv s from a finite alphabet where is a pmf on and for

CSE370: Introduction to Digital Design

Modern Digital Communication Techniques Prof. Suvra Sekhar Das G. S. Sanyal School of Telecommunication Indian Institute of Technology, Kharagpur

Research Article Doppler Velocity Estimation of Overlapping Linear-Period-Modulated Ultrasonic Waves Based on an Expectation-Maximization Algorithm

Transcription:

FACULTY OF ENGINEERING MULTIMEDIA UNIVERSITY LAB SHEET ETM 3136 Digital Communications Trimester 1 (2010/2011) DTL1: Pulse Code Modulation (PCM) Important Notes: Students MUST read this lab sheet before attending the lab. Oral assessment could be conducted on-the-spot. The lab report is to be submitted within two weeks of working days. Ayman, Chuah, May 2010 1

Experiment DTL1: Pulse Code Modulation (PCM) 1. OBJECTIVES: 1. To study the quantization process of PCM schemes 2. To synthesize the coding process of PCM schemes 3. To compare and analyze uniform and non-uniform quantization schemes 2. BASIC CONCEPTS DEFINITION: Pulse code modulation (PCM) is essentially an analog-to-digital conversion (ADC) process where the information contained in the instantaneous samples of an analog signal is represented by digital codewords in a serial bit stream. This can be accomplished by representing the signal in discrete form in both time and amplitude domain. A PCM signal is generated at the transmitter by carrying out three basic operations: sampling, quantizing, and encoding. The pulse code modulation (PCM) process samples an input analog signal, quantizes the sampled signal, and codes the quantized signal into binarycoded digits. A functional block diagram of a PCM transmitter is shown in Figure 1. Figure 1: Block diagram of a PCM transmitter. 2.1 Sampling: It is the process of obtaining an instantaneous value of the input analog signal amplitude at regular intervals. The signal m(t) entering the sampler is band-limited to B Hz. Usually there exists a filter with bandwidth B Hz prior to the sampler to cutoff the out-of-band components. The sampling rate must be sufficiently large so that the analog signal can be reconstructed from its samples m s (n) with sufficient accuracy. The input analog signal is sampled at a rate higher than the Nyquist rate to allow for some guard-band. Thus, the sampling frequency, f s, can be restricted by f s 2B (1) The sampling period is T s second/sample and it is related to the sampling frequency by f s = 1/ T s (2) 2

2.2 Quantization: It is the process of converting the voltage level of the sampled amplitude to the voltage value of the nearest standard level, or quantization level. At the end of this stage, the signal m q (n) will be represented discretely in both time and amplitude. The quantizers can be classified as uniform quantizers or nonuniform quantizers. In uniform quantization, the quantization regions are chosen to have equal length where as in nonuniform quantization, regions of variable length are used. The two techniques are further described as follows: 2.2.1 Uniform Quantization: Assumed that the range of the input samples is [-m max, +m max ]. In uniform quantization, all quantization regions except the first and last ones are of equal length, which is denoted by Δ, and the number of quantization levels L is an integer power of 2. From this, the length of the quantization region is given by 2 Δ= m L The quantization levels are chosen to be the midpoints of the quantization regions and therefore, the quantization error at the n th sample is given by max (3) e(n) = m(t) m q (n) (4) where t = nt s and e(n) has a uniform probability density function on the interval (-Δ/2, +Δ/2]. Two types of uniform quantizer characteristics are shown in Table 1. Table 1: Types of uniform quantizer characteristics Mid-tread Uniform Quantizer Mid-rise Uniform Quantizer Origin point lies in the middle of a tread. Origin point lies in the middle of a rising edge. Used with odd number of quantization levels, Used with even number of quantization levels, e.g. L = 7 as above. e.g. L = 8 as above. 3

Figure 2 shows a midrise uniform quantizer of 8 levels, q 1, q 2,, q 8. The input analog signal m(t) is firstly sampled at nts time intervals to generate the samples signal m s (t) (not shown in the figure). The samples are then quantized to the nearest quantization level to construct the quantized signal m q (n). The 6 samples of the signal, from left to right, will be mapped to q 2, q 3, q 5, q 6, q 6, q 7 because the amplitude of these samples fall into the Reg 2, Reg 3, Reg 5, Reg 6, Reg 6, Reg 7, respectively. Figure 2: An example of quantized samples using 8-level midrise uniform quantizer. 2.2.2 Nonuniform Quantization: As long as the amplitude statistics of the input signal are close to the uniform distribution (all amplitudes have about equal chances to appear), uniform PCM works fine. However, in coding of certain signals such as speech, the input signal amplitude distribution is far from being uniformly distribution. For a speech waveform, in particular, there exists higher probability of smaller amplitudes and lower probabilities for larger amplitudes. Therefore, it makes sense to design a quantizer with more quantization levels at lower amplitudes and less quantization levels at larger amplitudes. The resulting quantizer will be a nonuniform quantizer having variable spacing between the quantization levels. In nonuniform quantization, the sampled signal is first passed through a nonlinear element that compresses the large amplitudes (reduces the dynamic range of the signal) and then perform a uniform quantization on the output. Two types of compressors are used in nonuniform quantizers; the µ-law and the A-law nonlinearities. The input-output relation of the µ-law compressor is defined by 4

where x 1, and its inverse is x = log(1 +μ x ) y = f( x) = sgn x μ ( 1 y +μ) 1 sgn ( y ) μ ( ) (5) (6) The standard PCM system in the U.S. and Canada employs a µ-law compressor with µ = 255. This relation is displayed in Figure 3 with different µ values (µ = no compression). 1 0.8 0.6 mu = 0 mu = 10 mu = 50 mu = 255 y = output 0.4 0.2 0-0.2-0.4-0.6-0.8-1 -1-0.8-0.6-0.4-0.2 0 0.2 0.4 0.6 0.8 1 x = input Figure 3: µ-law input-output relation with different µ values. In A-law quantizers (in practice A = 87.6 is used), the input-output relation is defined by A x 1, x < 1 + A ln( A) y = f( x) = sgn( x) (7) 1 + ln( A x ) 1, x 1. 1+ ln( A) A and it inverse is expressed as y (1+ ln( A)) 1, y < A 1+ ln( A) = ( ) = sgn( ) exp( y (1 + ln( A)) 1) 1, y 1. A 1+ ln( A) 1 x f y y (8) 5

2.3 Encoding: It is the process of representing a particular quantization level of the analog signal with a binary codeword. After quantization, the quantized levels are encoded using R bits for each quantization level. The encoding scheme that is usually employed is natural binary coding (NBC), meaning that the lowest quantization level is mapped into a sequence of all 0 s and the highest level is mapped into a level of all 1 s. In Figure 2 above, R = log 2 (L) = log 2 (8) = 3 bits per codeword were used. 3. PCM SYSTEM PERFORMANCE: 3.1 Performance Metrics: 3.1.1 PCM Transmission Bandwidth: The transmission bandwidth of a serial binary PCM (B PCM ) waveform depends on the bit rate and the waveform pulse shape used to represent the data. The bit rate R b is given by R b = R f s (9) Because an input analog signal band-limited to B Hz, requires a minimum f s 2B samples per second (Nyquist theorem), we require a total of R b = 2RB bits per second. Since we can transmit error-free at most two pieces of information per second per Hz bandwidth (realizable using Nyquist pulse shape), thus we require a minimum PCM transmission bandwidth of B PCM = R B (10) 3.1.2 PCM Signal-to-Quantization-Noise Ratio: The signal-to-quantization-ratio (SQNR) in db is expressed as follows SQNR db = σ ( ) 10log = E m 10log 20log σ E m m = norm( m) norm( m m ) 2 2 m 2 2 e ( q) q (11) where is the variance of the input analog signal m(t) and is the variance of the quantization noise error as stated in equation 4. 6

3.2 Performance Tradeoffs From equation 10, and for reasonable large values of R, the bandwidth of the serial PCM signal will be significantly larger than the bandwidth of the original analog signal it represents. Thus, increasing the number of quantization levels will lead to the negative effect of increasing the transmission bandwidth. However, this increased number of quantization levels will result in the positive effect of reducing the quantization error in equation 11 and thus, improving the SQNR. Therefore, there exists a tradeoff between BCM transmission bandwidth (cost) and the SQNR (quality). 4. EXPERIMENT SOFTWARE 4.1 Introductory Information In this lab experiment, the MATLAB software will be used to understand and simulate the basic concepts and mechanisms of uniform and nonuniform PCM quantizers. MATLAB is an interactive matrix based system for scientific and engineering numeric computation and visualization. Its strength lies in the fact that complex numerical problems can be solved easily and in fraction of the time required with a programming language such as Fortran or C. it is also powerful in the sense that by using its relatively simple programming capabilities, MATLAB can be easily extended to create new commands and functions. 4.2 Getting familiar with related MATLAB Commands and Functions 1. Open and start the MATLAB program by double-clicking the MATLAB icon. 2. In the MATLAB COMMAND WINDOW, learn about the following commands using the MATLAB help. Explain briefly the functionality of each command and also perform two arbitrary corresponding examples to enhance your familiarity. >> help randn >> help abs >> help max 7

>> help length >> help ones >> help fix >> help find >> help norm >> help echo >> help pause >> help sort >> help global >> help de2bi 8

4.3 SIMULATION OF PCM OPERATIONS USING MATLAB 4.3.1 Setup of PCM MATLAB Functions and Scripts into MATLAB Environment In this experiment, there are 7 MATLAB scripts; uniform_ex1.m, uniform_ex2.m, uniform_ex3.m, uniform_ex4.m, uniform_ex5.m, nonuniform_ex4.m, and finally nonuniform_ex5.m. The first five scripts are based on mid-rise uniform quantization (even number of quantization levels) whereas the last two examples are based on a µ-law nonuniform quantization. These 7 MATLAB scripts (examples) use another 5 MATLAB functions; uniform_pcm.m, mulaw_pcm.m, mulaw.m, invmulaw.m, and signum.m. Figure 4 shows a roadmap that describes the interrelationships between the individual MATLAB scripts and functions. For instance, when running the nonuniform_ex5.m script, this script will call the mulaw.m function which will then call the functions unifro_pcm.m, mulaw.m, and invlulaw.m where the last two functions invokes the signum.m function implicitly. Figure 4: Roadmap showing the interrelationships between MATLAB scripts and functions. To setup the experiment, follow the following steps: 1. Go to MATLAB CURRENT DIRECTROY WINDOW and create a folder and name it as DTL1_PCM. 9

2. Copy and paste all the MATLAB scripts and functions below into individual new m- files and save them into the DTL1_PCM folder. By the end of this step, your DTL1_PCM should contain a total of 12 m-files. 3. You only need to run the 7 MATLAB scripts whereas the other 5 MATLAB functions will be called automatically by corresponding scripts as shown in Figure 4. To run a script, double-click its m-file and then click on the Run button and follow the requested commands in the COMMAND WINDOW (if any). 4. Proceed to section 4.3.2 to start exercising the examples provided. Please analyze the codes carefully and understand the effect of executing each individual line. This is a crucial task as it will help you to easily modify a code for a given problem. 4.3.2 PCM MATLAB-BASED FUNCTIONS Please STUDY the following functions carefully! Function: uniform_pcm This function, given next, takes as its input a sequence of sampled values (m_samp) and the number of desired quantization levels (L) and finds the quantized sequence (m_quan), the encoded sequence (code), and the resulting SQNR (sqnr) in db. It is used in uniform quantization and it is also used with other functions in nonuniform quantization as shown in Figure 4. Copy and paste this MATLAB function into an m-file and save it as uniform_pcm.m function [sqnr,m_quan,code]=uniform_pcm(m_samp,l) global q %UNIFORM_PCM uniform PCM encoding of a sequence % [sqnr,m_quan,code]= uniform_pcm(m_samp,l) % m_samp = input sampled sequence. % L = number of quantization levels (even). % sqnr = output SQNR (in db). % m_quan = quantized output before encoding. % idx_quan = index of quantized output. % code = the encoded output. m_max = max(abs(m_samp)); % Find the maximum value of m_samp. m_quan = m_samp/m_max; % Normalizing m_samp. idx_quan = m_quan; % Quantization index. delta = 2/L; % Quantization step. q = delta.*[0:l-1]; % Define quantization regions. q = q-((l-1)/2)*delta; % Centralize all quantization levels % around the x-axis. for i=1:l m_quan(find((q(i)-delta/2 <= m_quan) & (m_quan <= q(i)+delta/2)))=... q(i).*ones(1,length(find((q(i)-delta/2 <= m_quan) &... (m_quan <= q(i)+delta/2)))); idx_quan(find(m_quan==q(i)))=(i-1).*ones(1,length(find(m_quan==q(i)))); end 10

m_quan; idx_quan; m_quan = m_quan * m_max; % Release normalization for quantized values. R =ceil(log2(l)); % Define no. of bits per codeword. code = de2bi(idx_quan', R, 'left-msb'); % Generate codewords. sqnr = 20 * log10(norm(m_samp)/norm(m_samp - m_quan)); % Estimate SQNR. Function: mulaw_pcm This function is the equivalent of the m-file uniform_pcm.m when using a µ-law PCM scheme. It takes as its input a sequence of sampled values (m_samp) and the number of desired quantization levels (L), and the compression constant µ and finds the quantized sequence (m_quan), the encoded sequence (code), and the resulting SQNR (sqnr) in db. Copy and paste this MATLAB function into an m-file and save it as mulaw_pcm.m function [sqnr,m_quan,code]=mulaw_pcm(m_samp,l,mu) %MULAW_PCM mu-law PCM encoding of a sequence % [SQNR,A_QUAN,CODE]=MULAw_PCM(m_samp,L,MU). % m_samp = input sequence. % L = number of quantization levels (even). % sqnr = output SQNR (in db). % m_quan = quantized output before encoding. % code = the encoded output. % Call the mulaw function: [y,m_max]=mulaw(m_samp,mu); % Call the uniform_pcm function: [sqnr,y_quan,code]=uniform_pcm(y,l); % Call the invmulaw function: x = invmulaw(y_quan,mu); m_quan = m_max*x; sqnr = 20*log10(norm(m_samp)/ norm(m_samp - m_quan)); Function: mulaw This function realizes the µ -law nonlinearity used in nonuniform quantization. The inputs to this function are the sampled sequence and µ and it produces the compressed output y and the maximum value of the samples sequence m_max. Copy and paste this MATLAB function into an m-file and save it as mulaw.m function [y,m_max]=mulaw(m_samp,mu) %MULAW mu-law nonlinearity for nonuniform PCM % Y = MULAW(M_SAMP,MU). % M_SAMP = input vector. m_max = max(abs(m_samp)); y =(log(1+mu*abs(m_samp/m_max))./log(1+mu)).*signum(m_samp); 11

Function: invmulaw This function is the inverse of the µ-law; the inputs are the compressed sequence y and µ and the output is the decompressed one x. Copy and paste this MATLAB function into an m-file and save it as invmulaw.m function x = invmulaw(y,mu) %INVMULAW The inverse of mu-law nonlinearity %X=INVMULAW(Y,MU) Y=normalized output of the mu-law nonlinearity. x=( ((1+mu).^(abs(y))-1)./mu).*signum(y); Function: signum This function is used to realize the sign function which is often called the signum function in mathematics. The output of this function is -1 if the current input value is negative, 0 if the current input value is also 0, and +1 if the input is positive. This function is used by the mulaw.m and invmulaw.m m-files. Copy and paste thi s MATLAB function into an m-file and save it as signum.m function y = signum(x) %SIGNUM finds the signum of a vector. % Y=SIGNUM(X) % X=input vector y=x; y(find(x>0))=ones(size(find(x> 0))); y(find(x<0))=-ones(size(find(x<0))); y(find(x==0))=zeros(size(find(x==0))); 4.3.3 PCM MATLAB-BASED SCRIPTS (EXAMPLES) Please RUN the following scripts and answer the corresponding questions! 4.3.3.1 Uniform Quantization: EXAMPLE (uniform_ex1): This example is just an illustrative example with very low sampling frequency. It is u sed to allow you to investigate easily on the resulting outputs. Two figures will be generated; the first one shows the original input signal and its samples whereas the second one shows the normalized signal with its quantized values. In addition, the SQNR and the encoded quantized samples (codewords) are returned. Remember that this is mid-rise uniform quantization where 12

the number of quantization levels has to be even (e.g. 8 as in this example). Copy and paste this MATLAB script into an m-file and save it as uniform_ex1.m % MATLAB script for Illustrative Example uniform_ex1.m clc;clear;close all % echo on global q t m = [0:pi/100:2*pi]; = 10 * sin(t); t_samp = [0:pi/4 :2*pi]; m_samp = 10 * sin(t_samp) figure(1) plot (t,m,'-b','linewidth', 2) hold on stem (t_samp,m_samp, '--ro','linewidth', 2) hold on legend('original signal','sampled signal'); axis([0 2*pi -15 15]) L=16; [sqnr,m_quan,code]=uniform_pcm(m_samp,l) figure(2) plot (t,m/max(m),'-b','linewidth', 2) hold on stem (t_samp,m_samp/max(m), '--ro','linewidth', 2) hold on for i=1:l plot (t,q(i)* ones(1,length(t)),'--g');hold on end legend('normalized original signal','normalized sampled signal'); axis([0 2*pi -1.5 1.5]) echo off Questions: 1) Write down the samples of the signal, their quantized values, and their codewords. 2) Show the waveforms of the second figure generated on a full page. Define the quantization regions, indicate the quantization levels, indicate the amplitudes of the quantized samples, and show how the quantized samples have been successfully mapped to the corresponding quantization levels. 3) On your sketch above, show the assigned codewords to each quantization level and show that the codewords obtained are correct. EXAMPLE (uniform_ex2): This example generates a sinusoidal signal with amplitude =1. Using uniform quantization, the samples sequence is quantized using 8 and 16 quantization levels. It plots the original signal and the quantized signal on the same axes. 13

Copy and paste this MATLAB script into an m-file and save it as uniform_ex2.m % MATLAB script for Illustrative Example uniform_ex2.m clc;clear;close all echo on t_samp = [0:0.01:10]; m_samp = sin(t_samp); [ sqnr_8l,m_quan_8l,code_8l]=uniform_pcm(m_samp,8); [sqnr_16l,m_quan_16l,code_16l]=uniform_pcm(m_samp,16); pause sqnr_8l % Press a key to see the SQNR for L = 8. pause % Press a key to see the SQNR for L = 16. sqnr_16l pause % Press a key to see the plot of the signal and its % quantized versions. t = t_samp; m = m_samp; plot(t,m,'-',t,m_quan_8l,'-.',t,m_quan_16l,'-',t,zeros(1,length(t)),... 'linewidth', 2) legend('original','quantized (L=8)','Quantized (L=16)','Location',... 'SouthEast'); % pause % Press a key to see the first 5 samples, corresponding quanited % % values, and corresponding codewords with 8 quantization levels % m(1:5) % m_quan_8l(1:5) % code_8l(1:5,:) % % pause % Press a key to see the first 5 samples, corresponding quanited % % values, and corresponding codewords with 16 quantization levels % m(1:5) % m_quan_16l(1:5) % code_16l(1:5,:) echo off Questions: 1) Determine the sampling frequency used in this example. 2) Record the SQNR obtained in the two cases where the number of quantization levels, L, is 8 and 16. Compare the values obtained. Justify your answer. 3) View the MATLAB figure generated in full screen and observe the nature of the two quantization signals. Relate your observation to the SQNR values obtained above. EXAMPLE (uniform_ex3): This example generates a sequence of length 500 of zero mean, unit variance Gaussian random variables. It calls the uniform_pcm function to evaluate the SQNR with 64 quantization levels. In addition, it displays the first 5 samples, their corresponding quantized values, and the corresponding codewords. 14

Copy and paste this MATLAB script into an m-file and save it as uniform_ex3.m % MATLAB script for Illustrative Example uniform_ex3.m clc;clear;close all echo on m_samp = randn(1,500); L = 64; [sqnr,m_quan,code]=uniform_pcm(m_samp,l); pause % Press a key to see the SQNR. sqnr pause % Press a key to see the first five input values. m_samp(1: 5) pause % Press a key to see the first five quantized values. m_quan(1:5) pause % Press a key to see the first five codewords. code(1:5,:) echo off Questions: 1) Record the first 5 samples obtained, their corresponding quantized values, and the corresponding codewords. 2) How many bits per codeword is used? Why is it so? 3) Show the PCM transmission bandwidth versus the SQNR tradeoff discussed in section 3.2 by increasing the number of quantization levels to 128 (you need to estimate the PCM bandwidth and SQNR for the two cases). EXAMPLE (uniform_ex4): In this example, the quantization error, defined as the difference between the input value and the quantized value. Also, it plots the quantized output values as a function of the input value. This is done with the number of quantization levels set to 16, 64, and 128. Uniform quantization is used. Copy and paste this MATLAB script into an m-file and save it as uniform_ex4.m % MATLAB script for Illustrative Example uniform_ex4.m clc;clear;close all % echo on no_samp = 500; L = [16 64 128]; for i=1:length(l) randn('state',0) m_samp= randn(1,no_samp); [sqnr,m_quan,code]=uniform_pcm(m_samp,l(i)); %---------------------------------------------------------------------- L(i) %---------------------------------------------------------------------- pause % Press a key to see the SQNR. sqnr % pause % Press a key to see the first five input values. 15

% m_samp(1:5) % pause % Press a key to see the first five quantized values. % m_quan(1:5) % pause % Press a key to see the first five codewords. % code(1:5,:) % Plot the quantization error for each sample. q_error = m_samp - m_quan; figure; plot ([1:no_samp],q_error) % Plot the quantized value as a function of the input value. [Y,Index]=sort( m_samp); figure; plot(y,m_quan(index)) end echo off Questions: 1) Record the SQNR values obtained with the number of quantization levels set to 16, 64, and 128 and show the figures obtained. 2) On the figures of quantization error in each sample, compare between the natures of the curves for the 3 cases with different number of quantization levels. 3) Observe the changes between the 3 figures of input-output relationships. How can you relate this to the SQNR values recorded above? EXAMPLE (uniform_ex5): In this example, a non-stationary sequence of length 500 consists of two parts. The first 20 samples are generated according to a Gaussian random variable with zero mean and variance 400 (σ = 20), and the next 480 samples are drawn according to a Gaussian random variable with mean and variance 1. Uniform quantization is used. Copy and paste this MATLAB script into an m-file and save it as uniform_ex5.m % MATLAB script for Illustrative Example uniform_ex5.m clc;clear;close all % echo on no_samp = 500; L = [16 64 128]; for i=1:length(l) randn('state',0) m_samp= [20*randn(1,no_samp-480) randn(1,no_samp-20)]; [sqnr,m_quan,code]=uniform_pcm(m_samp,l(i)); %---------------------------------------------------------------------- L(i) %---------------------------------------------------------------------- pause % Press a key to see the SQNR. sqnr % pause % Press a key to see the first five input values. % m_samp(1:5) % pause % Press a key to see the first five quantized values. % m_quan(1:5) % pause % Press a key to see the first five codewords. 16

% code(1:5,:) % Plot the quantization error for each sample. q_error = m_samp - m_quan; figure; plot ([1:no_samp],q_error) % Plot the quantized value as a function of the input value. [Y,Index]=sort( m_samp); figure; plot(y,m_quan(index)) end echo off Questions: 1) Record the SQNR values obtained with the number of quantization levels set to 16, 64, and 128 and show the figures obtained. 2) Explain how is the sort(m_samp) command in the script above functioning? 3) What is the effect of the randn('state',0)command? 4.3.3.2 Nonuniform Quantization: EXAMPLE (nonuniform_ex4): This example is similar to the preceding uniform_ex4.m example above except that a µ-law nonuniform quantization is used here. Copy and paste this MATLAB script into an m-file and save it as nonuniform_ex4.m % MATLAB script for Illustrative Example nonuniform_ex4.m clc;clear;close all % echo on no_samp = 500; L = [16 64 128]; mu = 255; for i=1:length(l) randn('state',0) m_samp=randn(1,no_samp); [sqnr,m_quan,code]=mulaw_pcm(m_samp,l(i),mu); %---------------------------------------------------------------------- L(i) %---------------------------------------------------------------------- pause % Press a key to see the SQNR. sqnr % pause % Press a key to see the first five input values. % m_samp(1:5) % % pause % Press a key to see the first five quantized values. m_quan(1:5) % pause % Press a key to see the first five codewords. % code(1:5,:) % Plot the quantization error for each sample. q_error = m_samp - m_quan; figure; plot ([1:no_samp],q_error) % Plot the quantized value as a function of the input value. 17

[Y,Index]=sort(m_samp); figure; plot(y,m_quan(index)) end echo off Questions: 1) Record the SQNR values obtained with the number of quantization levels set to 16, 64, and 128 and show the figures obtained. 2) What is the effect of the command figure in the line; figure; plot ([1:no_samp],q_error) 3) Explain briefly the execution flow of this script and show why the uniform_pcm.m m-file is still used and invoked by the mulaw_pcm.m m-file. EXAMPLE (nonuniform_ex5): This example is similar to the preceding uniform_ex5.m example above except that µ-law nonuniform quantization is used here. Copy and paste this MATLAB script into an m-file and save it as nonuniform_ex5.m % MATLAB script for Illustrative Example nonuniform_ex5.m clc;clear;close all % echo on no_samp = 500; L = [16 64 128]; mu = 255; for i=1:length(l) randn('state',0) m_samp= [20*randn(1,no_samp-480) randn(1,no_samp-20)]; [sqnr,m_quan,code]=mulaw_pcm(m_samp,l(i),mu); %---------------------------------------------------------------------- L(i) %---------------------------------------------------------------------- pause % Press a key to see the SQNR. sqnr % pause % Press a key to see the first five input values. % m_samp(1:5) % pause % Press a key to see the first five quantized values. % m_quan(1:5) % pause % Press a key to see the first five codewords. % code(1:5,:) % Plot the quantization error for each sample. q_error = m_samp - m_quan; figure; plot ([ 1:no_samp],q_error) % Plot the quantized value as a function of the input value. [Y,Index]=sort(m_samp); figure; plot(y, m_quan(index)) end echo off 18

Questions: 1) Record the SQNR values obtained 64, and 128 and show the figures obtained. with the number of quantization levels set to 16, 2) Tabulate the SQNR values at different number of quantization levels (16, 64, and 128) obtained from the scripts uniform_ex4.m, uniform_ex5.m, nonuniform_ex4.m, and nonuniform_ex5.m. 3) Compare between the SQNR values obtained from the four scripts as tabulated above. Show which quantization scheme, uniform or nonuniform, is superior to the other one and how this superiority is sensitive to the dynamic range of the input sequence? Justify your answer. 4.3.3 PCM MATLAB-BASED EXERCISES PROBLEM (1): Construct a MATLAB script for a mid-tread uniform quantizer (odd number of quantization levels) that can digitize the analog signal m(t) = t 2 sin(t), -10 sec t 10 sec with a sampling duration of 1 sec and 7 quantization levels. Show the signal samples, their quantized values, and the corresponding codewords. Plot the original and quantized signals and then verify graphically the correctness of the codewords obtained. Then, vary the odd number of quantization levels and plot the corresponding SQNR values as a function of the number of bits per codeword. Consider that the number of quantization levels is L = 2 i -1, where i = 3, 4,, 8. PROBLEM (2): Develop two functions for an A-law compressor and its inverse and test their correct functionality as they are supposed to compensate each other for an arbitrary input sequence. Repeat the two examples for nonuniform_ex4.m and nonuniform_ex5.m using A-law and its inverse functions and then tabulate and compare the SQNR values obtained for 3 different quantization levels. Discuss the PCM performance in the two cases and its sensitivity to the dynamic range of the input sequence. REFERENCES [1] J. G. Proakis, M. Salehi, and G. Bauch, 2004, Contemporary Communication Systems using MATLAB, Thomoson. [2] J. G. Proakis and M. Salehi, 2002, Communication Systems Engineering, Prentice Hall. [3] L. W. Couch, 2007, Digital and Analog Communication Systems, Prentice Hall. [4] M. Rice, 2009, Digital Communications: A Discrete-Time Approach, Prentice Hall. [5] B. P. Lathi, 1998, Modern Digital & Analog Communication Systems, Oxford. 19