# A (Re)Introduction to MATLAB and an Introduction to Quantization

This lab is a brief refresher for MATLAB, and an introduction to some of the rudimentary DSP concepts.  If it is taking you a long time, please come talk to me and I'll help you.  The lab consists of several short exercises.

#### 1. Delayed Sample Function.

A unit sample sequence d[n] of length N can be generated using the MATLAB command
d=[1 zeros(1,N-1)];

Write a similar function for a delayed sample sequence dm[n] which is delayed by M samples.

For N=8, M=4, make a figure with two subplots that shows d and dm, like the one shown below. Make sure the graphs are labeled and the axes are scaled as shown in the figure.  Note that n=1 is taken as the origin, since MATLAB can't handle array indices equal to zero.

#### 2. More simple functions.

Create a simple m-file that creates a unit sample, unit step, and unit ramp function for a sequence of length 10.  Use a for loop to generate the ramp sequence.  Then plot them all on the same axes with a stem plot.   Add a legend.  The final result should be as shown below. Note that n=1 is taken as the origin, since MATLAB can't handle array indices equal to zero.  Note that the legend command for a stem plot is given as:
legend('sample','step','ramp')

#### 3. Even More of the Same

Use the sawtooth and square functions in MATLAB to generate the two sequences shown and plot them using stem.  I input data specified by the user are are the length of the sequence (L), the peak value (A), and the period (N).  Use A=8, L=30, N=11, and Fs=10kHz.

#### 4. Aliasing

On one graph plot three sinusoids with frequencies of 3 Hz, 7 Hz and 13 Hz.  Also plot the three stem plots that correspond to the three signals sampled at 10 Hz.  Qualitatively and quantitatively explain your results using the sampling theorem.  You might want to use doc plot to see how to plot several functions with different line styles.

#### 5.   Quantization (rounding)

In practice the sampling process also involves "quantization" in which the signal being sampled is assigned a discrete value so it can be  used inside a computer.

The quantization process is often modeled as a "quantization noise" being added to a signal.  In the diagram below the quantization noise is labeled "e[n]."

When a signal is digitized, quantization errors occur.  This is demonstrated below where the input signal, x,  is rounded to one of 8 quantization levels.

In this diagram the sampled (but not yet quantized) signal is in blue (i.e., x[n]), the sampled and quantized signal (i.e., xq[n]), and the quantization error (i.e., e[n]) is shown in green.

In class we showed that for a sinusoidal signal the SQNR (Signal to Quantization Noise Ratio) given by quantization by rounding is:

SQNRdB = 10log10(SQNR) = 1.76 + 6.02b

where "b" is the number of bits.  Use the code below to verify this (link to file).

The file rounds a sequence (x) to b bits (2b quantization levels).  It also plots the signal, the quantized signal, and the error

You will need to use the fact that the power of a sampled signal, y(n), of period N is given by

a) Use the equation above for the SQNR to calculate the SQNR and the code below to empirically calculate the SQNR for b = 2, 4, 6, 8 and 10 when the input is a sine wave.

b) Repeat for a triangle wave input (you'll have to derive an expression for the SQNR because the power present in a triangle wave is different than that in a sine wave).

#### 6. Quantization (truncation)

Truncation is another quantization method in which quantization levels are determined by "flooring" rather than "rounding" (this is sometimes easier electronically).  In this case the quantized value is lower than the analog value.   This is shown below.  Note that the quantization transition occurs in the middle of an interval for rounding, but at the end of an interval for truncating.

Repeat parts a and b of the task 5 (you'll need to derive new expressions for SQNR in both cases).

#### 7. Quantization (à la Proakis et Manolakis)

Your book uses a simpler equation, shown below (equation 1.15 - with xmax-xmin=2).  This yields one too many quantization levels (Figure 1.9 from text has 33 quantization levels, instead of 25=32 level; in the figure below, the book's method yields 9 quantization levels instead of 8).

xq=round(x*2^(n-1))/2^(n-1);

Repeat parts a and b of task 5 (you needn't re-derive the SQNR in this case - just use present the empirical results).  Compare the results of the two methods and comment on their accuracy when compared to the theoretical equation for SQNR.

#### 8. Oversampling

A very common way of improving the SQNR is to employ "oversampling" in which we sample at a faster rate than necessary according to the Nyquist criterion.  We then process several consecutive signals and use that result as our final sample.  This is shown in the diagram below.

If we want a final sample rate of 8 kHz and oversample by a factor of 4 (i.e., os=4) our initial sample rate (cyan lines) is 32 kHz.  In this diagram we quantize the signal at this point, and then we "smooth" (in our case this will consist of an average of the last "os" points).  Finally we take one of every "os" points to yield a final sample rate of 8 kHz (red line).

Though it is in practice more difficult, we could also smooth before quantization, as shown below.

The following example implements both methods (link to file). You needn't completely understand how the code works.

In the output of this program the variable "sqnr_sq" is the SQNR between the exact result (unquantized - or, more precisely, quantized at a much higher resolution (all numbers in computers are quantized), so we can ignore quantization error) and the signal that is quantized and then smoothed; "sqnr+qs" is SQNR for the signal that is smoothed and then quantized. Run the code with a random input at several different resolutions (i.e., vary the number of bits). As the oversampling rate increases we increase the smoothing. Also not that the original signal is also denser relative to the down sampled signal at higher rates. For each set of data, a new set of random input data is used, so results will vary a bit from one execution of the program to the next.

Try to understand the results qualitatively.  For the case of 8 quantization levels (b=3):

• Explain why one method is better than the other.  Hint: Redraw the diagrams with the quantizer as above (i.e., an error signal added to the desired signal).
• Qualitatively explain the trends in the numbers as the oversampling ratio is increased.

Note: Many modern analog to digital converters use an extension of this idea to create high-resolution conversions with only a single bit quantizer (i.e., a comparator, the output is either high or low).

#### 9 Extra

I did a rather inelegant job with the MATLAB quantization code for problem 5.  See if you can do it more elegantly without using MATLAB's quantization toolbox.

## To turn in.

I expect only a small amount of actual prose in your report, but strive for clarity and completeness.  Also, please shrink the graphs down so that  more than one will fit on a page.

1.  (10 pts) The MATLAB code and the resulting graph.

2.  (10 pts) Your MATLAB code, and the resulting graph.

3.  (10 pts) Your MATLAB code, and the resulting graph.

4.  (15 pts) Your MATLAB code, and the resulting graph, and an explanation of the results.  Strive for clarity, and completeness, in your explanation of this and subsequent parts.

5.  (20 pts) An expression for the SQNR of a sine and triangle wave, along with empirical evidence that verifies your equation.

6.  (20 pts) An expression for the SQNR of a sine and triangle wave, along with empirical evidence that verifies your equation.

7.  (10 pts) An expression for the SQNR of a sine and triangle wave, along with empirical evidence that verifies your equation.

8.  (15 pts) Your explanation of why one method is better than the other and a qualitative discussion of the effect of the oversampling ratio.

9.  (5 pts) Your code and evidence that it works.  You can simply run the code I gave you with your quantization code substituted for mine.  Show results for sawtooth.