# ECE 445S Real-Time Digital Signal Processing Lab - Homework 1 Hints

Homework #1: Assignment in Word and PDF formats.
• As much as possible, the TAs, grader and I would like to give you feedback on your critical thinking processes on all assignments in the class.
• Please keep all parts of a question together. Avoid separating the code/plots from the rest of the question. Remember that both code and plots need to be included and legible in order to get full credit.
• Label graph axes, including units. Missing or incorrect units may result in point deduction.
• When doing calculations, keep them in order with your final solution at the end (or the bottom). Simplify as much as possible, including convolutions.
• Show all of your work. Do not just give a final answer.

• For the names of Matlab functions for common signals, please see Common Signals in Matlab. You can receive help on a particular Matlab command by typing `help exp` and use the `lookfor` command to find a Matlab command for a given task.

• Problem 1.1. This problem concerns four commonly used building blocks in systems.

(a) The problem concerns a five-tap (or five-coefficient) discrete-time averaging filter.

The five-tap discrete-time averaging filter, with input x[n] and output y[n], is described by

```y[n] = 1/5 x[n] + 1/5 x[n-1] + 1/5 x[n-2] + 1/5 x[n-3] + 1/5 x[n-4]
```
Five "taps" means five terms. To compute the current output y[n], we average the current input value x[n] and the previous four input values.

For analysis of averaging filters in the time, z, and frequency domains, please see the handout on designing averaging filters.

A very efficient implementation is possible when making all of the coefficients equal to one because the difference equation would be computed without any multiplications. This is equivalent to scaling the filter coefficients by 5, which in turn would scale the magnitude response in linear units by 5 or shift the magnitude response in dB up by 20 log10(5) = 13.98 dB. The shape of the magnitude response would not change and remain lowpass. Here's the analysis for the scaled version of the averaging filter: z domain part 1 and z domain part 2

(c) The discrete-time analogy to integration is summation. For the discrete-time integrator, the relationship between the input x[n] and output y[n] for n ≥ 0 becomes

```        n
---|
\
y[n] =   |  x[m]
/
---|
m = 0
```
The above equation is not terribly efficient as the amount of memory to store previous input values grows without bound as n increases. A computationally efficient version of the equation is given below:
y[n] = y[n-1] + x[n]
with initial condition y[-1] = 0
The discrete-time integrator, a.k.a. running summation, is an example of an infinite impulse response filter. That is, the impulse response is infinite in non-zero extent.

• Problem 1.2: For cos( angle(t) ), the instantaneous frequency in rad/s is d/dt angle(t).
• Consider the familiar case of angle(t) = 2 pi f0 t. Instantaneous frequency is 2 pi f0.
• For the chirp signal, angle(t) = 2 pi (f0 + 1/2 fstep t) t. Instantaneous frequency is 2 pi (f0 + fstep t).

Please see slides 1-14 through 1-20 in Common Signals in Matlab for more information about chirp signals and how to visualize their relationship between time and frequency using spectrograms.

(b) The Matlab command `plotspec` is defined by the Matlab file plotspec.m from the Software Receiver Design book. The `plotspec` function takes the time-domain signal and the sampling time as arguments, and plots the time-domain waveform and the magnitude of its Fourier transform. The Fourier transform give the average frequency content in the time-domain signal, and does not indicate at what times the frequencies occur.

The `plotspec` command will accept the vector of amplitude values in the time domain and a sampling period Ts. It will plot the signal in the continuous-time domain (top plot) and in the frequency domain (bottom plot). The time-domain plot assumes that the time-domain signal starts at t = 0. The magnitude response will be plotted over the frequencies -1/2 fs to 1/2 fs, where fs is the sampling rate.

Matlab has its own `plotspec` function that takes three arguments and plots a time-frequency signal representation called a spectrogram. This is a very different function from the `plotspec` function in the JSK textbook.

