Lab 05
Lab 05
Contents
0 Laboratory Objectives and Student Outcomes 2
0.1 Laboratory Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
0.2 Student Outcomes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1 Background 3
1.1 Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Code-Division Multiplexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1
0 Laboratory Objectives and Student Outcomes
0.1 Laboratory Objectives
This laboratory will assist students in understanding fundamental concepts of spread spectrum communica-
tion systems. The activities focus on direct sequence spread spectrum (DSSS) techniques such as those used
in the Global Positioning System (GPS) and 3G Code Division Multiple Access (CDMA) cellular phone sys-
tems. Students will use simulation to experiment with and verify the operation of DSSS systems. Students
will then utilize SDR hardware to implement a spread-spectrum receiver capable of decoding a DSSS signal
sent over a wireless channel.
Implement a spread-spectrum receiver using SDR hardware and verify its operation over a wireless
channel.
2
1 Background
This laboratory concerns direct sequence spread spectrum (DSSS) communication systems. We will consider
the baseband modulator (transmitter) given by the following block diagram
m(t) g(t)
- × -
6
c(t)
where m(t) is the message signal and c(t) is the spreading signal. The baseband demodulator (receiver) is
given by the following block diagram
g(t) m̂(t)
- × -
6
c(t)
3
1.1 Synchronization
The DSSS receiver must have knowledge of the PN code in order to generate c(t) for the despreading
operation. Furthermore, the locally generated PN code in the receiver must be synchronized to the PN
code used in the transmitter. Details of the synchronization processes used in real-world DSSS systems are
beyond the scope of this laboratory. However, many of the synchronization processes involve correlating the
received data with the known PN code. These techniques leverage the fact that certain PN codes exhibit
special correlation properties making them useful for synchronization1 . These properties are revealed by
computing the autocorrelation function of the PN code.
Let c[n] denote the periodic spreading sequence having period M . Define the autocorrelation function of
c[n] as
M −1
1 X
Rc [k] = c[n]c[n − k]
M n=0
The autocorrelation function is a measure of similarity between the code and time-shifted versions of the
code. Note that for c[n] taking on values ±1, Rc [0] = 1 is the maximum value of the autocorrelation function.
For purposes of synchronization, it then follows that a code having small autocorrelation values for k 6= 0 is
desirable.
Figure 1 shows an example MATLAB script for computing the autocorrelation function of a given PN code.
The script also demonstrates using the MATLAB function xcorr for correlating a received signal with its
PN code. The issue of synchronization will be further explored in Sections 2 and 4.
m-sequences, Gold codes, and Walsh-Hadamard codes. Students interested in learning more are encouraged to consult the
course textbook or take a follow-on course in communication systems and/or coding theory.
4
1 %% example_correlation .m
2 % Example script that computes the au t oc or re l at io n function of a
3 % PN sequence . The script also demonstrates using the MATLAB " xcorr "
4 % function to correlate a received signal with a PN sequence .
5 %
6 % Cory J . Prust , Ph . D .
7 % Last Modified : 8/3/2018
8
9 close all
10 clear all
11
12 % % Specify PN code
13 code = [ -1 1 -1 1 1 1 -1 1 1 -1 -1 -1 1 1 1 1] ' ;
14 M = length ( code ) ;
15
16 % % Compute A u to co rr e la ti o n Function
17 % R [ k ] = 1/ M sum_ { n =1}^{ M } c [ n ] c [n - k ]
18 % Note : " circshift " is used because the sequence is assumed periodic
19 R = zeros (M ,1)
20 k = 0:1:( M -1) ;
21 for ii =1: length ( k )
22 R ( ii ) = 1/ M * ( code ' * circshift ( code , k ( ii ) ) ) ; % circshift
23 end
24 stem (k , R )
25
26 % % Simulate received DSSS data and use " xcorr " to correlate it with PN code
27 % Assume spreading factor of M
28 m = [1 1 -1 1] ' ;
29 g = [ m (1) * code ; m (2) * code ; m (3) * code ; m (4) * code ]; % or , use " g = kron (m , code ) ;"
30
31 [r , lag ] = xcorr (g , code ) ;
32 figure
33 stem ( lag , r )
Figure 1: MATLAB example showing calculation of the autocorrelation function of a PN sequence and
use of MATLAB’s xcorr function.
5
2 Direct Sequence Spread Spectrum Simulation
In this portion of the laboratory you will construct a Simulink model for simulating a direct sequence spread
spectrum (DSSS) communication system. The complete model is shown in Figure 2.
[-1 1 -1 1 1 1 -1 1 1 -1 -1 -1 1 1 1 1]
set Sample time to Tc, and set Samples per frame to frameSize. The output of this block
connects to a Ideal Rectangular Filter block, which translates the sequence into rectangu-
lar pulses. Set Pulse length (number of samples) to Tc/Ts, set Form output after final
data value by to Cyclic repetition. Set the Input processing for Columns as frames and
6
set the Rate options for Allow multirate processing. The output of this block is the spread-
ing waveform, c(t).
The Product block creates the complex envelope of the DSSS BPSK signal, m(t)c(t).
Experiment with this portion of the model by adding Time Scope and Spectrum Analyzer blocks.
Hint: You may find it helpful to change the Time Scope to a “stem” plot by selecting View -> Style
and changing the Plot type setting.
3. Add the AWGN Channel block, specifying the Mode as Variance from port and using a Constant
block to control the noise variance. As you experiment with the model, adjust the noise variance (i.e.
the noise power) to see the effects of noise on the system.
5. Experiment with the model by running it. Add Time Scope and Spectrum Analyzer blocks as
needed. Be sure you understand how it operates before proceeding.
7
Question 2.1: Based on the parameters specified in Step 1 above, what is the spreading factor
of the DSSS system?
Question 2.2: Use MATLAB to compute the autocorrelation function for the sequence given in
Step 2 above. Comment on the properties of the autocorrelation function. Does the sequence
exhibit desirable properties for use in a DSSS communication system? Submit your MATLAB
code and plot of the autocorrelation function.
Question 2.3: In the DSSS BPSK Transmitter, connect a Spectrum Analyzer block to the
BPSK message waveform m(t) and the spread waveform m(t)c(t). Submit a screen capture.
Measure the bandwidth of each signal. Do these bandwidth measurements agree with the
spreading factor of the DSSS system? Hint: Measure the bandwidth as the frequency of the first
spectral null.
Question 2.4: In the DSSS BPSK Receiver, connect a Time Scope and Spectrum Analyzer
block to the recovered message waveform (output of the Product block). Carefully examine
these plots as you adjust the Delay length parameter of the Delay block. You should observe
that the receiver properly recovers the message signal when the Delay length parameter is 0.
Describe what happens to the recovered message when the Delay length parameter is 1.
Describe what happens when the Delay length parameter is 16. Explain the results.
8
3 Code Division Multiplexing Simulation
In this portion of the laboratory you will construct a Simulink model for simulating a communication system
that uses code division multiplexing to send multiple messages simultaneously in the same frequency channel.
The complete model is shown in Figure 3.
[1 -1 -1 1 -1 1 1 -1]
and
[1 1 -1 -1 1 1 -1 -1]
are the spreading codes for User 1 and User 2, respectively. Note that these codes are members
of the length 8 Walsh-Hadamard codes.
The Add block is used to sum the User 1 and User 2 coded messages into a single transmission.
3. Construct the User 1 Receiver and User 2 Receiver portions of the model.
9
The Ideal Rectangular Filter, Product, Integrate and Dump, and Gain blocks are used
in the same manner they were in Section 2.
Be sure to use the proper sequences for each receiver.
4. Experiment with the model by running it. Add Time Scope and Spectrum Analyzer blocks as
needed. Be sure you understand how it operates before proceeding.
Question 3.1: In the Transmitter, use Time Scope and Spectrum Analyzer blocks to
observe the inputs to and the output of the Add block. That is, observe the User 1 and User 2
DSSS signals, and their sum. Include screen captures and comment on what you observe. In
particular, comment on the signal bandwidths.
Question 3.2: Use a Time Scope block to display the User 1 and User 2 messages in the
transmitter, and the User 1 and User 2 messages recovered in the receiver. Submit screen
captures for simulations where the AWGN noise variance is 0, 1, and 10. Comment on the
results.
Question 3.3: Repeat the previous problem, however now change the User 2 Receiver sequence
to
[1 -1 1 -1 -1 1 -1 1]
10
4 DSSS BSPK - Text Message Communication
In this portion of the laboratory you will receive a DSSS BPSK waveform using your personal SDR device.
You will then despread the signal and recover an ASCII text message.
Construct the Simulink receiver model shown in Figure 4.
Some salient points regarding the DSSS broadcast and the receiver model:
The DSSS broadcast consists of BPSK symbols. The message information is encoded using BSPK
modulation, giving symbols having values ±1. The chip sequence also contains values of ±1. Therefore,
the complex envelope of the broadcast signal has values of ±1, and can therefore be considered to be
a BSPK waveform.
The message bits are sent at 1000 bits per second, and the chip rate is 8000 chips per second.
The chip sequence used in the transmitter is
[-1 1 -1 1 1 1 -1 1 1 -1 -1 -1 1 1 1 1]
11
– The center frequency must be corrected according to the calibration performed in our previous
laboratory.
The FIR Decimation block reduces the sample rate of the receive data by a factor of 3. The output
of the RTL-SDR Receiver block is configured for 240e3 samples per second, therefore the decimation
reduces the rate to 240e3/3 = 80e3 samples per second. This gives 10 samples per chip, or 80 samples
per bit, in the receive data. Confirm the sample rate, and the frequency offset correction, by carefully
viewing the Spectrum Analyzer plot.
The Carrier Synchronizer block adjusts for carrier frequency and phase offsets. Configure the
Modulation for BPSK and Samples per symbol to 10. This block will synchronize to the chip sequence.
IMPORTANT: This block may synchronize to a constellation that is a 180 degree rotation of the
transmitted signal. We will detect the presence of and correct for any such rotation in a later step.
The Symbol Synchronizer adjusts for clock drift in the incoming signal, ensuring that the incoming
pulses are sampled at the appropriate time. Configure the Timing error detector for Zero-Crossing
(decision directed) and Samples per symbol to 10. The block will output exactly one sample per
chip.
The Eye Diagram can be used to confirm that carrier synchronization has occurred. Set Samples
per symbol to 10.
The Constellation Diagram can be used to confirm the received constellation. Set Samples per
symbol to 1.
The To Workspace block writes data to the MATLAB workspace. Configure Limit data points
to last to 4000, and Save format to Structure.
– After running the model, you will see a structure named yout in the MATLAB workspace. You
can save the MATLAB workspace, and therefore your recorded data, using the save function in
MATLAB. You can then restore the workspace at a later time using the load function.
– The actual symbols are located within the structure at yout.signals.values
At this point, you should have a vector in MATLAB containing 4000 values, each of which corresponds to a
chip in the DSSS sequence. Your task is to recover the ASCII text message. The following sequence of steps
is suggested:
1. Confirm that the vector of data represents a baseband BSPK signal. You can do so by plotting the
data (e.g., using stem or scatterplot).
2. You will notice that the vector of data is complex-valued. Because the data is BPSK modulated, we
only need its real part. The command
data = real(yout.signals.values);
will extract just the real part of the vector.
3. You must implement the despreading operation. Doing so will require several steps:
Use the MATLAB xcorr function to correlate the data vector with the spreading sequence. For
example
code = [-1 1 -1 1 1 1 -1 1 1 -1 -1 -1 1 1 1 1];
[r,lag] = xcorr(code,data);
stem(lag,r)
will compute and plot the cross-correlation between the data vector and the spreading sequence.
12
Examine the cross-correlation plot carefully. It will tell you how to shift the spreading sequence
so as to properly despread the received data.
You must create the despreading signal. This can be thought of as a vector where each value
corresponds to a chip. You may find the MATLAB function circshift helpful for shifting the
code sequence. See Figure 1 for an example of using this function. You may find the MATLAB
function repmat helpful for creating the despreading signal. For example
c = repmat(code',250,1);
will create a column vector containing 250 consecutive copies of code representing the despreading
signal. Note that the dimensions of c should exactly match your received data vector.
The despreading operation is simply the element-by-element product of the received data and the
despreading signal. Plot the resulting waveform. You should be able to clearly see the BSPK
symbols, with 8 consecutive samples of nearly identical amplitude representing each bit.
4. Following proper despreading, it will be convenient to reduce the data vector to one sample per bit. It
is up to you to decide how to achieve this.
Hint: Because the transmitter used rectangular pulses, this operation could be as simple as retaining
every 8th sample. A more robust approach would be to compute the mean of every 8 samples, which
is effectively a matched filter operation for rectangular pulses.
5. You can now perform the BSPK demodulation to recover the binary data sequence.
6. Plot and carefully inspect the binary data sequence. In particular, locate a complete data frame by
identifying the message preamble (described by your instructor). If you see an inverted version of the
preamble, this means that the Carrier Synchronizer locked 180 degrees out of phase relative to the
transmit carrier. You can correct for this by inverting your binary data sequence.
7. After identifying a complete data frame, convert the binary sequence to ASCII characters. This process
is identical to the one used in a previous laboratory.
Question 4.1: Provide a plot of the cross-correlation between your received data and the
spreading sequence. Explain what is seen in the plot. Explain how this information was used in
the despreading operation.
Question 4.2: Provide time-domain stem plots showing the received data before and after the
despreading operation. Explain what is seen in the plot.
Question 4.3: Provide frequency-domain plots showing the signal before and after the
despreading operation. Explain what is seen in the plots. Comment on the relative bandwidth
of the signals.
Question 4.4: Explain how you processed the data vector, after despreading, to retain one
sample per bit (as described in Item 4 above). Include the MATLAB code you used to achieve
this task.
Question 4.5: Provide a time-domain stem plot of the recovered message bit sequence. This
plot must show the message preamble and a complete data frame. Annotate the plot, identifying
the preamble.
Question 4.6: What is the text message? Provide the complete MATLAB code listing you
used in this portion of the lab.
13
References
[1] Mathworks. Communications Toolbox. Online Resource. https://www.mathworks.com/products/communications.html.
[2] Mathworks. USRP Support Package from Communications Toolbox. Online Resource.
http://www.mathworks.com/discovery/sdr/usrp.html.
[3] Mathworks. RTL-SDR Support Package from Communications Toolbox. Online Resource.
https://www.mathworks.com/hardware-support/rtl-sdr.html.
14