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

Homework #4: Assignment in Word and PDF formats.
• Please keep all of the work for each problem together. This will make it easier for the grader to grade them and hence assign appropriate partial or full credit for problems.

• Connections between signals & systems and probability include
• When summing two statistically independent random variables x and y, i.e. z = x + y, the PDF of z is the convolution of the probability density functions of x and y, i.e. fZ(z) = fX(z) * fY(z)
• The characteristic function for a random variable is the Fourier transform of the random variable's probability density function
• The moment generation function for a continuous random variable is the Laplace transform of the random variable's probability density function
• Ordinary generating function of a sequence an is the z-transform of an.

• Pseudo-random binary sequences are also known as pseudo-noise (PN) sequences because the sequences resemble noise. These binary sequences appear to be random but instead have structure. They have all frequencies present; i.e., there are no nulls in the magnitude spectrum.

PN sequences are widely used to

• generate test, measurement and calibration signals (e.g. in audio, biomedical and communication systems)
• generate training signals in communication systems (homework assignments 4-7)
• scramble and descramble data in communication systems (lab 4)
• generate additive dither in data converters (lecture 10)

The first application is to use a PN sequence to estimate an impulse response of an unknown subsystem, e.g. the cascade of a source, empty chamber, and receiver in a biomedical instrument. Once the impulse response is known, calibration of the system can proceed by means of a linear time-invariant filter in the probe/transmitter (known as a predistorter) or detector/receiver (known as an equalizer) to compensate for the distortion in the subsystem.

As a training signal, the PN sequence would be the digital data to be transmitted over the unknown communication channel. The receiver knows the bits that had been transmitted, and can use that knowledge to adapt receiver settings to improve communication performance (signal quality). This is the application explored in homework #4.

• JSK, chapters 4, 5, 6, and 8; Appendix B
• Course reader appendices L & S;
• Simon Haykin, Communication Systems
• 4.6 Random Processes
• 4.8 Mean, Correlation and Covariance
• 4.9 Ergodicity

• Problem 4.1. Part (b) builds on part (a).

In part (b), the `rand` function in Matlab generates random numbers that are uniformly distributed on the interval [0,1]. For part II, you are asked to convert a random variable x that is uniformly distributed on the interval [0,1] to a random variable y that is uniformly distributed on the interval [-1,1]. Let's try an affine mapping (i.e. a linear mapping plus an offset) as used in part I:

y = a x + b

Here, a and b are constants. Due to this affine mapping, y has value b when x = 0, and y has value a + b when x = 1. Hence, y is on the interval [b, a+b]. From here, we can solve for a and b.

In part (c), you can generate the 31-length bit sequence by using one of the tables in Appendix L in the course reader or by using the seqgen.pn command in Matlab or by using the sequence you generated in lab 4. The seqgen.pn command requires the Matlab communications toolbox. Please make sure that the PN sequence has maximal length (i.e. its smallest period is 31 samples).

In part (c), you are asked to map a bit sequence of 0s and 1s so that 0 maps to -1 and 1 maps to 1. For efficiency in Matlab, it is important to avoid programming using for loops and instead use vector arithmetic. In this case, one way to perform the mapping follows:

```pn = [ 1 0 1 0 0 1 1 ];
pnOnes = round(2*pn - 1);
```
The freqz command
```freqz(pnOnes);
```
will plot the magnitude response in deciBels.

In answering part (c), please consider the magnitude spectrum in linear units, which you can plot using `plotspec`. For a particular PN sequence, I found that the magnitude response varied from 0 dB and 18.6 dB, or equivalently from 1 to 8.6 in linear units.

For part (c), in addition to plotting the discrete-time Fourier transform of the PN sequence, here's Matlab code to plot the discrete Fourier transform of a PN sequence. The magnitude of all DFT coefficents are the same except the DC coefficient which 1/Nth of the magnitude of the other coefficients.