```%%% Generate chirp signal with frequency
%%% increasing from f0 to (f0 + fstep time) over time seconds
time = 10;
f0 = 20;
fstep = 420;
fs = 44100;
Ts = 1 / fs;
%%% Add code here to define chirp signal y = cos( angle(t) )
sound(y, fs)
```

When you play the chirp signal on a laptop or tablet, whether it is over their speakers or through headphones, you might not hear frequencies below 200 Hz. To hear those frequencies, the playback system would need to be able to convert the these low frequencies in the electrical signal into large wavelength pressure waves via piezoelectric devices. The corresponding size of these devices does not fit in laptops and tables. In a conventional audio system, a sub-woofer would handle these low frequencies.

(d) Please create a new plot window to plot the spectrogram by using the Matlab command

```figure;
```
The spectrogram will be hard to follow if it appears in the plot window used by the JSK plotspec command. Here is an example use of the spectrogram function:
```blockSize = 256;
shift = 128;
overlap = blockSize - shift;
spectrogram(y, hamming(blockSize), overlap, blockSize, fs, 'yaxis');
```
The fourth argument indicates the number of samples in a block of samples, and hence, it's also the length of the fast Fourier transform applied to the block of samples. It does not have to be a power of two. The third argument is how many samples overlap in current block with the previous block. The second argument indicates weighting applied to samples before the fast Fourier transform is applied. Using a Hamming window can reduce high-frequency artifacts that arise from periodically extending the block of samples due to the FFT.

A Hamming window has finite duration, and is even symmetric about the midpoint with endpoints having value 0.08. The amplitude values of the causal Hamming window of length N samples is defined as

```w[n] = 0.54 - 0.46 cos(2 pi n / (N - 1))
```
for n = 0, 1, ..., N-1. One can plot a 256-point Hamming window in Matlab by evaluating the command
```stem(hamming(256));
```

The gong audio file comes with the zip file that accompanies the Software Receiver Design book.

