ECE431, Experiment 2, 2016 Communications Lab, University of Toronto Experiment 2: Sampling and Quantization Bruno Korst - bkf@comm.utoronto.ca Abstract In this experiment, you will see the effects caused by changes in sampling time and quantization levels on a digitized (i.e., sampled) sinusoidal signal. You will start by designing and testing a system to produce an output delayed half of a sample relative to the input. On the second part of the experiment, you will move on to changing the number of bits assigned to each sample of the input signal, and will observe the presence of quantization noise. Keywords sampling half-sample delay quantization levels quantization noise Contents Introduction 1 1 Experiment 2 1.1 Half-Sample Delay................................................................. 2 Problem Statement Simulation - PreLab Questions Implementation 1.2 Quantization..................................................................... 3 2 Accomplishments 4 Acknowledgments 4 References 4 Introduction In order to manipulate an analog signal using a microprocessor, the signal must be converted into a digital form. The purpose of this experiment is to introduce sampling and quantization. It is fair to say that every textbook on Digital Signal Processing (DSP) starts by describing how an analog signal is converted into a digital signal prior to being processed [1], [2]. A/D conversion (analog-to-digital, or simply a to d ) is the very first step to be performed, usually by a hardware device called an A/D (analog-to-digital, or simply a to d ) converter. The A/D converter will take samples of the signal at regular intervals (a process called sampling) and will assign binary quantities to these samples (a process called quantization). After the signal is processed by the microprocessor, the resulting digital samples are sent to a D/A converter to be recomposed into an analog (continuous) signal. The hardware you will use in this lab includes an integrated circuit that does both A/D and D/A conversions (a CODEC), and a dedicated signal processor (a DSP ) that performs all necessary computation as specified in the program you will run. The CODEC operates at 48KHz sampling rate, with each two-channel sample represented in 32 bits (that is, 16 bits per channel). Keep these numbers in mind throughout the lab. In this experiment, you will play with the sample time and the number of quantization bits. It is a good idea if you review your notes on convolution, half-sample delay from introduction to signals and systems, and bitwise operations from microprocessor theory. The experiment will be done in two main parts: First, you will create a half sample delay by means of a convolution between the input analog signal and an impulse response sequence that you will calculate. Second, you will observe the effects of reducing the number of bits used per sample. As this number decreases, noise increases and the resulting signal is a deteriorated version of the original analog signal.
Experiment 2: Sampling and Quantization 2/5 Note that the lab answer sheet is to be done in groups of two students. Before you come to the lab, complete the Preparation document. You should also print the answer sheet to bring to the lab. Note that there are a few questions marked PreLab on the answer sheet. You are expected to fill those out prior to the experiment. You will need the results of that portion for the experiment. In the lab, then, you will follow the procedure below on the computer and record your answers for the remaining questions on the answer sheet. 1. Experiment 1.1 Half-Sample Delay In a previous course on signals and systems, you would have been introduced to convolution. In this part of the lab you will design a sequence and convolve it with a sinusoid. While in MATLAB you can design your sequences to have defined lengths, in a real-time implementation your incoming signal comes from a signal generator. For all purposes, the signal coming from the generator is infinite, for it lasts while the instrument is on. 1.1.1 Problem Statement Suppose that you have the sampled version of a continuous-time signal x(t) (which is bandlimited and is assumed to satisfy the Nyquist criterion) x[n] = x(nt s ), (1) where the sampling frequency is 48kHz. Your goal is to find y[n], the half-sample delayed version of x[n], i.e., y[n] = x((n 1/2)T s ). (2) Since x(t) satisfies Nyquist, you could in principle reconstruct x(t) from x[n], then resample to get y[n]. The point of this exercise is to find a digital way to accomplish the same thing. 1 In your lab preparation, design a digital filter with impulse response h[n] so that y[n] = x[n] h[n]. (3) Derive a mathematical expression for h[n], as follows. Consider the block diagram x(t T/2) x(t) x[n] x(t) y[n] C/D D/C e st/2 C/D h[n] The bandlimited signal x(t) is sampled to produce x[n]. By the sampling theorem, x(t) will be reconstructed by D/C. Delaying x(t) by T /2 seconds gives x(t T /2). Sampling this signal produces the desired y[n]. But the system in the block diagram from x[n] to y[n] is actually a discrete-time LTI system; let h[n] be its impulse response function. To find out what h[n] is, just set x[n] to be δ[n], the unit impulse, and find y[n]. Then h[n] must be equal to y[n] by definition of impulse response. The impulse response h[n] is of infinite length (IIR). In practical implementation, we need to limit it to a finite duration. For the rest of the lab, assume that the filter length is at most 32. 1.1.2 Simulation - PreLab Questions Your task now is to create: a) a finite sequence (i.e., a vector) of a sinusoid of frequency 2kHz, of length 100, sampled at 48kHz (to make it better to visualize, plot only 2 cycles of the sinusoid, or 48 samples); b) a 32 element sequence h 1 [n] to generate a delay of 17 samples; and c) a 32 element sequence h 2 [n] to generate a delay of 17-and-a-half samples (similar to the one you designed above). Plot h 1 [n] and h 2 [n] and attach to your answer booklet. You will determine that the half-sample delay is taking place by comparing the results of two convolutions: sinusoid convolved with 17-sample-delayed sequence; and sinusoid convolved with 17-and-half-sample-delayed sequence. Here is a hint: your designed sequences should look like the ones presented below. You may choose the same length as presented or not, but avoid going beyond a length of 35 samples. 1 The relevant material in the text is shown in the example for Noninteger Delay
Experiment 2: Sampling and Quantization 3/5 (a) Half Sample Figure 1. Two sequences to be convolved with the input (b) Pure Delay (shifted impulse) 1.1.3 Implementation The convolution that you just performed in MATLAB is the simulation of what you will do in real time. In order to run this convolution in real time, a skeleton program is given to you. Open Code Composer Studio - CCS - and click on the project named ECE431 Exp01a Half Sample Delay. After the project is selected as active (i.e., the name becomes bold-faced), click on it again to open the folder and see the files. You will need to insert your two sequences in the two coefficient files, written in C (look at the project tree on Code Composer Studio). Open the files, then copy and paste your sequences from within MATLAB. It may be a good idea to display the transpose of your sequence on the MATLAB active window and copy from there. After you do this, you will need to recompile the project and run the executable. This is done by clicking on the bug icon (a button with a green bug drawn on). Remember that the program deals with samples coming as one 32-bit word representing two channels (from a stereo CODEC). Two convolutions will be performed (one for each channel) and two outputs are generated. The outputs are combined into one word and sent back to the CODEC. With the DSP running already, turn on your oscilloscope and signal generator, and connect inputs and outputs appropriately. Make sure the output button on the signal generator is pressed or you will not have a signal. Set your signal to a 1.8 V pp (peak-to-peak) sine at 1KHz. Display both outputs on the scope. After you verify that it runs, modify the frequency to 20KHz, leaving the amplitude unchanged. Now move on to answer the questions on the answer sheet. When you are successful in observing the half-sample delay, ask the TA to sign off on your lab. Answer additional questions in the answer booklet. 1.2 Quantization This part of the experiment will be done without simulation. Stop the previous program and select now as active project the one named ECE431 Exp01b Quantization. After doing so, build it and run it. This program will leave the sampling rate untouched, but will allow you to modify the number of bits used to represent the incoming signal. The quatization done by the existing CODEC on the board provides 16 bits per sample, which means 2 16 quantization levels. That is a very good resolution for an audio signal, for instance. The hardware you use determines the word size in which samples are represented, and for this case it is two channels with 16 bits per sample each (i.e., it is a 32-bit CODEC). The experiment now is to limit the number of bits from the original 16-bit quantization, and observe the results on the oscilloscope. You will effectively increase the quantization error. As you have already reviewed, when you reduce the number of bits on the quantization, you increase the error introduced in the process by reducing the number of quantization levels available. The question to be asked is: how much error can you tolerate for your application? Or, in other words, how many bits do you really need in order to represent your signal and maintain the desired quality? Figure 2 below shows a sinusoid, its 3-bit quantization version and the resulting error. You will utilize a bit mask to modify the word length of the incoming signal. You have likely utilized masking in a Microprocessors course. It consists of performing a logical AND operation between the data word passed on to the DAC and a
Experiment 2: Sampling and Quantization 4/5 Figure 2. Original Sinusoid, Quantized Sinusoid and Quantization Error mask that you select. Figure 3 below shows a sinusoid and a ramp, quantized with 3 bits, for instance. Notice the 8 levels of the output. (a) Sinusoid Figure 3. A Quantized Sinusoid and a Quantized Ramp (b) Ramp (Sawtooth) You will need to rebuild the project after every change you make on the code. Use an input signal of 1.8V pp and 100Hz to answer the questions below. The reason why you are using 1.8V pp is that you want to use the full voltage range of the CODEC, so that all bits of the word will be used. If you do not have the full range of the signal going into the CODEC, the different masks may not give you the right results. 2. Accomplishments In this experiment, you acquired a better understanding of sampling and quantization by further probing into the limitations of the sampling frequency and the number of bits per sample. You verified the introduction of aliasing by sampling a signal at a rate lower than the one dictated by the sampling theorem. You also verified the consequence of reducing the quantization levels by limiting the number of bits utilized to represent the samples of a signal. Acknowledgments Thanks for all the students who have provided input on the previous versions of this experiment.
Experiment 2: Sampling and Quantization 5/5 References [1] Schafer R. W. Oppenheim A. V. Discrete-Time Signal Processing, 3rd. Ed. Prentice-Hall, 2009. [2] Manolakis D. K. Proakis J. G. Digital Signal Processing, Principles, Algorithms and Applications, 4th. Ed. Prentice-Hall, 2006.