```pn = [ 1 0 1 0 0 1 1 ];
pnOnes = round(2*pn - 1);

y = fft(pnOnes);
m = abs(y);
Ny = length(y);

%%% Different approach if length is odd or even
if floor(Ny/2) == Ny/2    %% even
kstart = -Ny/2;
kend = (Ny/2) - 1;
else                      %% odd
kstart = -(Ny-1)/2;
kend = (Ny-1)/2;
end
kshift = kstart : kend;
stem(kshift, fftshift(m));
```

• Problem 4.2. This problem examines the robustness of detecting a marker embedded in a long sequence of numbers.

Using correlation against a known marker sequence to find the start of frame is generally not perfect; i.e., there is a probability of error. When gathering statistics, it is important to gather enough sample points to have reliability in the results. For each part of this problem, run the simulation 10000 times, which will give accuracy of 10-2 in the calculation of the correct detection rate.

Here are two ways that the simulation could be run 10000 times:

1. Generate one massively long vector that is formed by concatenating 10000 data vectors, correlate, and find all of the peaks.
2. We could put a for loop around the code for one data vector and keep a running sum of the number of correct marker detections.

The problem with the first approach is that by default, Matlab stores each real number in double-precision floating-point format (i.e. in 8 bytes on a 32-bit machine). The first issue comes when constructing the massively long vector by concatenating 10000 data vectors. The 10000 calls to dynamic memory allocation will lead to fragmented memory and also be really slow. A workaround is to allocate the massively long vector first and fill in its values as they are calculated. The second issue is that long vectors will cause long simulations due to management of long vectors by the desktop computer memory system (between level 1 and level 2 caches as well as RAM).

Hence, the second approach is much more efficient in using computer resources. For the second approach, the resulting Matlab code for part (a) follows:

```totalCorrect = 0;
totalRuns = 10000;
for run = 1:totalRuns
%% Begin code from Software Receiver Design book
loc=30; r=25;          % place header in position loc
data=[ sign(randn(1,loc-1)) header sign(randn(1,r)) ]; % generate signal
sd=0.25; data=data+sd*randn(size(data)) ; % add noise
y=xcorr(header, data) ; % do cross correlation
[m, ind]=max(y);        % location of largest correlation
%% End of code from Software Receiver Design book
if ( loc == headstart )
totalCorrect = totalCorrect + 1;
end
end
totalCorrect / totalRuns
```

In this approach, the amplitudes of the noise signal sd*randn(size(data)) will change with each run but the noise power will remain the same.

In your analysis, compute the probability of correct detection for each marker for different values of the standard deviation sd and put the results in a table. For the marker of all 1s, consider what happens if the bit before the marker is one and consider what happens if the bit after the marker is one. For the PN sequence marker, consider the probability that a random sequence of bits prior to the marker could lead to false detection.

• Problem 4.3. This problem examines the robustness of detecting a known marker sequence after it passes through a communication channel. The communication channel is modeled as a finite impulse response (FIR) filter followed by spectrally-flat additive noise.

Before working this problem, it is recommended that you should complete problem 8.17 first.

In generating random 4-PAM symbol amplitudes from the set {-3, -1, 1, 3}, the Matlab command `rand` might be helpful. The `rand` command generates random numbers that follow a uniform distribution on the interval (0,1).

Entries in the marker should be taken from the set {-3, +3}. That is, amplitude should be either +3 or -3. Please use a pseudo-noise marker sequence.

For the "long sequence of random 4-PAM data", use a sequence that is at least ten times the marker length (i.e. set `loc` to be 310).

Run your simulation 10,000 times to gather accurate statistics about the correctness in detecting the marker. See the example code in the hint above for problem 4.2.

If you don't see any bit errors, then increase the standard deviation (`sd`) of the additive noise to 0.5.

In evaluating the difference in marker detection in the receiver, please examine the frequency content of the marker sequence (in magnitude) and the frequency response of each channel (in magnitude).

Last updated 03/25/24. Send comments to bevans@ece.utexas.edu