This problem has four parts:
(a) Remove DC offset on baseband output for downconversion using sinusoidal demodulation.
(b) Replace downconversion with squaring device + lowpass filter. (Don't remove DC offset yet)
(c) Add a square root operation after the lowpass filter.
(d) Remove DC offset on new baseband output.

Here are hints on parts (a) and (b) and comments on applications of this problem.

(a) Mean squared error (MSE) is a measure of signal quality between signals x[n] and y[n]:

```          M-1              2
MSE =  Sum    (x[n] - y[n])
n=0
```
where M is the number of samples to be compared and a higher MSE value means a large error between the signals. The MSE value will generally increase with M.

The normalized mean squared error (NMSE) is a common alternative because its value normalizes away the dependence on the discrete-time signal length M:

```         1     M-1              2
NMSE =  --- Sum    (x[n] - y[n])
M     n=0
```
For example, the NMSE for discrete-time signals x1[n] and y1[n] of length M1 samples and x2[n] and y2[n] of length M2 samples can be directly compared.

Accounting for delay through the lowpass filter. In computing the mean squared error between the message signal at the transmitter (`basebandInput`) and the message signal decoded at the receiver (`basebandOutput`), one also has to take into account the delay in the transmitter and receiver. The only delay is in the lowpass filter used in the receiver.

The group delay through a finite impulse response (FIR) filter whose N coefficients are even symmetric about its midpoint is (N-1)/2 samples. We'll derive this formula in lecture 5 slides. In order for the group delay to be an integer, N must be odd.

We can force the filter length to be odd by replacing

```FIRlength = floor(fs/f1);
```
with
```%%% Use an odd-length FIR filter
FIRlength = floor(fs/f1);
if 2*floor(FIRlength/2) == FIRlength
FIRlength = FIRlength - 1;
end
```
In computing the mean squared error between the baseband input signal (gong signal) and the baseband output signal, it's important to account for the delay through the filter. Given the `basebandInput` and `basebandOutput` vectors, we remove the last (N-1)/2 samples from `basebandInput` because the corresponding filter output would occur at the `basebandOutput` vector. Also, we remove the first (N-1)/2 samples from the basebandOutput signal because these represent partial responses from the filter. The lower the mean squared error, the closer the signals are in numerical value. MSE may not correlate with perceptual quality, e.g. how good the audio sounds.

(b) Downconversion using a squaring block. You're asked to use a squaring block followed by a lowpass filter and square root block to realize downconversion. When a cosine of fixed frequency is input to a squaring block, the output would consist of a zero-frequency component and a cosine at twice the fixed frequency. An advantage of using the squaring block is that the receiver would not need to know the exact carrier frequency to perform downconversion.

Here are the block diagrams for downconversion using (a) amplitude sinusodial demodulation which is amplitude sinusodial modulation followed by a lowpass filter, and (b) an alternate approach using a cascade of a squaring block, a different lowpass filter, and a square root block. In the alternate approach, y2(t) = r2(t) = r(t) r(t). In the frequency domain, Y2(f) = R(f) * R(f). When convolving two finite-length signals, the result has a duration that is the sum of the non-zero extents of the two signals. Since r(t) is a bandpass signal, the baseband component of y2(t) will have twice the bandwidth than that of y(t).

Adjusting averaging filter length. As noted in the block diagram, the baseband bandwidth in the receiver will be twice that of downconversion using sinusoidal amplitude demodulation. You'll need to change the demodulating lowpass FIR filter to extract the baseband component. Please see the handout on designing averaging filters. (The continuous-time rectangular pulse is the impulse response of an continuous-time averaging filter, and its Fourier transform is a sinc function times a phase shift, as you had seen on homework problem 0.1). Due to sampling, we can connect 2 pi / length = 2 pi f0 / fs, and hence length = fs / f0. Since length is an integer, length = floor(fs / f0).

Please change the averaging FIR filter length by updating

```FIRlength = floor(fs/f1);
```
beyond needing to make sure that the filter length is odd as above.

Gain A gain of two is used for sinusoidal demodulation, and that gain works well for demodulation using the squaring block. In trying out different gain values, one gets an MSE of 133 for a gain of 2 and 39.3 for a gain of 3 and 43.1 for a gain of 4. In terms of MSE, a filter gain of 3 is a better choice than a filter gain of 2 or 4. Keeping the filter gain of 2 used for sinusoidal modulation is also fine.

More information about using a squaring block in downconversion is available on slides 5-8 and 5-9 by Prof. Stephen Tretter.

Epilogue: Why use the DC offset at baseband in the transmitter?
It greatly simplifies the receiver when using a squaring block but doubles the transmission power. The squaring block represents the voltage effect of a nonlinear circuit such as a transistor or vacuum tube (before transistors). The receiver can be implemented with a single transistor and a handful of resistors and capacitors. Here's an example AM radio receiver circuit.

AM radio stations started widespread broadcasting in the 1920s using this style of double sideband large carrier (DSB-LC) amplitude modulation (i.e. adding a DC offset to the baseband speech/audio signal). They made the broadcasting transmitter more expensive so that receiver would be inexpensive and more people would be able to buy one for their homes. The invention of the transistor in 1947 enabled the invention of the transistor radio. The first transistor radio reached the market in 1954.

The DC component is at baseband and will be modulated to be centered at the carrier frequency. This places a lot signal power at the carrier frequency, which gives rise to the name Double Sideband - Large Carrier Amplitude Modulation. Adding a DC component at baseband is wasteful in the sense that it is not being used to make the message signal (basebandInput) more powerful. In AM radio transmissions, the DC component has as much power in it as the message signal. After modulation, the extra power in the DC component shifts to the carrier frequency, which greatly simplifies the receiver. A receiver can be constructed out of a single transistor. For more information, see the Web page for lecture 19 on sinusoidal modulation.

Last updated 09/13/24. Send comments to bevans@ece.utexas.edu