Top Banner
APPROVED: Shengli Fu, Major Professor and Program Coordinator Kamesh Namuduri, Committee Member Xinrong Li, Committee Member Murali Varanasi, Chair of the Department of Electrical Engineering Costas Tsatsoulis, Dean of the College of Engineering James D. Meernik, Acting Dean of the Toulouse Graduate School A BIDIRECTIONAL TWO-HOP RELAY NETWORK USING GNU RADIO AND USRP Johnny Le Thesis Prepared for the Degree of MASTER OF SCIENCE UNIVERSITY OF NORTH TEXAS August 2011
139

A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Feb 10, 2017

Download

Documents

vandieu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

APPROVED: Shengli Fu, Major Professor and Program

Coordinator Kamesh Namuduri, Committee Member Xinrong Li, Committee Member Murali Varanasi, Chair of the Department of

Electrical Engineering Costas Tsatsoulis, Dean of the College of

Engineering James D. Meernik, Acting Dean of the

Toulouse Graduate School

A BIDIRECTIONAL TWO-HOP RELAY NETWORK USING GNU RADIO AND USRP

Johnny Le

Thesis Prepared for the Degree of

MASTER OF SCIENCE

UNIVERSITY OF NORTH TEXAS

August 2011

Page 2: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Le, Johnny. A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP.

Master of Science (Electrical Engineering), August 2011, 130 pp., 4 tables, 28 illustrations,

bibliography, 19 titles.

A bidirectional two-hop relay network with decode-and-forward strategy is implemented

using GNU Radio (software) and several USRPs (hardware) on Ubuntu (operating system). The

relay communication system is comprised of three nodes; Base Station A, Base Station B, and

Relay Station (the intermediate node). During the first time slot, Base Station A and Base Station

B will each transmit data, e.g., a JPEG file, to Relay Station using DBPSK modulation and

FDMA. For the final time slot, Relay Station will perform a bitwise XOR of the data, and

transmit the XORed data to Base Station A and Base Station B, where the received data is

decoded by performing another XOR operation with the original data.

Page 3: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Copyright 2011

by

Johnny Le

ii

Page 4: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

CONTENTS

LIST OF TABLES vi

LIST OF FIGURES vii

CHAPTER 1. INTRODUCTION (OVERALL ON SDR, GNU RADIO, AND USRP) 1

1.1. Software Defined Radio 1

1.2. GNU Radio 3

1.3. Universal Software Radio Peripheral (USRP) 3

1.3.1. USRP Motherboard 4

1.3.2. Daughterboard 4

CHAPTER 2. OVERVIEW OF RELAY NETWORK WITH GNU SDR 11

2.1. Design Setup 11

2.2. Data Packet Structure 11

2.3. Differentially Encoded Binary Phase-Shift Keying (DBPSK) 13

2.3.1. Binary Phase-Shift Keying (BPSK) and Phase Ambiguity 13

2.3.2. Coherent vs. Noncoherent DBPSK Demodulation 14

2.4. Abstract Flowgraphs for Transmitter and Receiver 15

2.5. Timing Diagrams for Relay System 16

CHAPTER 3. RELAY NETWORK (THEORETICAL SIMULATION) 19

3.1. Bit Error Probability for Entire Relay Network 19

3.2. Bit Error Probability for Base Station A/B 20

3.3. Bit Error Probability for Relay Station 21

CHAPTER 4. DEVELOPMENT OF RELAY NETWORK OVER GNU RADIO 23

iii

Page 5: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

4.1. Differential Binary Phase-Shift Keying (DBPSK) 23

4.1.1. DBPSK Modulator 23

4.1.2. Coherent DBPSK Demodulator 25

4.2. Relay Station 27

4.3. Base Station A/B 27

4.4. Onscreen Display for Relay Network Applications 28

4.5. Streaming Music using GStreamer 29

4.6. Complete Abstract Flowgraphs for TX and RX 30

CHAPTER 5. CONCLUSION 32

5.1. Future Work 32

APPENDIX A. GNU RADIO INSTALLATION GUIDE 33

A.1. Stage 1: Install Ubuntu 34

A.2. Stage 2: Install Package Prerequisites 34

A.3. Stage 3: Install GNU Radio 35

A.4. Stage 4: Configure USRP1 Support 35

A.5. Stage 5: Address Broken libtool 36

A.6. Stage 6: Enable Real-Time Scheduling 36

A.7. Terminal Window Output After make check 38

APPENDIX B. EXAMPLES IN GNU RADIO 41

B.1. Example 1: dial tone.py 42

B.2. Example 2: usrp benchmark usb.py 44

B.3. Example 3: usrp siggen.py, usrp oscope.py, and usrp fft.py 45

B.4. Example 4: usrp probe 46

B.5. Source Code for dial tone.py 47

APPENDIX C. SOURCE CODES FOR RELAY SYSTEM OVER GNU RADIO 50

C.1. Source Code for dbpsk.py 51

iv

Page 6: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

C.2. Base Station A/B 65

C.2.1. Source Code for combine.py 65

C.2.2. Source Code for transmit path.py 73

C.2.3. Source Code for usrp transmit path.py 78

C.2.4. Source Code for receive path.py 82

C.2.5. Source Code for usrp receive path.py 88

C.2.6. Source Code for generic usrp.py 92

C.3. Relay Station 103

C.3.1. Source Code for combine.py 103

C.3.2. Source Code for combine path.py 113

BIBLIOGRAPHY 129

v

Page 7: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

LIST OF TABLES

1.1 Available Daughterboards 7

2.1 Data Packet Structure Information 12

4.1 Receive Phase Onscreen Display 29

A.1Excluded Components After Make Check 36

vi

Page 8: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

LIST OF FIGURES

1.1 Example Radio Communication System 2

1.2 Block Diagram for USRP 5

1.3 USRP Motherboard 6

1.4 USRP Motherboard on Chassis 8

1.5 RFX2400 Daughterboard 8

1.6 40 dB Attenuator 9

1.7 SMA-703 (Top) and HG2407RD-SM (Bottom) 9

1.8 USRP with FLEX400, or RFX400, (Side B) and RFX2400 (Side A) 10

1.9 TX and RX paths for computer and USRP 10

2.1 Data Packet Structure 11

2.2 Bit Error Probability Curve for BPSK and Coherent DBPSK Demodulation (both

with Gray Coding in AWGN) 15

2.3 Bit Error Probability Curve for Coherent and Noncoherent DBPSK Demodulation

in AWGN 16

2.4 TX Flowgraph 16

2.5 RX Flowgraph 16

2.6 Relay Communication System (first time slot) 17

2.7 Relay Communication System (final time slot) 17

2.8 Relay Communication System (first and final time slot) 18

3.1 Simplified Relay Network 19

vii

Page 9: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

3.2 Bit error probability curve for entire relay network (nodes A and B) in AWGN 20

3.3 Bit error probability curve for Base Station A/B (node A/B) in AWGN 22

3.4 Bit error probability curve for Relay Station (node R) in AWGN 22

4.1 Abstract flowgraph for DBPSK modulator in GNU Radio 23

4.2 Abstract flowgraph for coherent DBPSK demodulator in GNU Radio 25

4.3 Complete TX Flowgraph 31

4.4 Complete RX Flowgraph 31

B.1dial tone.py 44

B.2usrp benchmark usb 45

B.3usrp probe Application Window 47

viii

Page 10: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

CHAPTER 1

INTRODUCTION (OVERALL ON SDR, GNU RADIO, AND USRP)

1.1. Software Defined Radio

Software defined radio (SDR) is a term used to describe a radio communication system

where a range of different hardware components (e.g., filters, amplifiers, modulators/de-

modulators, detectors, etc.) are implemented in software. When components are built as

application-specific hardware, there is difficulty in modifying existing systems or prototyping

new systems. This is due to the relatively more time and monetary costs required to change

hardware, than it is to change code in software.

Once the functionality of radio components are implemented on a software environment,

there are additional advantages gained, such as greater flexibility. For a radio communi-

cation system, flexibility means the ability to transmit and receive widely different radio

protocols (or waveforms). Another benefit from using software is that the data processing

may be performed with any general purpose computer, eliminating the need to buy expensive

specialized hardware. Furthermore, software even allows a component to be reused [13].

In summary, SDR minimizes hardware to provide faster modifications, easier prototyping,

lower costs, greater flexibility, and reusability. In academia, a majority of learning and

research in digital communications, involves developing software using computers. Thus,

SDR is very beneficial for educational institutions where computers are plenty, but the

budget for supplementary hardware is modest.

Figure 1.1 shows an example of a radio communication system [3, 11, 17]. The following

are brief descriptions of the basic elements:

1

Page 11: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Figure 1.1. Example Radio Communication System

• Information source and input/output transducer: The information source can be

audio, picture, video, etc. The input transducer converts the information to an

electrical signal taking values in the set {0, 1}.

• Source encoder: Removes redundancy from the source, i.e., compression.

• Channel encoder: Adds redundancy for error detection and/or correction.

• Digital modulator: Maps digital information to waveforms.

• DAC: The digital-to-analog converter (DAC) converts digital information to an

analog signal.

• RF front end (TX): The RF front-end at the transmitter, or TX, converts a lower

intermediate frequency respectively to RF.

• Channel: Physical medium, e.g., Radio Frequency (RF) cable, free space.

• RF front end (RX): The RF front-end at the receiver, or RX, converts RF to a

lower intermediate frequency.

• ADC: The analog-to-digital converter (ADC) converts an analog signal to digital

information.

• Digital demodulator: Recovers digital information from waveforms.

• Channel decoder: Detects and/or corrects error.

2

Page 12: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

• Source decoder: Adds redundancy to the source, i.e., decompression.

• Output transducer and output signal: The output transducer converts the electrical

signal to the output signal, e.g., sound, light, etc.

In the case of a SDR system, the transducers, source encoder/decoder, channel en-

coder/decoder, and digital modulator/demodulator blocks may be defined in software, and

the DAC/ADC and RF front-end blocks can be accomplished in hardware. For this thesis,

a relay communication system is implemented using GNU Radio, a software toolkit, and

Universal Software Radio Peripheral (USRP), a RF hardware.

1.2. GNU Radio

GNU Radio is a cross-platform and open-source software toolkit aimed at providing a

framework for learning and developing software defined radios [19]. Essentially, GNU Radio

is a software package that contains many signal processing libraries (or packages). The

signal processing packages are coded in python (for non-critical performance functions, with

emphasis on high level organization) and C++ (for critical and optimized performance) [8].

This is done in order to maximize the inherent benefits of the coding architectures. An index

of the packages are located at [10].

GNU Radio first began in 2001 with Eric Blossom at the helm as project manager. In

2010, Tom Rondeau became the new project manager. Both individuals and many others

contribute to the development of the GNU Radio project, and actively participate in feedback

and solving issues pertaining to GNU Radio. The following is the GNU Radio homepage (for

information and guides), and GNU Radio mailing lists (for discussion and troubleshooting)

[9]:

• http://gnuradio.org/

• http://www.gnu.org/software/gnuradio/mailinglists.html

1.3. Universal Software Radio Peripheral (USRP)

The Universal Software Radio Peripheral (USRP) is a reconfigurable RF hardware de-

signed by Matt Ettus, and distributed by Ettus Research LLC; a company founded by Matt

3

Page 13: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

in 2004, and as of 2010, operates as a subsidiary of National Instruments Corporation. Pro-

nounced ”usurp,” the USRP interfaces with a computer through Universal Serial Bus 2.0

(USB 2.0), with the goal of allowing personal computers to serve as high bandwidth software

radios. Collectively, GNU Radio and its signal processing libraries, a commercial computer,

and the USRP provides the ideal solution for developing software radio systems with minimal

and generic hardware on a modest budget.

1.3.1. USRP Motherboard

The main components of the USRP motherboard include four high-speed 64 MS/s (64M

samples/second ) 12-bit ADCs, four high-speed 128 MS/s (128M samples/second) 14-bit

DACs, a million gate Altera Cyclone EP1C12 field programmable gate array (FPGA), and

a Cypress FX2 chip; a programmable USB 2.0 controller. Since the USB 2.0 interface has a

theoretical data throughput of 480 Mb/s, the FPGA has four digital downconverters (DDC)

with programmable decimation rates (for RX side), and two digital upconverters (DUCs)

with programmable interpolation rates (for TX side). The USRP performance is limited

by the speeds of USB 2.0 and the computer processing unit (CPU). Refer to [6] for more

information regarding the USRP. Figure 1.2 shows a block diagram for the USRP and Figure

1.3 shows a picture of the USRP motherboard.

1.3.2. Daughterboard

The USRP may be paired with daughterboards; modular components for the USRP which

serves as the RF front-ends. The USRP can accommodate up to four basic daughterboards,

or two RF transceiver daughterboards, for 4 (or 2 I-Q pairs) input channels and 4 (or 2 I-Q

pairs) output channels. There are numerous daughterboards which each operating within a

frequency range. Altogether, the daughterboards cover a wide frequency spectrum; 0 to 5.9

GHz. Table 1.1 lists the available daughterboards at Ettus Research LLC homepage [7].

Figure 1.4 shows the USRP motherboard mounted on the black metal chassis (without

the top cover) with a fan on the left side. The motherboard has two sides, designated as

Side A and Side B. Each side has 2 slots (TX and RX) where the daughterboard can be

4

Page 14: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Figure 1.2. Block Diagram for USRP

mounted. The daughterboard slots on Side B are inverted and precaution should be taken

for proper daughterboard placement.

Each basic daughterboard has one SubMiniature version A (SMA) connector; TX or RX.

The SMA connectors are industry standardized coaxial RF connectors used to interface with

antennas or RF cables. Unlike the basic daughterboards, each RF transceiver daughterboards

have two SMA connectors (TX/RX and RX2). The TX/RX port may transmit or receive a

signal in half-duplex mode. Hence, the daughterboard family name is ”transceiver.” For full-

duplex operation using a single daughterboard, RX2 can receive and TX/RX can transmit

simultaneously. The aforementioned actions are set in software.

When using RF cables to directly connect TX to a separate RX (or RX2) SMA connector,

it is important to use a 40-50 dB attenuator. Otherwise, the voltage from a direct connection

may damage the USRP. Figure 1.5 shows a RFX2400 daughterboard and Figure 1.6 shows

a 40 dB attenuator connected to a RF cable.

5

Page 15: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Figure 1.3. USRP Motherboard

Figure 1.7 shows two antennas used for RFX400 and RFX2400 daughterboards. The

SMA-703 (top) is a tri-band HT antenna with frequency operation at 118-160, 250-290, 360-

390, 420-470 and 820-960 MHz. The HG2407RD-SM (bottom) is a ”rubber duck” antenna

with an operational frequency range of 2.4-2.5 GHz. Both antennas are omnidirectional so

it is best to place them parallel to eachother. The antennas should also be at least three

meters apart. Every daughterboard will require the matching antennas to work properly at

their designed frequency bands.

Figure 1.8 shows the USRP with two RF transceiver daughterboards mounted on the

USRP motherboard; the FLEX400 (RFX400) on Side B and RFX2400 on Side A. The

RFX400 daughterboard on Side B is placed upside down. It is important that while the

USRP is powered by the power adaptor connected to a wall outlet, plugging or unplugging

6

Page 16: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Table 1.1. Available Daughterboards

Daughterboard Daughterboard Type Frequency Band

BasicRX Receiver 0.1-300 MHz

BasicTX Transmitter 0.1-200 MHz

LFRX Receiver DC-30 MHz

LFTX Transmitter DC-30 MHz

TVRX Receiver 50-860 MHz

DBSRX Receiver 800-2400 MHz

WBX Transceiver 50-2200 MHz

RFX400 Transceiver 400-500 MHz

RFX900 Transceiver 750-1050 MHz

RFX1200 Transceiver 1150-1450 MHz

RFX1800 Transceiver 1500-2100 MHz

RFX2400 Transceiver 2250-2900 MHz

XCVR2450 Dual-Band Transceiver 2400-5000 MHz

the daughterboard will blow the USRP motherboard’s on-board fuse and cause permanent

damage to the USRP.

Figure 1.9 shows the TX and RX flow graphs of the USRP connected to a computer

through USB interface. The DUC and DAC are integrated into a single circuit; the AD9862.

7

Page 17: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Figure 1.4. USRP Motherboard on Chassis

Figure 1.5. RFX2400 Daughterboard

8

Page 18: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Figure 1.6. 40 dB Attenuator

Figure 1.7. SMA-703 (Top) and HG2407RD-SM (Bottom)

9

Page 19: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Figure 1.8. USRP with FLEX400, or RFX400, (Side B) and RFX2400 (Side A)

Figure 1.9. TX and RX paths for computer and USRP

10

Page 20: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

CHAPTER 2

OVERVIEW OF RELAY NETWORK WITH GNU SDR

2.1. Design Setup

A bidirectional two-hop relay network with decode-and-forward (DF) strategy is imple-

mented using GNU Radio and several USRPs on Ubuntu. The relay communication system

is comprised of three nodes; Base Station A, Base Station B, and Relay Station (the interme-

diate node). The source code for Base Station A and Base Station B are equivalent. These

base stations, along with Relay Station, will access channels using Frequency Division Mul-

tiple Access (FDMA), to transmit and receive data packets. The USRP at Base Station A is

configured with a RFX400 daughterboard with a frequency band of 400-500 MHz. At Base

Station B, the USRP is configured with a RFX2400 daughterboard having an operational

frequency band of 2250-2900 MHz. Relay Station has a USRP equipped with RFX400 on

Side B and RFX2400 on Side A.

2.2. Data Packet Structure

All data is assembled into packets consisting of preamble access code, header, offset,

payload, 32-bit Cyclic Redundancy Check (CRC), and ’\x55’. Refer to [16] to learn more

about CRC. Figure 2.1 shows the data packet structure and Table 2.1 lists the descriptions.

Figure 2.1. Data Packet Structure

11

Page 21: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Table 2.1: Data Packet Structure Information

Section Size Description

Preamble 2 Bytes The preamble is used to synchronize with the

receiver. Its hexidecimal string representa-

tion is ’\xA4\xF2’.

Access Code 8 Bytes The preamble and access code are used

to correlate with the receiver, to deter-

mine when the packet starts. The access

code’s hexidecimal string representation is

’\xAC\xDD\xA4\xE2\xF2\x8C\x20\xFC’.

Header 4 Bytes The header contains the length of the pay-

load with CRC, repeated twice (”length” is

2 bytes).

CRC-32 4 Bytes (32 Bits) The CRC-32 is error detecting code for the

payload.

Whitener Offset 0 to 15 Bytes The payload is whitened (a bitwise exclusive

OR (XOR) operation with a known PN code

sequence) to prevent phase error when send-

ing constant symbols. The PN code used

for whitening is the output of a 15-bit lin-

ear feedback shift register (LFSR). The offset

sets where to begin in the whitening string

,i.e., shifts the start of XOR operation (de-

fault whitening offset is zero).

12

Page 22: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Payload 0 to 4092 bytes The payload contains the data, and is en-

coded with a 2-byte header containing a

number for packet labeling and tracking.

The user can dynamically set the payload

length from 0 to 4092 bytes (default length

is 1500 bytes).

’\x55’ 1 Byte ’\x55’ is the end byte.

2.3. Differentially Encoded Binary Phase-Shift Keying (DBPSK)

Differentially Encoded Binary Phase-Shift Keying (DBPSK), as the name suggests, is

an extension of Binary Phase-Shift Keying (BPSK). When data from BPSK is differentially

encoded, information is conveyed by the phase differences between consecutive symbols [2].

Differential encoding is expressed in the following equation:

(1) y[n] = y[n− 1]⊕ x[n]

where x[n] is the input binary sequence. More information on DBPSK can be found at

[5].

2.3.1. Binary Phase-Shift Keying (BPSK) and Phase Ambiguity

BPSK is a type of digital modulation scheme where each symbol requires one bit and

takes on values in the set {-a, a}. The symbols are denoted by a phase shift of a cosine wave;

the first, with a zero phase transition and the second, with a 180 degree phase transition.

Therefore, for every symbol transition, the signal shifts its phase by 180 degrees. However,

the channel may introduce a phase shift of 180 degrees, causing the symbol constellation to

invert and increasing complexity of decision at the receiver. This issue is known as phase

ambiguity.

13

Page 23: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

One solution for the phase ambiguity problem is to use a 7-bit scrambler, which scrambles

the bits with a self-synchronizing pseudorandom sequence of bits (PN code). However, with

a 7-bit scrambler and 7-bit descrambler, each channel error results in three bit errors.

A more efficient method to solve phase ambiguity is differential encoding, i.e., Differen-

tially Encoded BPSK (DBPSK). As previously discussed, DBPSK conveys information in

the phase differences between consecutive symbols, also eliminating phase ambiguity issue.

Each channel error in DBPSK results in two bit errors (an SNR decrease of 3 dB). The bit

error probability for a BPSK signal is given by Equation 2.

(2) Pb =1

2erfc(

√Eb

N0

)

The bit error probability for a coherently detected DBPSK signal is given by Equation

3.

(3) Pb = erfc(

√Eb

N0

)[1− 1

2erfc(

√Eb

N0

)]

The BER comparison curve for BPSK and coherently dectected DBPSK (both with gray

coding over Additive White Gaussian Noise (AWGN) channel) is shown in Figure 2.2.

2.3.2. Coherent vs. Noncoherent DBPSK Demodulation

The abbreviation DBPSK has two meanings; Differentially Encoded BPSK and Differ-

ential BPSK. The former is a coherent demodulation, and the latter implies a noncoherent

demodulation. In coherent demodulation, a reference signal is used to determine the exact

phase of the received signal, usually implemented in a phase locked loop (PLL). DBPSK

is a noncoherent modulation scheme, but DBPSK demodulation will still benefit (at least

slightly) by using a PLL. The bit error probability for a noncoherently detected DBPSK

signal is found in Equation 4. Figure 2.3 shows the theoretical bit error probability curve

for coherent vs. noncoherent DBPSK demodulation (and simulation of coherent DBPSK

14

Page 24: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Figure 2.2. Bit Error Probability Curve for BPSK and Coherent DBPSK

Demodulation (both with Gray Coding in AWGN)

demodulation) over AWGN channel. A Costas PLL was used for the implementation of the

relay network in GNU Radio (discussed in the following chapter).

(4) Pb =1

2exp−Eb

N0

2.4. Abstract Flowgraphs for Transmitter and Receiver

The transmitters at each station take one user input (e.g., from a picture file) and con-

struct the data packets with the structure in Figure 2.1. As the data packets are being

assembled, they will be passed to the DBPSK modulator in Figure 4.1, and then sent to the

USRP. Figure 2.4 shows the TX flowgraph.

Similarly, the receivers at each station will receive the incoming signal from the USRP.

A decimating channel filter will remove the adjacent channels. The signal be demodulated,

and the data packets will be disassembled to recover the data. Figure 2.5 shows the RX

flowgraph.

15

Page 25: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Figure 2.3. Bit Error Probability Curve for Coherent and Noncoherent

DBPSK Demodulation in AWGN

Figure 2.4. TX Flowgraph

Figure 2.5. RX Flowgraph

2.5. Timing Diagrams for Relay System

During the first time slot, Base Station A and Base Station B will simultaneously transmit

data (e.g., picture, music, movie file) to Relay Station using DBPSK modulation. For the

16

Page 26: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

final time slot, Relay Station will perform a bitwise XOR operation with the data, and

transmit the XORed data to Base Station A and Base Station B. The received data is

decoded by performing another XOR operation with the original data (XOR’s inverse is

itself). The first time slot, final time slot, and both time slots together are illustrated in

Figures 2.6, 2.7, and 2.8 respectively.

Figure 2.6. Relay Communication System (first time slot)

Figure 2.7. Relay Communication System (final time slot)

17

Page 27: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Figure 2.8. Relay Communication System (first and final time slot)

18

Page 28: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

CHAPTER 3

RELAY NETWORK (THEORETICAL SIMULATION)

First, let’s consider a simplified system model of Figure 2.8, shown in Figure 3.1. Base

Station A, Base Station B, and Relay Station are replaced with A, B, and R respectively.

There are bidirectional communication links between A and R (A to R and R to A), and

between R and B (B to R and R to B). The four paths are denoted as AR, RA, BR, and RB

accordingly. Remember that during the first time slot, AR and BR occur (shown in Figure

2.6). During the final time slot, RA and RB occur (shown in Figure 2.7).

Figure 3.1. Simplified Relay Network

3.1. Bit Error Probability for Entire Relay Network

Considering there is no direct link between A and B, and that for DF strategy the signal is

decoded (or quantized), the four paths (AR, BR, RA, and RB) can be treated independently

as discussed in [14]. The bit error probability for node A and node B can be found by a

summation of the bit error probabilities of the four paths:

PA&Be = PAR

e (1− PBRe )(1− PRA

e )(1− PRBe )

19

Page 29: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

+PBRe (1− PAR

e )(1− PRAe )(1− PRB

e )

+PRAe (1− PAR

e )(1− PBRe )(1− PRB

e )

+PRBe (1− PAR

e )(1− PBRe )(1− PRA

e )(5)

≈ PARe + PBR

e + PRAe + PRB

e(6)

Expanding equation (3.1) produces many small terms that can be neglected for simplicity,

to yield the approximation in equation (3.2). The theoretical and simulated bit error (BER)

probability curve for the entire relay network (nodes A and B) in AWGN using MATLAB

is shown in Figure 3.2. As expected, the curves are lifted higher than the BER probability

curve for DBPSK (Figure 2.3).

Figure 3.2. Bit error probability curve for entire relay network (nodes A and

B) in AWGN

3.2. Bit Error Probability for Base Station A/B

Following the same process in calculating the BER for the entire relay, the BER for Base

Station A is dependent on the three paths; AR, BR, and RA. The bit error probability for

20

Page 30: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

node A is:

PAe = PAR

e (1− PBRe )(1− PRA

e )

+PBRe (1− PAR

e )(1− PRAe )

+PRAe (1− PAR

e )(1− PBRe )(7)

≈ PARe + PBR

e + PRAe(8)

The bit error probability for node B is dependent on the three paths; AR, BR, and RB;

it can be found as:

PBe = PAR

e (1− PBRe )(1− PRB

e )

+PBRe (1− PAR

e )(1− PRBe )

+PRBe (1− PAR

e )(1− PBRe )(9)

≈ PARe + PBR

e + PRBe(10)

The theoretical and simulated bit error (BER) probability curve for Base Station A/B

(node A/B) in AWGN using MATLAB is shown in Figure 3.3. The curves are slightly lower

than the BER probability curves for the entire network (Figure 3.2).

3.3. Bit Error Probability for Relay Station

Similarily, the BER for Relay Station is dependent on the two paths; AR and BR. The

bit error probability for node R is:

PRe = PAR

e (1− PBRe )

+PBRe (1− PAR

e )(11)

≈ PARe + PBR

e(12)

The theoretical and simulated bit error (BER) probability curve for Relay Station (node

R) in AWGN using MATLAB is shown in Figure 3.4. The curves are more recessed than the

21

Page 31: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Figure 3.3. Bit error probability curve for Base Station A/B (node A/B) in AWGN

BER probability curves for the entire network (Figure 3.2) and Base Station A/B (Figure

3.3).

Figure 3.4. Bit error probability curve for Relay Station (node R) in AWGN

22

Page 32: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

CHAPTER 4

DEVELOPMENT OF RELAY NETWORK OVER GNU RADIO

4.1. Differential Binary Phase-Shift Keying (DBPSK)

DBPSK modulation and demodulation are implemented in dbpsk.py. The source code

can be found in Appendix C.1. Figures 4.1 and 4.2 show the abstract flowgraphs for

DBPSK modulator and demodulator respectively. Following the figures are program notes

for dbpsk.py.

4.1.1. DBPSK Modulator

Figure 4.1. Abstract flowgraph for DBPSK modulator in GNU Radio

• Line(s) 28-44: The required modules are imported and global variables are declared.

Among the imported modules is psk, from another file named psk.py. It is impor-

tant that the files, psk.py and dbpsk.py, are in the same directory. The psk module

calculates the gray code and symbol constellations. The variables def verbose and

def log determines if information will be printed on the screen and logged in data

(DAT) files respectively. Both variables are initialized as false but can be changed

if desired.

• Line(s) 51-58: The dbpsk mod class is initialized using the init method. A number

of arguments are used. Notice that self is the first argument. This is always the

case for every class method and self acts as an object reference. When calling

23

Page 33: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

methods, self does not need to be specified in the argument. The init method

never returns a value.

• Line(s) 77-81: The dbpsk.py module includes both the modulator and demodula-

tor flow graphs. Hierarchical blocks are created to contain the flow graphs. The

hierarchical block for the modulator accepts a byte stream of unsigned char and

outputs a complex signal (complex float) at baseband.

• Line(s) 83-95: Several instance variables are declared using self. The value of arity

is two and the bits per symbol for DBPSK modulation is one.

• Line(s) 97-104: The bytes2chunks block is created by calling the signal processing

block packed to unpacked bb from the package gr. The function packed to unpacked bb

block unpacks every byte (8 bits) into k bit vectors. Since DBPSK uses 1-bit per

symbol, each byte stream is unpacked into eight 1-bit vectors (or chunks). The

method binary to gray is called from the module psk to calculate the gray code

constellation. In digital communications, gray coding maps a natural binary code

to symbols whose adjacent gray code point in the constellation differs by 1 bit.

This allows better error correction. Another signal processing block from GNU

Radio is called; map bb. This signal processing block maps the bit vectors to the

gray code constellation. Notice that each of the signal processing blocks share the

same suffix bb. Thus, the input and output data type is byte.

• Line(s) 106: The signal processing block diff encoder bb, is the differential encoder.

The input and output data type is byte. In order to convert dbpsk.py to BPSK,

this block may be removed. However, in doing so there must be another method

added to compensate for the phase ambiguity. One method is to scramble the bits

with a self-synchronizing pseudorandom sequence of bits (PN code). The function

scrambler bb from gr package performs this action.

– self. scrambler = gr.scrambler bb(0x8A, 0x7F, 7) # CCSDS 7-bit scrambler

24

Page 34: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

• Line(s) 108-109: The symbol constellation is calculated using the module psk.

The block, chunks2symbols, maps a stream of bytes to a complex constellation

symbol (-1+0j, 1+0j) in one dimension. The input and output data types are bits

and complex float respectively. The signal processing blocks packed to unpacked

and chunks to symbols are commonly used together to map a stream of bytes to

constellation symbols.

• Line(s) 111-119: The signal processing block firdes.root raised cosine is used to de-

sign a root raised cosine (RRC) FIR filter. The signal processing block interp fir filter

creates an interpolating FIR filter. Another RRC filter is also used for the demod-

ulator to form a raised cosine and matched filter. The data types are complex float

input, complex float output, and float taps.

• Line(s) 122-125: The blocks are connected together to create the flow graph for the

DBPSK modulator.

• Line(s) 134-182: Various functions are created. These are denoted by the reserved

word def. Among the functions are print verbage and setup logging which can be

used to print information on the screen and create DAT log files respectively.

4.1.2. Coherent DBPSK Demodulator

Figure 4.2. Abstract flowgraph for coherent DBPSK demodulator in GNU Radio

• Line(s) 191-202: The dbpsk demod class is initialized using the init method.

• Line(s) 229-233: The hierarchical block for the DBPSK demodulator flow graph

accepts an input of complex float and outputs a byte stream of unsigned char.

• Line(s) 235-248: Several instance variables are declared using self. The value of

arity is two and the bits per symbol for DBPSK modulation is one.

25

Page 35: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

• Line(s) 250-255: The signal processing block multiply const cc multiplies the com-

plex input by variable scale. This is used to scale the signal from full-range (the

full dynamic range of the USRP is ±32,768 (±215) to ±1. The signal processing

block feedforward agc is a non-casual automatic gain control (AGC). The input

and output data type is complex float.

• Line(s) 257-265: The signal processing block firdes.root raised cosine is used to de-

sign root raised cosine (RRC) FIR filter. The signal processing block interp fir filter

creates an interpolating FIR filter. This is the second RRC filter (the first in the

modulator). The pair forms a raised cosine filter and matched filter. The data

types are complex float input, complex float output, and float taps.

• Line(s) 258-273: The signal processing block mpsk receiver cc recovers the fre-

quency, phase, and symbol clock for M-Array PSK signals. The frequency and

phase recovery is performed using a Costas loop. The symbol clock synchroniza-

tion is performed using a modified Mueller and Muller circuit. The input and

output data type is complex float.

• Line(s) 267-284: The signal processing block diff phasor is a differential decoder

based on the change in phase between consecutive symbols. The input and out-

put data type is complex float. If a scrambler was added in the modulator flow

graph, the a descrambler may be used to replace this block or placed following.

The following program line creates the descrambler block by calling the function

descrambler bb from gr package:

– self. descrambler = gr.descrambler bb(0x8A, 0x7F, 7) # CCSDS 7-bit de-

scrambler

• Line(s) 289-294: The signal processing block constellation decoder cb decodes the

nearest symbol in the constellation based on where the phase shift places the symbol

on the unit circle. The input and output data types are complex float and byte

respectively.

26

Page 36: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

• Line(s) 312-315: The signal processing blocks are connected together to form the

flow graph for the DBPSK demodulator.

• Line(s) 317-402: Various functions are created. These are denoted by the keyword

def. Among the functions are print verbage and setup logging which can be used

to print information on the screen and create DAT log files respectively.

4.2. Relay Station

The main files for Relay Station application include:

• combine.py (Appendix C.3.1)

• combine path.py (Appendix C.3.2)

The following is the script used to execute the Relay Station application at the junction

node to simultaneously communicate with Base Station A and Base Station B:

$ ./combine.py -f 450M -w 0 -m bpsk -r 500k

where the frequency set by the suffix -f is arbitrary (the RX and TX frequencies are coded

in combine path.py), and suffix -m specifies the modulation scheme (modulations besides

DBPSK are available, e.g., GMSK, QPSK, etc.). The particular value ”bpsk” for suffix -m

refers to the modulation scheme in the file bpsk.py in the same directory. The bpsk.py file

can be modified or renamed to implement another modulation technique. The parameters

for suffix -m must match for TX and RX sides, i.e., the modulation scheme must match

to properly demodulate the data. Run the Relay Station application first (receive mode),

and after all the data from Base Station A and B have been received, press ”Enter” on the

keyboard to begin transmission (transmit mode).

4.3. Base Station A/B

The source code for Base Station A and Base Station B are equivalent. The main files

for Base Station A/B application include:

• combine.py (Appendix C.2.1)

• transmit path.py (Appendix C.2.2)

27

Page 37: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

• usrp transmit path.py (Appendix C.2.3).

• receive path.py (Appendix C.2.4)

• usrp receive path.py (Appendix C.2.5).

• generic usrp.py (Appendix C.2.6)

The following are scripts used to execute the Base Station A/B application at peer one and

peer two:

Terminal 1

$ ./combine.py –tx-freq 400M –rx-freq 450M -w 0 -u 1 -m bpsk –from-file pic.jpg -r

500k

Terminal 2

$ ./combine.py –tx-freq 2.425G –rx-freq 2.475G -w 1 -u 1 -m bpsk –from-file pic.jpg

-r 500k

When the Base Station applications are initiated, press ”Enter” on the keyboard for each

terminal window to begin transmitting data to the Relay Station application. Once the

transmissions are finished, the Base Station applications will enter standby mode to receive

data from the Relay Station.

4.4. Onscreen Display for Relay Network Applications

Executing the applications for Base Station A/B and relay Station will display infor-

mation on the terminal window in real-time. During a transmission phase, a dot will print

onscreen for every data packet sent:

$ Ready to receive packets

$ Press Enter to start transmitting packets

$ Transmitting packets

$ ......................................................................................

During a receive phase, the following data is displayed onscreen:

$ ok = True pktno = 463 n rcvd = 464 n right = 464

28

Page 38: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

$ ok = True pktno = 464 n rcvd = 465 n right = 465

$ ok = True pktno = 465 n rcvd = 466 n right = 466

where each line represents a received data packet and its information. Table 4.1 gives de-

scriptions for the onscreen variables and their values.

Table 4.1. Receive Phase Onscreen Display

Variable Value

ok If the payload passes the CRC check, ”ok” will be

”True”. Otherwise, ”ok” will be False.

pktno The data packet number encoded in the header of each

payload for labeling and tracking. The value is a natural

number starting from zero.

n rcvd A counter to keep track of the total number of received

data packets regardless of wether the payload passes the

CRC check or not.

n right A counter to keep track of the total number of correctly

received data packets (payload passes the CRC check).

4.5. Streaming Music using GStreamer

The Relay Network applications can also be used to stream music using GStreamer, a

multimedia framework preinstalled on Ubuntu similar to [1]. To begin using GStreamer with

GNU Radio, install the additional package using Synaptic Package Manager:

• gstreamer0.10-tools

Execute the following script for Relay Application at computer one:

$ ./combine.py -f 450M -w 0 -m bpsk -r 500k –tx-amplitude 3000 -M 7

To transmit a music file from Base Station A, and a picture file from Base Station B, execute

the following scripts at computer two in order:

29

Page 39: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Terminal 1

$ mkfifo /home/YOURUSERNAME/Desktop/BASESTATIONBFOLDER/received.dat

$ gst-launch playbin num-buffers=6000 uri=file:/home/YOURUSERNAME/Desktop

/BASESTATIONBFOLDER/received.dat

Terminal 2

$ ./combine.py –tx-freq 400M –rx-freq 450M -w 0 -u 1 -m bpsk –from-file music.mp3

-r 500k -M 7

Terminal 3

$ ./combine.py –tx-freq 2.425G –rx-freq 2.475G -w 1 -u 1 -m bpsk –from-file pic.jpg

-r 500k

The Relay Application will play the music in real-time as it is transmitted and received. For

best results, use an MP3 music file (encoded with Constant Bitrate (CBR)) and a JPEG

picture file.

4.6. Complete Abstract Flowgraphs for TX and RX

Shown in Figure 4.3 is the full TX flowgraph; encompassing Figures 1.9, 2.1, 2.4, and

4.1. Correspondingly, Figure 4.4 shows the full RX flowgraph; encompassing Figures 1.9,

2.5, and 4.2.

30

Page 40: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Figure 4.3. Complete TX Flowgraph

Figure 4.4. Complete RX Flowgraph

31

Page 41: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

CHAPTER 5

CONCLUSION

In this thesis a bidirectional two-hop relay network with decode-and-forward strategy

is implemented using GNU Radio (software) and several USRPs (hardware) on Ubuntu

(operating system). The relay communication system worked perfectly and was able to

successfully relay data to each station.

5.1. Future Work

Possible improvements to the system may include:

• Using one channel and receive, i.e., one antenna at Relay Station and superposi-

tioning of the signals and source separation. This will greatly increase throughput.

• Modulate with BPSK using a known bit sequence to compensate for phase ambi-

guity at the receiver.

• Enable retransmission when a data packet does not pass check.

• Alternative to CRC, e.g., Forward Error Correction (FEC) for better perfomance.

• Encoder and decoder for error correction.

• Multiple relays for cooperative diversity.

32

Page 42: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

33

APPENDIX A

GNU RADIO INSTALLATION GUIDE

Page 43: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

With the latest releases of GNU Radio, there are binary and source packages avail-

able. This guide will cover the installation of Ubuntu (operating system based on Debian

GNU/Linux distribution), the package prerequisites for GNU Radio, and GNU Radio.

A.1. Stage 1: Install Ubuntu

It is recommended to install Ubuntu onto its own hard drive partition (as opposed to

installing Ubuntu as a virtual machine or a windows application) to ensure full compatibility

with GNU Radio.

(i) Visit the GNU Radio homepage [18]. Note the Download and Build Guide sections.

• Download an ISO image for Ubuntu, e.g., Ubuntu Desktop 32-bit, and burn

it to a DVD as an ISO image.

• Insert the Ubuntu DVD into a DVD drive. Boot from the DVD and install

Ubuntu.

(ii) Perform a system update in Ubuntu.

• System → Administration → Update Manager → Check → Install Updates

(iii) Add the terminal launcher to panel. The terminal is a command line interface

(CLI) where commands can be typed to perform actions. The terminal will be

used often.

• Applications → Accessories → Right-click on terminal → Add this launcher

to panel

(iv) Configure copy and paste keyboard shortcuts for the terminal.

• Terminal → Edit → Keyboard Shortcuts → Configure copy and paste key-

board shortcuts

(v) Extend the computer idle time. This step is recommended because Ubuntu dims

the screen (lowers the screen brightness) once idle.

• System → Preferences → Screensaver → Increase computer idle time

A.2. Stage 2: Install Package Prerequisites

(i) Visit the GNU Radio homepage. Note the Download and Build Guide sections.

34

Page 44: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

• http://gnuradio.org/redmine/wiki/gnuradio

(ii) Run the install script for the package prerequisites (available in Build Guide section)

in the terminal.

A.3. Stage 3: Install GNU Radio

(i) Download the GNU Radio tar archive (tarball), e.g., gnuradio-3.3.0.tar.gz. Extract

the tarball to the desktop.

• The extracted folder contains installation files and many example programs.

• This folder will be frequently referred to as the Source Folder, e.g., gnuradio-

3.3.0.

• Keep the Source Folder or a copy on the desktop for easy access.

(ii) In the Terminal, navigate to the Source Folder and run the following commands

to install GNU Radio. The listing following the terminal commands shows the

terminal window output after completing the make check command.

$ ./configure

$ make

$ make check

$ sudo make install

Shown in Appendix A.7 are components that did not pass configuration checks and may

be excluded. Table A.1 lists these components and their reasons for exclusion:

A.4. Stage 4: Configure USRP1 Support

This stage may be omitted if the USRP1 hardware is not used, or the UHD software

driver is installed. Otherwise, configure USRP1 support by running the following commands

in the terminal. Substitute <YOUR USERNAME> with the appropriate username.

$ sudo addgroup usrp

$ sudo usermod -G usrp -a <YOUR USERNAME>

$ echo ’ACTION==”add”, BUS==”usb”, SYSFSidVendor==”fffe”,

SYSFSidProduct==”0002”, GROUP:=”usrp”, MODE:=”0660”’ > tmpfile

35

Page 45: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Table A.1. Excluded Components After Make Check

Component Reason for Exclusion

gcell and gr-gcell Gcell and gr-gcell are only for the IBM Cell processor.

gr-audio-jack Gr-audio-jack is the GR audio interface to the Jack audio system.

Gr-audio-alsa is built, so there’s no need to build gr-audio-jack.

gr-audio-osx Gr-audio-osx only builds on OS/X.

gr-audio-portaudio Gr-audio-portaudio not needed.

gr-audio-windows Gr-audio-windows only builds on Microsoft Windows.

$ sudo chown root.root tmpfile

$ sudo mv tmpfile /etc/udev/rules.d/10-usrp.rules

$ sudo udevadm control –reload-rules

A.5. Stage 5: Address Broken libtool

Address the broken libtool implementation on Debian and Ubuntu by invoking the fol-

lowing commands in the terminal:

$ cp /etc/ld.so.conf /tmp/ld.so.conf

$ echo /usr/local/lib >> /tmp/ld.so.conf

$ sudo mv /tmp/ld.so.conf /etc/ld.so.conf

$ sudo ldconfig

A.6. Stage 6: Enable Real-Time Scheduling

Enable real-time scheduling for non-root users in the group USRP. This allows real-time

priority and more CPU resources to the group.

”Realtime scheduling changes the scheduling policy of selected threads to a priority over

normal threads. In Linux, root access is required in order to do so. It should result in more

lower latencies and more deterministic behavior.” - Thomas Tsou from the GNU Radio

Mailing Lists

Otherwise, the error below may appear while running some applications in GNU Radio:

36

Page 46: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

$ Warning: failed to enable realtime scheduling

To enable real-time scheduling:

(i) Open the text editor Gedit with root permissions by running the following com-

mand in the terminal:

$ sudo gedit

(ii) In Gedit, open file limits.conf in the following directory:

• /etc/security/limits.conf

(iii) Add the following line to file limits.conf and save:

• @usrp - rtprio 50

(iv) Run the following command in the terminal:

$ sudo ldconfig

(v) Log out and log back in, or restart the computer.

37

Page 47: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

A.7. Terminal Window Output After make check

1 The f o l l o w i n g components were skipped e i t h e r because you asked

2 not to bu i ld them or they didn ’ t pass c o n f i g u r a t i o n checks :

3

4 usrp2−f i rmware

5

6 These components w i l l not be b u i l t .

7

8 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗

9 The f o l l o w i n g GNU Radio components have been s u c c e s s f u l l y

10 con f i gu r ed :

11

12 c o n f i g

13 g rue l

14 omnithread

15 gnuradio−core

16 pmt

17 mblock

18 usrp

19 usrp2

20 gr−usrp

21 gr−usrp2

22 gr−audio−a l s a

23 gr−audio−os s

24 gr−cvsd−vocoder

25 gr−gpio

38

Page 48: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

26 gr−gsm−f r−vocoder

27 gr−pager

28 gr−radar−mono

29 gr−radio−astronomy

30 gr− t r e l l i s

31 gr−video−s d l

32 gr−wxgui

33 gr−qtgu i

34 gr−sounder

35 gr−u t i l s

36 gnuradio−examples

37 grc

38 docs

39

40 You my now run the make command to bu i ld these components .

41

42 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗

43 The f o l l o w i n g components were skipped e i t h e r because you asked

44 not to bu i ld them or they didn ’ t pass c o n f i g u r a t i o n checks :

45

46 g c e l l

47 gr−g c e l l

48 gr−audio−j ack

49 gr−audio−osx

50 gr−audio−portaudio

51 gr−audio−windows

39

Page 49: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

52

53 These components w i l l not be b u i l t .

40

Page 50: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

41

APPENDIX B

EXAMPLES IN GNU RADIO

Page 51: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

The GNU Radio source folder, the extracted folder from the tarball containing the in-

stallation files, has many useful programs to study and try out. Some of these programs

are applications, i.e., stand-alone programs that can be run in the terminal. A majority of

the applications can be found in the folder gnuradio-examples. The applications may have

an options menu to provide more information on the their usage. The options menu can be

accessed by entering the suffix -h or –help in the terminal:

$ ./APPLICATIONNAME.py -h

$ ./APPLICATIONNAME.py –help

Other programs in the GNU Radio source folder are modules that can be imported when

writing programs in GNU Radio. An example of applications are the benchmarking TX and

RX programs by Digital-Bert where two USRPs, one running the TX program and the other

running the RX program, can send information packets with onscreen display of information

regarding the transaction, such as receive confirmation. Examples of modules include a

wide variety of modulation schemes such as PSK, DPSK, QAM, GSMK, and OFDM. It

is recommended to search through the GNU Radio source folder, paying close attention to

files with the python extension (py). In the following sections, several applications and the

DBPSK module will be examined and a tutorial for Digital-Berts benchmarking applications

will be given. The relay communication system is based on the benchmark applications. Visit

[15] for tutorials on coding in python and [4] for more projects in GNU Radio.

B.1. Example 1: dial tone.py

The dial tone example generates two sine waves (US telephone dial tones) and sends

them to the sound card in the left and right channels. To execute the dial tone application,

use the following command in the terminal:

$ ./dial tone.py

The source code for dial tone.py can be found in Appendix B.5. Below are a few program

notes for dial tone.py:

42

Page 52: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

• Line(s) 1: The line # !/usr/bin/env python, known as a shebang, is the first line

included in applications in GNU Radio. The beginning symbol # indicates that

the line is a comment, so that the python interpreter ignores the line. The shebang

instructs the shell what to do with the file if an interpreter is not specified, i.e.,

$ ./benchmark rx.py

vs

$ python benchmark rx.py

• Line(s) 23-26: The required modules are imported. The basic GNU Radio module

is gr (from gnuradio-package) and is necessary for every GNU Radio applications.

Module gr contains the digital signal processing blocks.

• Line(s) 28: A class called my top block is defined which will contain the flow graph.

It is derived from another class called top block from module gr (gr.top block).

• Line(s) 34-36: Allows the user to specify the PCM output device by using suffix

-O. It is recommended to use plughw:0,0 (if supported by the sound card), since it

has a resampler.

$ ./dial tone.py -O plughw:0,0

• Line(s) 37-39: Allows the user to specify sampling rate (default value is 48000) by

using suffix -r, e.g.,

$ ./dial tone.py -O plughw:0,0 -r 48000

• Line(s) 48-49: Two sine waves are generated by calling function sig source f from

module gr. The return value is stored in variable srcX to create two blocks; src0

and src1. A block in a flow graph is created by calling a function, and storing the

return value to a variable. In this case, the scrX blocks are source blocks (initial

blocks in a flow graph). The function sig source f has suffix f to indicate it accepts

and outputs data type float. There are three parameters; sample rate, wave type

(e.g., sine, cosine, square), frequency, and the amplitude.

43

Page 53: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

• Line(s) 50: The sink function is called from the audio module where data is pro-

cessed and sent to the sound card. Block dst is a sink block (end block in a flow

graph)

• Line(s) 50-52: Two source blocks srcX are connected to two inputs in sink block

dst. Every flow graph usually needs to have at least one source block and one sink

block.

• Line(s) 55-59: The schedule for the flow graph of top block is controlled here.

Control logic, such as the keyboard interrupt, can be implemented here. Class

my top block which contains the flow graph starts by using function start(), and

ends with function stop() once the Enter key is pressed.

The flow graph for the dial tone example is recreated in GNU Radio Companion [12],

and is shown in the figure below:

Figure B.1. dial tone.py

B.2. Example 2: usrp benchmark usb.py

Application usrp benchmark usb.py tests the throughput between the USB and the

USRP. The USRP interfaces with the computer through a USB port. A USRP is needed to

44

Page 54: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

run this example. The following figure shows the output window after executing the USRP

benchmark USB application.

$ ./usrp benchmark usb.py

Figure B.2. usrp benchmark usb

B.3. Example 3: usrp siggen.py, usrp oscope.py, and usrp fft.py

Application usrp siggen.py generates a waveform (e.g., sine, constant, uniform, or gauss-

ian), modulates the signal with carrier, and transmits it over-the-air using USRP. Application

usrp oscope.py is an oscilloscope and usrp fft.py is a spectrum analyzer. These applications

can be used together to test if the USRP is working properly. The applications can be run

45

Page 55: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

concurrently in separate terminals. The terminals can be on the same or separate machines.

The following are sample scripts to run the applications:

Terminal 1

$ ./usrp siggen.py -f 450M

Terminal 2

$ ./usrp oscope.py -f 450M

B.4. Example 4: usrp probe

Some applications in GNU Radio require the user to specify information pertaining to

the configuration of the USRP hardware, e.g., USRP unit number (multiple USRPs can be

connected to the computer), requested center frequency (different frequency band for each

daughterboard), placement of the daughterboard (Side A or Side B), etc. A simple method

to retreive information regarding the connected USRP, is running the application usrp probe

from the terminal. This application is integrated into GNU Radio, and can be invoked in

the terminal regardless of the current directory.

$ usrp probe

Figure B.3 displays the application window of the application usrp probe. Several infor-

mation on the connected USRP can be seen:

• The USRP unit number is 0 (as more USRPs are detected the Unit Number can

be 0,1,2, etc.)

• The user may select which USRP daughterboard side (Side A or Side B) and

subdevice (TX or RX) to ”probe” for information.

• Daughterboard name (in this case it is RFX2400).

• Converter rate, i.e., clock rate, sampling rate, etc.

46

Page 56: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

Figure B.3. usrp probe Application Window

B.5. Source Code for dial tone.py

1 #!/ usr / b in /env python

2 #

3 # Copyright 2004 ,2005 ,2007 Free Sof tware Foundation , Inc .

4 #

5 # This f i l e i s par t o f GNU Radio

6 #

7 # GNU Radio i s f r e e so f tware ; you can r e d i s t r i b u t e i t and/or modify

8 # i t under the terms o f the GNU General Pub l i c License as pub l i s h ed by

47

Page 57: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

9 # the Free Sof tware Foundation ; e i t h e r ve r s i on 3 , or ( at your opt ion )

10 # any l a t e r ve r s i on .

11 #

12 # GNU Radio i s d i s t r i b u t e d in the hope t ha t i t w i l l be u s e fu l ,

13 # but WITHOUT ANY WARRANTY; wi thout even the imp l i ed warranty o f

14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

15 # GNU General Pub l i c License f o r more d e t a i l s .

16 #

17 # You shou ld have r e c e i v ed a copy o f the GNU General Pub l i c License

18 # along wi th GNU Radio ; see the f i l e COPYING. I f not , wr i t e to

19 # the Free Sof tware Foundation , Inc . , 51 Frank l in S t ree t ,

20 # Boston , MA 02110−1301 , USA.

21 #

22

23 from gnuradio import gr

24 from gnuradio import audio

25 from gnuradio . eng opt ion import eng opt ion

26 from optparse import OptionParser

27

28 class my top block ( gr . top b lock ) :

29

30 def i n i t ( s e l f ) :

31 gr . top b lock . i n i t ( s e l f )

32

33 par s e r = OptionParser ( o p t i o n c l a s s=eng opt ion )

34 par s e r . add opt ion ( ”−O” , ”−−audio−output ” , type=” s t r i n g ” ,

35 d e f a u l t=”” ,

36 help=”pcm output dev i ce name . E. g . , hw: 0 , 0 or /dev/dsp” )

37 par s e r . add opt ion ( ”−r ” , ”−−sample−r a t e ” , type=” e n g f l o a t ” ,

38 d e f a u l t =48000 ,

48

Page 58: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

39 help=” s e t sample ra t e to RATE (48000) ” )

40 ( opt ions , args ) = par s e r . p a r s e a r g s ( )

41 i f l en ( args ) != 0 :

42 par s e r . p r i n t h e l p ( )

43 raise SystemExit , 1

44

45 sample rate = i n t ( opt ions . sample rate )

46 ampl = 0 .1

47

48 s r c0 = gr . s i g s o u r c e f ( sample rate , gr .GR SIN WAVE, 350 , ampl )

49 s r c1 = gr . s i g s o u r c e f ( sample rate , gr .GR SIN WAVE, 440 , ampl )

50 dst = audio . s ink ( sample rate , opt ions . audio output )

51 s e l f . connect ( src0 , ( dst , 0 ) )

52 s e l f . connect ( src1 , ( dst , 1 ) )

53

54

55 i f name == ’ ma in ’ :

56 s e l f = my top block ( )

57 s e l f . s t a r t ( )

58 raw input ( ’ Press Enter to qu i t : ’ )

59 s e l f . s top ( )

49

Page 59: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

50

APPENDIX C

SOURCE CODES FOR RELAY SYSTEM OVER GNU RADIO

Page 60: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

C.1. Source Code for dbpsk.py

1 #

2 # Copyright 2005 ,2006 ,2007 ,2009 Free Sof tware Foundation , Inc .

3 #

4 # This f i l e i s par t o f GNU Radio

5 #

6 # GNU Radio i s f r e e so f tware ; you can r e d i s t r i b u t e i t and/or modify

7 # i t under the terms o f the GNU General Pub l i c License as pub l i s h ed by

8 # the Free Sof tware Foundation ; e i t h e r ve r s i on 3 , or ( at your opt ion )

9 # any l a t e r ve r s i on .

10 #

11 # GNU Radio i s d i s t r i b u t e d in the hope t ha t i t w i l l be u s e fu l ,

12 # but WITHOUT ANY WARRANTY; wi thout even the imp l i ed warranty o f

13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

14 # GNU General Pub l i c License f o r more d e t a i l s .

15 #

16 # You shou ld have r e c e i v ed a copy o f the GNU General Pub l i c License

17 # along wi th GNU Radio ; see the f i l e COPYING. I f not , wr i t e to

18 # the Free Sof tware Foundation , Inc . , 51 Frank l in S t ree t ,

19 # Boston , MA 02110−1301 , USA.

20 #

21

22 # See gnuradio−examples /python/ d i g i t a l f o r examples

23

24 ”””

25 d i f f e r e n t i a l BPSK modulation and demodulat ion .

26 ”””

27

28 from gnuradio import gr , gru , m o d u l a t i o n u t i l s

29 from math import pi , s q r t

51

Page 61: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

30 import psk

31 import cmath

32 from ppr int import ppr int

33

34 # de f a u l t v a l u e s ( used in i n i t and add op t ions )

35 de f samp le s pe r symbo l = 2

36 d e f e x c e s s b w = 0.35

37 d e f g r a y c o d e = True

38 d e f v e r b o s e = False

39 d e f l o g = False

40

41 d e f c o s t a s a l p h a = 0 .1

42 de f ga in mu = None

43 def mu = 0 .5

44 d e f o m e g a r e l a t i v e l i m i t = 0.005

45

46

47 # /////////////////////////////////////////////////////////////////////

48 # DBPSK modulator

49 # /////////////////////////////////////////////////////////////////////

50

51 class dbpsk mod ( gr . h i e r b l o c k 2 ) :

52

53 def i n i t ( s e l f ,

54 samples per symbol= de f sample s per symbo l ,

55 excess bw= de f exce s s bw ,

56 gray code= de f g ray code ,

57 verbose= de f v e rbo s e ,

58 log= d e f l o g ) :

59 ”””

52

Page 62: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

60 Hie ra r ch i c a l b l o c k f o r RRC− f i l t e r e d d i f f e r e n t i a l BPSK modulation .

61

62 The input i s a by t e stream ( unsigned char ) and the

63 output i s the complex modulated s i g n a l a t baseband .

64

65 @param samples per symbo l : samples per baud >= 2

66 @type samples per symbo l : i n t e g e r

67 @param excess bw : Root−r a i s ed cos ine f i l t e r exce s s bandwidth

68 @type excess bw : f l o a t

69 @param gray code : Te l l modulator to Gray code the b i t s

70 @type gray code : boo l

71 @param verbose : Print in format ion about modulator ?

72 @type verbose : boo l

73 @param lo g : Log modulation data to f i l e s ?

74 @type l o g : boo l

75 ”””

76

77 gr . h i e r b l o c k 2 . i n i t ( s e l f , ”dbpsk mod” ,

78 # Input s i gna tu r e

79 gr . i o s i g n a t u r e (1 , 1 , gr . s i z e o f c h a r ) ,

80 # Output s i gna tu r e

81 gr . i o s i g n a t u r e (1 , 1 , gr . s i z e o f g r c o m p l e x ) )

82

83 s e l f . samples per symbol = samples per symbol

84 s e l f . exces s bw = excess bw

85 s e l f . g ray code = gray code

86

87 i f not i s i n s t a n c e ( s e l f . samples per symbol , i n t ) \

88 or s e l f . samples per symbol < 2 :

89 raise TypeError , \

53

Page 63: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

90 ( ”sbp must be an i n t e g e r >= 2 , i s %d” \

91 % s e l f . samples per symbol )

92

93 ntaps = 11 ∗ s e l f . samples per symbol

94

95 a r i t y = pow(2 , s e l f . b i t s pe r symbo l ( ) )

96

97 # turn by t e s in t o k−b i t v e c t o r s

98 s e l f . bytes2chunks = \

99 gr . packed to unpacked bb ( s e l f . b i t s pe r symbo l ( ) , gr . GR MSB FIRST)

100

101 i f s e l f . g ray code :

102 s e l f . symbol mapper = gr . map bb ( psk . b ina ry to g ray [ a r i t y ] )

103 else :

104 s e l f . symbol mapper = gr . map bb ( psk . b inary to ungray [ a r i t y ] )

105

106 s e l f . d i f f e n c = gr . d i f f e n c o d e r b b ( a r i t y )

107

108 s e l f . chunks2symbols = \

109 gr . chunks to symbols bc ( psk . c o n s t e l l a t i o n [ a r i t y ] )

110

111 # pu l s e shaping f i l t e r

112 s e l f . r r c t a p s = gr . f i r d e s . r o o t r a i s e d c o s i n e (

113 s e l f . samples per symbol , # gain ( samples per symbo l s ince we ’ re

114 # in t e r p o l a t i n g by samples per symbo l )

115 s e l f . samples per symbol , # sampling ra t e

116 1 . 0 , # symbol ra t e

117 s e l f . excess bw , # exce s s bandwidth ( r o l l−o f f f a c t o r )

118 ntaps )

119 s e l f . r r c f i l t e r = gr . i n t e r p f i r f i l t e r c c f ( s e l f . samples per symbol ,

54

Page 64: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

120 s e l f . r r c t a p s )

121

122 # Connect

123 s e l f . connect ( s e l f , s e l f . bytes2chunks , s e l f . symbol mapper ,

124 s e l f . d i f f e n c , s e l f . chunks2symbols ,

125 s e l f . r r c f i l t e r , s e l f )

126

127 i f verbose :

128 s e l f . p r i n t v e r b a g e ( )

129

130 i f l og :

131 s e l f . s e t u p l o g g i n g ( )

132

133

134 def samples per symbol ( s e l f ) :

135 return s e l f . samples per symbol

136

137 # s t a t i c method t ha t ’ s a l s o c a l l a b l e on an ins tance

138 def b i t s pe r symbo l ( s e l f=None ) :

139 return 1

140 # make i t a s t a t i c method . RTFM

141 b i t s pe r symbo l = stat icmethod ( b i t s pe r symbo l )

142

143 def add opt ions ( par s e r ) :

144 ”””

145 Adds DBPSK modulation−s p e c i f i c op t i ons to the s tandard parser

146 ”””

147 par s e r . add opt ion ( ”” , ”−−excess−bw” , type=” f l o a t ” ,

148 d e f a u l t= de f exce s s bw ,

149 help=” s e t RRC exce s s bandwith f a c t o r [ d e f a u l t=%d e f a u l t ] ” )

55

Page 65: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

150 par s e r . add opt ion ( ”” , ”−−no−gray−code ” , des t=” gray code ” ,

151 ac t i on=” s t o r e f a l s e ” , d e f a u l t=True ,

152 help=” d i s a b l e gray coding on modulated b i t s (PSK) ” )

153 add opt ions=stat icmethod ( add opt ions )

154

155 def ex t ra c t kwarg s f r om opt i on s ( opt ions ) :

156 ”””

157 Given command l i n e opt ions ,

158 c r ea t e d i c t i ona r y s u i t a b l e f o r pas s ing to i n i t

159 ”””

160 return m o d u l a t i o n u t i l s . ex t ra c t kwarg s f r om opt i on s ( dbpsk mod . i n i t ,

161 ( ’ s e l f ’ , ) , opt i ons )

162 ex t rac t kwarg s f r om opt i on s=stat icmethod ( ex t rac t kwarg s f r om opt i on s )

163

164

165 def p r i n t v e r b a g e ( s e l f ) :

166 print ”\nModulator : ”

167 print ” b i t s per symbol : %d” % s e l f . b i t s pe r symbo l ( )

168 print ”Gray code : %s ” % s e l f . g ray code

169 print ”RRC r o l l−o f f f a c t o r : %.2 f ” % s e l f . exces s bw

170

171 def s e t u p l o g g i n g ( s e l f ) :

172 print ”Modulation l ogg ing turned on . ”

173 s e l f . connect ( s e l f . bytes2chunks ,

174 gr . f i l e s i n k ( gr . s i z e o f c h a r , ” tx bytes2chunks . dat ” ) )

175 s e l f . connect ( s e l f . symbol mapper ,

176 gr . f i l e s i n k ( gr . s i z e o f c h a r , ” tx graycoder . dat ” ) )

177 s e l f . connect ( s e l f . d i f f e n c ,

178 gr . f i l e s i n k ( gr . s i z e o f c h a r , ” t x d i f f e n c . dat ” ) )

179 s e l f . connect ( s e l f . chunks2symbols ,

56

Page 66: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

180 gr . f i l e s i n k ( gr . s i z e o f g r c o m p l e x , ” tx chunks2symbols . dat ” ) )

181 s e l f . connect ( s e l f . r r c f i l t e r ,

182 gr . f i l e s i n k ( gr . s i z e o f g r c o m p l e x , ” t x r r c f i l t e r . dat ” ) )

183

184

185 # /////////////////////////////////////////////////////////////////////

186 # DBPSK demodulator

187 #

188 # D i f f e r e n t i a l l y coherent d e t e c t i on o f d i f f e r e n t i a l l y encoded BPSK

189 # /////////////////////////////////////////////////////////////////////

190

191 class dbpsk demod ( gr . h i e r b l o c k 2 ) :

192

193 def i n i t ( s e l f ,

194 samples per symbol= de f sample s per symbo l ,

195 excess bw= de f exce s s bw ,

196 c o s t a s a l p h a= d e f c o s t a s a l p h a ,

197 gain mu= def gain mu ,

198 mu= def mu ,

199 o m e g a r e l a t i v e l i m i t= d e f o m e g a r e l a t i v e l i m i t ,

200 gray code= de f g ray code ,

201 verbose= de f v e rbo s e ,

202 log= d e f l o g ) :

203 ”””

204 Hie ra r ch i c a l b l o c k f o r RRC− f i l t e r e d d i f f e r e n t i a l BPSK demodulat ion

205

206 The input i s the complex modulated s i g n a l a t baseband .

207 The output i s a stream of b i t s packed 1 b i t per by t e (LSB)

208

209 @param samples per symbo l : samples per symbol >= 2

57

Page 67: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

210 @type samples per symbo l : f l o a t

211 @param excess bw : Root−r a i s ed cos ine f i l t e r exce s s bandwidth

212 @type excess bw : f l o a t

213 @param co s t a s a l p ha : loop f i l t e r gain

214 @type c o s t a s a l p h a s : f l o a t

215 @param gain mu : f o r M&M b lo c k

216 @type gain mu : f l o a t

217 @param mu: f o r M&M b lo c k

218 @type mu: f l o a t

219 @param omeg a r e l a t i v e l im i t : f o r M&M b lo c k

220 @type ome g a r e l a t i v e l im i t : f l o a t

221 @param gray code : Te l l modulator to Gray code the b i t s

222 @type gray code : boo l

223 @param verbose : Print in format ion about modulator ?

224 @type verbose : boo l

225 @param debug : Print modualt ion data to f i l e s ?

226 @type debug : boo l

227 ”””

228

229 gr . h i e r b l o c k 2 . i n i t ( s e l f , ”dbpsk demod” ,

230 # Input s i gna tu r e

231 gr . i o s i g n a t u r e (1 , 1 , gr . s i z e o f g r c o m p l e x ) ,

232 # Output s i gna tu r e

233 gr . i o s i g n a t u r e (1 , 1 , gr . s i z e o f c h a r ) )

234

235 s e l f . samples per symbol = samples per symbol

236 s e l f . exces s bw = excess bw

237 s e l f . c o s t a s a l p h a = c o s t a s a l p h a

238 s e l f . mm gain mu = gain mu

239 s e l f . mm mu = mu

58

Page 68: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

240 s e l f . mm omega re la t ive l im i t = o m e g a r e l a t i v e l i m i t

241 s e l f . g ray code = gray code

242

243 i f samples per symbol < 2 :

244 raise TypeError ,

245 ” samples per symbol must be >= 2 , i s %r ” \

246 % ( samples per symbol , )

247

248 a r i t y = pow(2 , s e l f . b i t s pe r symbo l ( ) )

249

250 # Automatic gain con t r o l

251 #sca l e = (1 .0/16384 .0)

252 #s e l f . p r e s c a l e r = gr . mu l t i p l y c on s t c c ( s c a l e )

253 # sca l e the s i g n a l from f u l l −range to +−1

254 s e l f . agc = gr . agc2 cc ( 0 . 6 e−1, 1e−3, 1 , 1 , 100)

255 #s e l f . agc = gr . f e ed f o rward agc cc (16 , 2 .0 )

256

257 # RRC data f i l t e r

258 ntaps = 11 ∗ samples per symbol

259 s e l f . r r c t a p s = gr . f i r d e s . r o o t r a i s e d c o s i n e (

260 1 . 0 , # gain

261 s e l f . samples per symbol , # sampling ra t e

262 1 . 0 , # symbol ra t e

263 s e l f . excess bw , # exce s s bandwidth ( r o l l−o f f f a c t o r )

264 ntaps )

265 s e l f . r r c f i l t e r=gr . i n t e r p f i r f i l t e r c c f (1 , s e l f . r r c t a p s )

266

267 # symbol c l o c k recovery

268 i f not s e l f . mm gain mu :

269 s e l f . mm gain mu = 0.1

59

Page 69: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

270

271 s e l f . mm omega = s e l f . samples per symbol

272 s e l f . mm gain omega = \

273 .25 ∗ s e l f . mm gain mu ∗ s e l f . mm gain mu

274 s e l f . c o s t a s b e t a = \

275 0 .25 ∗ s e l f . c o s t a s a l p h a ∗ s e l f . c o s t a s a l p h a

276 fmin = −0.25

277 fmax = 0.25

278

279 s e l f . r e c e i v e r=gr . mpsk r e c e i v e r c c ( a r i ty , 0 ,

280 s e l f . c o s t a s a lpha , s e l f . c o s t a s b e t a ,

281 fmin , fmax ,

282 s e l f . mm mu, s e l f . mm gain mu ,

283 s e l f . mm omega , s e l f . mm gain omega ,

284 s e l f . mm omega re la t ive l imi t )

285

286 # Do d i f f e r e n t i a l decoding based on phase change o f symbols

287 s e l f . d i f f d e c = gr . d i f f p h a s o r c c ( )

288

289 # f ind c l o s e s t c o n s t e l l a t i o n po in t

290 ro t = 1

291 r o t a t e d c o n s t = map(lambda pt : pt ∗ rot ,

292 psk . c o n s t e l l a t i o n [ a r i t y ] )

293 s e l f . s l i c e r = \

294 gr . c o n s t e l l a t i o n d e c o d e r c b ( ro ta t ed cons t , range ( a r i t y ) )

295

296 i f s e l f . g ray code :

297 s e l f . symbol mapper = \

298 gr . map bb ( psk . g r ay to b ina ry [ a r i t y ] )

299 else :

60

Page 70: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

300 s e l f . symbol mapper = \

301 gr . map bb ( psk . ungray to b inary [ a r i t y ] )

302

303 # unpack the k b i t v e c t o r in t o a stream of b i t s

304 s e l f . unpack = gr . unpack k b i t s bb ( s e l f . b i t s pe r symbo l ( ) )

305

306 i f verbose :

307 s e l f . p r i n t v e r b a g e ( )

308

309 i f l og :

310 s e l f . s e t u p l o g g i n g ( )

311

312 # Connect and I n i t i a l i z e base c l a s s

313 s e l f . connect ( s e l f , s e l f . agc , s e l f . r r c f i l t e r ,

314 s e l f . r e c e i v e r , s e l f . d i f f d e c , s e l f . s l i c e r ,

315 s e l f . symbol mapper , s e l f . unpack , s e l f )

316

317 def samples per symbol ( s e l f ) :

318 return s e l f . samples per symbol

319

320 # sta t i cme thod t ha t ’ s a l s o c a l l a b l e on an ins tance

321 def b i t s pe r symbo l ( s e l f=None ) :

322 return 1

323 # make i t a s t a t i c method . RTFM

324 b i t s pe r symbo l = stat icmethod ( b i t s pe r symbo l )

325

326 def p r i n t v e r b a g e ( s e l f ) :

327 print ”\nDemodulator : ”

328 print ” b i t s per symbol : %d” % s e l f . b i t s pe r symbo l ( )

329 print ”Gray code : %s ” % s e l f . g ray code

61

Page 71: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

330 print ”RRC r o l l−o f f f a c t o r : %.2 f ” % s e l f . exces s bw

331 print ” Costas Loop alpha : %.2e” % s e l f . c o s t a s a l p h a

332 print ” Costas Loop beta : %.2e” % s e l f . c o s t a s b e t a

333 print ”M&M mu: %.2 f ” % s e l f . mm mu

334 print ”M&M mu gain : %.2e” % s e l f . mm gain mu

335 print ”M&M omega : %.2 f ” % s e l f . mm omega

336 print ”M&M omega gain : %.2e” % s e l f . mm gain omega

337 print ”M&M omega l i m i t : %.2 f ” % s e l f . mm omega re la t ive l imi t

338

339 def s e t u p l o g g i n g ( s e l f ) :

340 print ”Modulation l ogg ing turned on . ”

341 s e l f . connect ( s e l f . p r e s c a l e r ,

342 gr . f i l e s i n k ( gr . s i z e o f g r c o m p l e x , ” r x p r e s c a l e r . dat ” ) )

343 s e l f . connect ( s e l f . agc ,

344 gr . f i l e s i n k ( gr . s i z e o f g r c o m p l e x , ” rx agc . dat ” ) )

345 s e l f . connect ( s e l f . r r c f i l t e r ,

346 gr . f i l e s i n k ( gr . s i z e o f g r c o m p l e x , ” r x r r c f i l t e r . dat ” ) )

347 s e l f . connect ( s e l f . r e c e i v e r ,

348 gr . f i l e s i n k ( gr . s i z e o f g r c o m p l e x , ” r x r e c e i v e r . dat” ) )

349 s e l f . connect ( s e l f . d i f f d e c ,

350 gr . f i l e s i n k ( gr . s i z e o f g r c o m p l e x , ” r x d i f f d e c . dat ” ) )

351 s e l f . connect ( s e l f . s l i c e r ,

352 gr . f i l e s i n k ( gr . s i z e o f c h a r , ” r x s l i c e r . dat ” ) )

353 s e l f . connect ( s e l f . symbol mapper ,

354 gr . f i l e s i n k ( gr . s i z e o f c h a r , ” rx symbol mapper . dat ” ) )

355 s e l f . connect ( s e l f . unpack ,

356 gr . f i l e s i n k ( gr . s i z e o f c h a r , ” rx unpack . dat ” ) )

357

358 def add opt ions ( par s e r ) :

359 ”””

62

Page 72: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

360 Adds DBPSK demodulation−s p e c i f i c op t i ons

361 to the s tandard parser

362 ”””

363 par s e r . add opt ion ( ”” , ”−−excess−bw” ,

364 type=” f l o a t ” , d e f a u l t= de f exce s s bw ,

365 help = \

366 ” s e t RRC exce s s bandwith f a c t o r ” + \

367 ” [ d e f a u l t=%d e f a u l t ] (PSK) ” )

368 par s e r . add opt ion ( ”” , ”−−no−gray−code ” ,

369 dest=” gray code ” ,

370 ac t i on=” s t o r e f a l s e ” , d e f a u l t= de f g ray code ,

371 help = \

372 ” d i s a b l e gray coding on modulated b i t s (PSK) ” )

373 par s e r . add opt ion ( ”” , ”−−costas−alpha ” ,

374 type=” f l o a t ” , d e f a u l t=None ,

375 help = \

376 ” s e t Costas loop alpha value [ d e f a u l t=%d e f a u l t ] (PSK) ” )

377 par s e r . add opt ion ( ”” , ”−−gain−mu” ,

378 type=” f l o a t ” , d e f a u l t= def gain mu ,

379 help= \

380 ” s e t M&M symbol sync loop gain mu value ” + \

381 ” [ d e f a u l t=%d e f a u l t ] (GMSK/PSK) ” )

382 par s e r . add opt ion ( ”” , ”−−mu” ,

383 type=” f l o a t ” , d e f a u l t= def mu ,

384 help= \

385 ” s e t M&M symbol sync loop mu value ” + \

386 ” [ d e f a u l t=%d e f a u l t ] (GMSK/PSK) ” )

387 par s e r . add opt ion ( ”” , ”−−omega−r e l a t i v e−l i m i t ” ,

388 type=” f l o a t ” , d e f a u l t= d e f o m e g a r e l a t i v e l i m i t ,

389 help= \

63

Page 73: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

390 ”M&M clock recovery omega r e l a t i v e l i m i t ” + \

391 ” [ d e f a u l t=%d e f a u l t ] (GMSK/PSK) ” )

392 add opt ions=stat icmethod ( add opt ions )

393

394 def ex t ra c t kwarg s f r om opt i on s ( opt ions ) :

395 ”””

396 Given command l i n e opt ions ,

397 c r ea t e d i c t i ona r y s u i t a b l e f o r pas s ing to i n i t

398 ”””

399 return m o d u l a t i o n u t i l s . ex t ra c t kwarg s f r om opt i on s (

400 dbpsk demod . i n i t , ( ’ s e l f ’ , ) , opt ions )

401 ex t rac t kwarg s f r om opt i on s = \

402 stat icmethod ( ex t rac t kwarg s f r om opt i on s )

403 #

404 # Add the s e to the mod/demod r e g i s t r y

405 #

406 m o d u l a t i o n u t i l s . add type 1 mod ( ’ dbpsk ’ , dbpsk mod )

407 m o d u l a t i o n u t i l s . add type 1 demod ( ’ dbpsk ’ , dbpsk demod )

64

Page 74: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

C.2. Base Station A/B

C.2.1. Source Code for combine.py

1 #!/ usr / b in /env python

2 #

3 # Copyright 2005 ,2006 ,2007 ,2009 Free Sof tware Foundation , Inc .

4 #

5 # This f i l e i s par t o f GNU Radio

6 #

7 # GNU Radio i s f r e e so f tware ; you can r e d i s t r i b u t e i t and/or modify

8 # i t under the terms o f the GNU General Pub l i c License as pub l i s h ed by

9 # the Free Sof tware Foundation ; e i t h e r ve r s i on 3 , or ( at your opt ion )

10 # any l a t e r ve r s i on .

11 #

12 # GNU Radio i s d i s t r i b u t e d in the hope t ha t i t w i l l be u s e fu l ,

13 # but WITHOUT ANY WARRANTY; wi thout even the imp l i ed warranty o f

14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

15 # GNU General Pub l i c License f o r more d e t a i l s .

16 #

17 # You shou ld have r e c e i v ed a copy o f the GNU General Pub l i c License

18 # along wi th GNU Radio ; see the f i l e COPYING. I f not , wr i t e to

19 # the Free Sof tware Foundation , Inc . , 51 Frank l in S t ree t ,

20 # Boston , MA 02110−1301 , USA.

21 #

22

23 from gnuradio import gr , gru , m o d u l a t i o n u t i l s

24 from gnuradio import usrp

25 from gnuradio import eng notat ion

26 from gnuradio . eng opt ion import eng opt ion

27 from optparse import OptionParser

28

65

Page 75: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

29 import random , time , s t ruc t , sys , s t r i ng , os

30 import numpy

31

32 # From current d i r

33 import usrp t ransmi t path

34 import u s r p r e c e i v e p a t h

35 import bpsk

36

37 #import os

38 #pr in t os . g e t p i d ( )

39 #raw input ( ’ Attach and pre s s en ter ’ )

40

41 class my top block ( gr . top b lock ) :

42 def i n i t ( s e l f , modulator , demodulator , r x ca l l back , opt ions ) :

43 gr . top b lock . i n i t ( s e l f )

44

45 # Set up transmi t path

46 s e l f . txpath = usrp t ransmi t path . us rp t ransmi t path ( modulator ,

47 opt ions )

48 s e l f . connect ( s e l f . txpath )

49

50 # Set up r e c e i v e path

51 s e l f . rxpath = u s r p r e c e i v e p a t h . u s r p r e c e i v e p a t h ( demodulator ,

52 rx ca l l back , opt ions )

53 s e l f . connect ( s e l f . rxpath )

54

55 # /////////////////////////////////////////////////////////////////////

56 # main

57 # /////////////////////////////////////////////////////////////////////

58

66

Page 76: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

59 global n rcvd , n r i ght , data len , data len min

60

61 def main ( ) :

62 global n rcvd , n r i ght , d e s t f i l e , data len , data len min

63

64 n rcvd = 0

65 n r i g h t = 0

66 data l en = 0

67 data len min = 0

68

69 def send pkt ( payload=’ ’ , e o f=Fal se ) :

70 return tb . txpath . send pkt ( payload , eo f )

71

72 def r x c a l l b a c k ( ok , payload ) :

73 print ”ok = %r , payload = ’%s ’ ” % ( ok , payload )

74

75 def r x c a l l b a c k ( ok , rpayload ) :

76 global n rcvd , n r i ght , d e s t f i l e , data len , data len min

77 ( rpktno , ) = s t r u c t . unpack ( ’ !H ’ , rpayload [ 0 : 2 ] )

78 rdata = rpayload [ 2 : ]

79 n rcvd += 1

80 i f ok :

81 n r i g h t += 1

82

83 # Do not wr i t e f i r s t dummy packe t ( pktno #0) and pktno #1

84 i f rpktno > 1 and rpktno < 7000 :

85 data dec = numpy . f r omst r ing ( data [ ( rpktno −2)∗( p k t s i z e −2): \

86 ( rpktno −2)∗( p k t s i z e −2)+len ( rdata ) ] , numpy . u int8 )

87 data 2 dec = numpy . f r omst r ing ( rdata , numpy . u int8 )

88

67

Page 77: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

89 # XOR data & data 2 and wr i t e to f i l e ( xored )

90 xored = data dec ˆ data 2 dec

91 xored = xored . t o s t r i n g ( )

92 d e s t f i l e . wr i t e ( xored )

93 d e s t f i l e . f l u s h ( )

94

95 e l i f rpktno > 7000 and data l en <= data len min :

96 d e s t f i l e . wr i t e ( rdata )

97 d e s t f i l e . f l u s h ( )

98

99 # Get min data l e n g t h ( da ta l en min ) from re l a y ( pktno #1)

100 e l i f rpktno == 1 :

101 ( data len min , ) = s t r u c t . unpack ( ’ ! I ’ , rdata )

102 print data len min

103

104 # Print data

105 print ”ok = %5s pktno = %4d n rcvd = %4d n r i g h t = %4d” \

106 % ( ok , rpktno , n rcvd , n r i g h t )

107

108 mods = m o d u l a t i o n u t i l s . type 1 mods ( )

109 demods = m o d u l a t i o n u t i l s . type 1 demods ( )

110

111 # Create op t i ons parser :

112 par s e r = OptionParser ( o p t i o n c l a s s=eng opt ion ,

113 c o n f l i c t h a n d l e r=” r e s o l v e ” )

114 exper t g rp = par s e r . add opt ion group ( ”Expert” )

115

116 par s e r . add opt ion ( ”−m” , ”−−modulation ” ,

117 type=” cho i c e ” , c h o i c e s=mods . keys ( ) , d e f a u l t=’gmsk ’ ,

118 help = \

68

Page 78: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

119 ” S e l e c t modulation from : %s [ d e f a u l t=%%d e f a u l t ] ” \

120 % ( ’ , ’ . j o i n (mods . keys ( ) ) , ) )

121

122 par s e r . add opt ion ( ”−s ” , ”−−s i z e ” ,

123 type=” e n g f l o a t ” , d e f a u l t =1500 ,

124 help = \

125 ” s e t packet s i z e [ d e f a u l t=%d e f a u l t ] ” )

126 par s e r . add opt ion ( ”−M” , ”−−megabytes” ,

127 type=” e n g f l o a t ” , d e f a u l t =1.0 ,

128 help = \

129 ” s e t megabytes to transmit [ d e f a u l t=%d e f a u l t ] ” )

130 par s e r . add opt ion ( ”” , ”−−d i s cont inuous ” ,

131 ac t i on=” s t o r e t r u e ” , d e f a u l t=False ,

132 help = \

133 ” enable d i s c on t i n ou s t ransmi s s i on ” + \

134 ” ( bur s t s o f 5 packets ) ” )

135 par s e r . add opt ion ( ”” , ”−−from− f i l e ” , d e f a u l t=None ,

136 help=” use f i l e f o r packet contents ” )

137

138 us rp t ransmi t path . add opt ions ( parser , expe r t g rp )

139 u s r p r e c e i v e p a t h . add opt ions ( parser , expe r t g rp )

140

141 for mod in mods . va lue s ( ) :

142 mod . add opt ions ( expe r t g rp )

143

144 ( opt ions , args ) = par s e r . p a r s e a r g s ( )

145

146 i f l en ( args ) != 0 :

147 par s e r . p r i n t h e l p ( )

148 sys . e x i t (1 )

69

Page 79: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

149

150 i f opt ions . t x f r e q i s None :

151 sys . s t d e r r . wr i t e ( ”You must s p e c i f y −f FREQ or −−f r e q FREQ\n” )

152 par s e r . p r i n t h e l p ( sys . s t d e r r )

153 sys . e x i t (1 )

154

155 i f opt ions . f r o m f i l e i s not None :

156 da ta l en = os . path . g e t s i z e ( opt ions . f r o m f i l e )

157 s o u r c e f i l e = open ( opt ions . f r o m f i l e , ’ r ’ )

158

159 d e s t f i l e = open ( ” r e c e i v e d . dat” , ’wb ’ )

160

161 # Bui ld the graph

162 tb = my top block (mods [ opt ions . modulation ] ,

163 demods [ opt ions . modulation ] , r x ca l l back , opt ions )

164

165 r = gr . e n a b l e r e a l t i m e s c h e d u l i n g ( )

166 i f r != gr .RT OK:

167 print ”Warning : f a i l e d to enable r ea l t ime schedu l ing ”

168

169 # Sta r t f l ow graph

170 tb . s t a r t ( )

171 print ”Ready to r e c e i v e packets ”

172 raw input ( ’ Press Enter to transmit packets ’ )

173 print ” Transmitt ing packets \n”

174

175 # Generate and send packe t s

176 nbytes = i n t (1 e6 ∗ opt ions . megabytes )

177 n = 0

178 pktno = 0

70

Page 80: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

179 p k t s i z e = i n t ( opt ions . s i z e )

180

181 # Send dummy f i r s t packe t ( pktno #0)

182 data = ( p k t s i z e − 2) ∗ chr ( pktno & 0 x f f )

183 payload = s t r u c t . pack ( ’ !H ’ , pktno & 0 x f f f f ) + data

184 send pkt ( payload )

185 n += len ( payload )

186 sys . s t d e r r . wr i t e ( ’ . ’ ) # Outputs ” . . . . . . . . ” oncreen

187 pktno += 1

188

189 while n < nbytes :

190 i f opt ions . f r o m f i l e i s None :

191 data = ( p k t s i z e − 2) ∗ chr ( pktno & 0 x f f )

192 else :

193 data = s o u r c e f i l e . read ( p k t s i z e − 2)

194 i f data == ’ ’ :

195 break ;

196

197 payload = s t r u c t . pack ( ’ !H ’ , pktno & 0 x f f f f ) + data

198 send pkt ( payload )

199 n += len ( payload )

200 sys . s t d e r r . wr i t e ( ’ . ’ ) # Outputs ” . . . . . . . . ” oncreen

201 i f opt ions . d i s cont inuous and pktno % 5 == 4 :

202 time . s l e e p (1 )

203 pktno += 1

204

205 time . s l e e p (5 ) # Let the l a s t packe t f i n i s h sending

206 send pkt ( eo f=True )

207 print ”\nFinished t ransmi t t i ng packets ”

208 s o u r c e f i l e . c l o s e ( )

71

Page 81: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

209

210 # Begin r e c e i v i n g

211 s o u r c e f i l e = open ( opt ions . f r o m f i l e , ’ r ’ )

212 data = s o u r c e f i l e . read ( )

213

214 # Stop f l ow graph

215 raw input ( ’ Press Enter to end ’ )

216 s o u r c e f i l e . c l o s e ( )

217 d e s t f i l e . c l o s e ( )

218 tb . stop ( )

219

220 i f name == ’ ma in ’ :

221 try :

222 main ( )

223 except KeyboardInterrupt :

224 pass

72

Page 82: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

C.2.2. Source Code for transmit path.py

1 #

2 # Copyright 2005 , 2006 , 2007 Free Sof tware Foundation , Inc .

3 #

4 # This f i l e i s par t o f GNU Radio

5 #

6 # GNU Radio i s f r e e so f tware ; you can r e d i s t r i b u t e i t and/or modify

7 # i t under the terms o f the GNU General Pub l i c License as pub l i s h ed by

8 # the Free Sof tware Foundation ; e i t h e r ve r s i on 3 , or ( at your opt ion )

9 # any l a t e r ve r s i on .

10 #

11 # GNU Radio i s d i s t r i b u t e d in the hope t ha t i t w i l l be u s e fu l ,

12 # but WITHOUT ANY WARRANTY; wi thout even the imp l i ed warranty o f

13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

14 # GNU General Pub l i c License f o r more d e t a i l s .

15 #

16 # You shou ld have r e c e i v ed a copy o f the GNU General Pub l i c License

17 # along wi th GNU Radio ; see the f i l e COPYING. I f not , wr i t e to

18 # the Free Sof tware Foundation , Inc . , 51 Frank l in S t ree t ,

19 # Boston , MA 02110−1301 , USA.

20 #

21

22 from gnuradio import gr , gru , b lks2

23 from gnuradio import eng notat ion

24

25 import copy

26 import sys

27

28 # /////////////////////////////////////////////////////////////////////

29 # transmi t path

73

Page 83: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

30 # /////////////////////////////////////////////////////////////////////

31

32 class t ransmit path ( gr . h i e r b l o c k 2 ) :

33 def i n i t ( s e l f , modu lator c la s s , opt ions ) :

34 ’ ’ ’

35 See below f o r what op t i ons shou ld ho ld

36 ’ ’ ’

37 gr . h i e r b l o c k 2 . i n i t ( s e l f , ” t ransmit path ” ,

38 # Input s i gna tu r e

39 gr . i o s i g n a t u r e (0 , 0 , 0 ) ,

40 # Output s i gna tu r e

41 gr . i o s i g n a t u r e (1 , 1 , gr . s i z e o f g r c o m p l e x ) )

42

43 # make a copy so we can d e s t r u c t i v e l y modify

44 opt ions = copy . copy ( opt ions )

45

46 s e l f . v e rbose = opt ions . verbose

47 # d i g i t a l ampl i tude sen t to USRP

48 s e l f . tx ampl i tude = opt ions . tx ampl i tude

49 # des i r ed b i t ra t e

50 s e l f . b i t r a t e = opt ions . b i t r a t e

51 # des i r ed samples /baud

52 s e l f . samples per symbol = opt ions . samples per symbol

53

54 # the modu la t o r c l a s s we are us ing

55 s e l f . m o d u l a t o r c l a s s = modu la to r c l a s s

56

57 # Get mod kwargs

58 mod kwargs = \

59 s e l f . m o d u l a t o r c l a s s . ex t ra c t kwarg s f r om opt i on s ( opt ions )

74

Page 84: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

60

61 # transmi t t e r

62 modulator = s e l f . m o d u l a t o r c l a s s (∗∗mod kwargs )

63 s e l f . pa cke t t r an sm i t t e r = \

64 b lks2 . mod pkts ( modulator ,

65 a c c e s s c o d e=None ,

66 msgq l imit =4,

67 pad fo r u s rp=True )

68

69 s e l f . amp = gr . m u l t i p l y c o n s t c c (1 )

70 s e l f . s e t t x amp l i tude ( s e l f . tx ampl i tude )

71

72 # Disp lay some in format ion about the se tup

73 i f s e l f . v e rbose :

74 s e l f . p r i n t v e r b a g e ( )

75

76 # Connect components in the f lowgraph

77 s e l f . connect ( s e l f . packe t t ransmi t t e r , s e l f . amp, s e l f )

78

79 def s e t tx amp l i tude ( s e l f , ampl ) :

80 ”””

81 Se t s the t ransmi t ampl i tude sen t to the USRP in v o l t s

82 @param : ampl 0 <= ampl < 1 .

83 ”””

84 s e l f . tx ampl i tude = max( 0 . 0 , min ( ampl , 1 ) )

85 s e l f . amp . s e t k ( s e l f . tx ampl i tude )

86

87 def send pkt ( s e l f , payload=’ ’ , e o f=Fal se ) :

88 ”””

89 Ca l l s the t r an smi t t e r method to send a packe t

75

Page 85: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

90 ”””

91 return s e l f . pa cke t t r an sm i t t e r . send pkt ( payload , eo f )

92

93 def b i t r a t e ( s e l f ) :

94 return s e l f . b i t r a t e

95

96 def samples per symbol ( s e l f ) :

97 return s e l f . samples per symbol

98

99 def add opt ions ( normal , expert ) :

100 ”””

101 Adds t ransmi t t e r−s p e c i f i c op t i ons

102 to the Options Parser

103 ”””

104 i f not normal . has opt ion ( ’−−b i t r a t e ’ ) :

105 normal . add opt ion ( ”−r ” , ”−−b i t r a t e ” ,

106 type=” e n g f l o a t ” , d e f a u l t =100e3 ,

107 help=” s p e c i f y b i t r a t e [ d e f a u l t=%d e f a u l t ] . ” )

108 normal . add opt ion ( ”” , ”−−tx−amplitude ” ,

109 type=” e n g f l o a t ” , d e f a u l t =0.250 ,

110 metavar=”AMPL” ,

111 help= \

112 ” s e t t r an smi t t e r d i g i t a l amplitude : ” + \

113 ”0 <= AMPL < 1 [ d e f a u l t=%d e f a u l t ] ” )

114 normal . add opt ion ( ”−v” , ”−−verbose ” ,

115 ac t i on=” s t o r e t r u e ” , d e f a u l t=False )

116

117 expert . add opt ion ( ”−S” , ”−−samples−per−symbol” ,

118 type=” i n t ” , d e f a u l t =2,

119 help = \

76

Page 86: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

120 ” s e t samples /symbol [ d e f a u l t=%d e f a u l t ] ” )

121 expert . add opt ion ( ”” , ”−−l og ” ,

122 ac t i on=” s t o r e t r u e ” , d e f a u l t=False ,

123 help = \

124 ”Log a l l par t s o f f low graph ” + \

125 ” to f i l e (CAUTION: l o t s o f data ) ” )

126

127 # Make a s t a t i c method

128 # to c a l l b e f o r e i n s t a n t i a t i o n

129 add opt ions = stat icmethod ( add opt ions )

130

131 def p r i n t v e r b a g e ( s e l f ) :

132 ”””

133 Prin t s in format ion about the t ransmi t path

134 ”””

135 print ”Tx amplitude %s ” \

136 % ( s e l f . tx ampl i tude )

137 print ” modulation : %s ” \

138 % ( s e l f . m o d u l a t o r c l a s s . name )

139 print ” b i t r a t e : %sb/ s ” \

140 % ( eng notat ion . num to str ( s e l f . b i t r a t e ) )

141 print ” samples /symbol : %3d” \

142 % ( s e l f . samples per symbol )

77

Page 87: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

C.2.3. Source Code for usrp transmit path.py

1 #

2 # Copyright 2009 Free Sof tware Foundation , Inc .

3 #

4 # This f i l e i s par t o f GNU Radio

5 #

6 # GNU Radio i s f r e e so f tware ; you can r e d i s t r i b u t e i t and/or modify

7 # i t under the terms o f the GNU General Pub l i c License as pub l i s h ed by

8 # the Free Sof tware Foundation ; e i t h e r ve r s i on 3 , or ( at your opt ion )

9 # any l a t e r ve r s i on .

10 #

11 # GNU Radio i s d i s t r i b u t e d in the hope t ha t i t w i l l be u s e fu l ,

12 # but WITHOUT ANY WARRANTY; wi thout even the imp l i ed warranty o f

13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

14 # GNU General Pub l i c License f o r more d e t a i l s .

15 #

16 # You shou ld have r e c e i v ed a copy o f the GNU General Pub l i c License

17 # along wi th GNU Radio ; see the f i l e COPYING. I f not , wr i t e to

18 # the Free Sof tware Foundation , Inc . , 51 Frank l in S t ree t ,

19 # Boston , MA 02110−1301 , USA.

20 #

21

22 from gnuradio import gr

23 import us rp opt i on s

24 import t ransmit path

25 from p i c k b i t r a t e import p i c k t x b i t r a t e

26 from gnuradio import eng notat ion

27

28 def a d d f r e q o p t i o n ( par s e r ) :

29 ”””

78

Page 88: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

30 Hackery t ha t has the −f / −−f r e q opt ion

31 s e t both t x f r e q and r x f r e q

32 ”””

33 def f r e q c a l l b a c k ( option , op t s t r , value , pa r s e r ) :

34 # parser . va l u e s . r x f r e q = va lue

35 par s e r . va lue s . t x f r e q = value

36

37 i f not par s e r . has opt ion ( ’−−f r e q ’ ) :

38 par s e r . add opt ion ( ’−f ’ , ’−−f r e q ’ , type=” e n g f l o a t ” ,

39 ac t i on=” c a l l b a c k ” , c a l l b a c k=f r e q c a l l b a c k ,

40 help=” s e t Tx and/ or Rx frequency to FREQ [ d e f a u l t=%d e f a u l t ] ” ,

41 metavar=”FREQ” )

42

43 def add opt ions ( parser , expert ) :

44 a d d f r e q o p t i o n ( par s e r )

45 us rp opt i on s . add tx opt i ons ( par s e r )

46 transmit path . t ransmit path . add opt ions ( parser , expert )

47 expert . add opt ion ( ”” , ”−−tx−f r e q ” , type=” e n g f l o a t ” ,

48 d e f a u l t=None ,

49 help=” s e t transmit f requency to FREQ [ d e f a u l t=%d e f a u l t ] ” ,

50 metavar=”FREQ” )

51 par s e r . add opt ion ( ”−v” , ”−−verbose ” , a c t i on=” s t o r e t r u e ” ,

52 d e f a u l t=False )

53

54 class usrp t ransmi t path ( gr . h i e r b l o c k 2 ) :

55 def i n i t ( s e l f , modu lator c la s s , opt ions ) :

56 ’ ’ ’

57 See below f o r what op t i ons shou ld ho ld

58 ’ ’ ’

59 gr . h i e r b l o c k 2 . i n i t ( s e l f , ” us rp t ransmi t path ” ,

79

Page 89: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

60 # Input s i gna tu r e

61 gr . i o s i g n a t u r e (0 , 0 , 0 ) ,

62 # Output s i gna tu r e

63 gr . i o s i g n a t u r e (0 , 0 , 0 ) )

64 i f opt ions . t x f r e q i s None :

65 sys . s t d e r r . wr i t e (\

66 ”−f FREQ or −−f r e q FREQ or −−tx−f r e q FREQ must be s p e c i f i e d \n” )

67 raise SystemExit

68 tx path = transmit path . t ransmit path ( modulator c la s s , opt ions )

69 for a t t r in d i r ( tx path ) : #forward the methods

70 i f not a t t r . s t a r t s w i t h ( ’ ’ ) and not hasa t t r ( s e l f , a t t r ) :

71 s e t a t t r ( s e l f , a t t r , g e t a t t r ( tx path , a t t r ) )

72 #setup usrp

73 s e l f . m o d u l a t o r c l a s s = modu la to r c l a s s

74 s e l f . s e t u p u s r p s i n k ( opt ions )

75 #connect

76 s e l f . connect ( tx path , s e l f . u )

77

78 def s e t u p u s r p s i n k ( s e l f , opt ions ) :

79 ”””

80 Creates a USRP sink , determines the s e t t i n g s f o r b e s t b i t r a t e ,

81 and a t t a che s to the t r an smi t t e r ’ s subdev i c e .

82 ”””

83 s e l f . u = us rp opt i on s . c r e a t e u s r p s i n k ( opt ions )

84 dac ra t e = s e l f . u . dac ra t e ( )

85 i f opt ions . verbose :

86 print ’USRP Sink : ’ , s e l f . u

87 ( s e l f . b i t r a t e , s e l f . samples per symbol , s e l f . i n t e r p ) = \

88 p i c k t x b i t r a t e ( opt ions . b i t r a t e ,

89 s e l f . m o d u l a t o r c l a s s . b i t s pe r symbo l ( ) ,

80

Page 90: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

90 opt ions . samples per symbol , opt ions . interp , dac rate ,

91 s e l f . u . g e t i n t e r p r a t e s ( ) )

92

93 s e l f . u . s e t i n t e r p ( s e l f . i n t e r p )

94 # s e l f . u . s e t a u t o t r ( Fa lse ) # For f u l l dup lex

95 s e l f . u . s e t a u t o t r ( True )

96

97 i f not s e l f . u . s e t c e n t e r f r e q ( opt ions . t x f r e q ) :

98 print ” Fa i l ed to s e t Rx frequency to %s ” % \

99 ( eng notat ion . num to str ( opt ions . t x f r e q ) )

100 raise ValueError , eng notat ion . num to str ( opt ions . t x f r e q )

81

Page 91: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

C.2.4. Source Code for receive path.py

1 #!/ usr / b in /env python

2 #

3 # Copyright 2005 ,2006 ,2007 Free Sof tware Foundation , Inc .

4 #

5 # This f i l e i s par t o f GNU Radio

6 #

7 # GNU Radio i s f r e e so f tware ; you can r e d i s t r i b u t e i t and/or modify

8 # i t under the terms o f the GNU General Pub l i c License as pub l i s h ed by

9 # the Free Sof tware Foundation ; e i t h e r ve r s i on 3 , or ( at your opt ion )

10 # any l a t e r ve r s i on .

11 #

12 # GNU Radio i s d i s t r i b u t e d in the hope t ha t i t w i l l be u s e fu l ,

13 # but WITHOUT ANY WARRANTY; wi thout even the imp l i ed warranty o f

14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

15 # GNU General Pub l i c License f o r more d e t a i l s .

16 #

17 # You shou ld have r e c e i v ed a copy o f the GNU General Pub l i c License

18 # along wi th GNU Radio ; see the f i l e COPYING. I f not , wr i t e to

19 # the Free Sof tware Foundation , Inc . , 51 Frank l in S t ree t ,

20 # Boston , MA 02110−1301 , USA.

21 #

22

23 from gnuradio import gr , gru , b lks2

24 from gnuradio import eng notat ion

25 import copy

26 import sys

27

28 # /////////////////////////////////////////////////////////////////////

29 # rec e i v e path

82

Page 92: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

30 # /////////////////////////////////////////////////////////////////////

31

32 class r e c e i v e p a t h ( gr . h i e r b l o c k 2 ) :

33 def i n i t ( s e l f , demod class , r x ca l l back , opt ions ) :

34 gr . h i e r b l o c k 2 . i n i t ( s e l f , ” r e c e i v e p a t h ” ,

35 # Input s i gna tu r e

36 gr . i o s i g n a t u r e (1 , 1 , gr . s i z e o f g r c o m p l e x ) ,

37 # Output s i gna tu r e

38 gr . i o s i g n a t u r e (0 , 0 , 0 ) )

39

40 # make a copy so we can d e s t r u c t i v e l y modify

41 opt ions = copy . copy ( opt ions )

42

43 s e l f . v e rbose = opt ions . verbose

44 # des i r ed b i t ra t e

45 s e l f . b i t r a t e = opt ions . b i t r a t e

46 # des i r ed samples / symbol

47 s e l f . samples per symbol = opt ions . samples per symbol

48

49 # t h i s c a l l b a c k i s f i r e d when the r e ’ s a packe t a v a i l a b l e

50 s e l f . r x c a l l b a c k = r x c a l l b a c k

51

52 # the demodu la to r c l a s s we ’ re us ing

53 s e l f . demod c lass = demod class

54

55 # Get demod kwargs

56 demod kwargs = \

57 s e l f . demod c lass . ex t ra c t kwarg s f r om opt i on s ( opt ions )

58

59 # Design f i l t e r to g e t a c t ua l channel we want

83

Page 93: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

60 sw decim = 1

61 c h a n c o e f f s = gr . f i r d e s . low pass ( 1 . 0 , # gain

62 sw decim ∗ s e l f . samples per symbol , # sampling ra t e

63 1 . 0 , # midpoint o f t rans . band

64 0 . 5 , # width o f t rans . band

65 gr . f i r d e s .WIN HANN) # f i l t e r type

66 s e l f . c h a n n e l f i l t e r = \

67 gr . f f t f i l t e r c c c ( sw decim , c h a n c o e f f s )

68

69 # re c e i v e r

70 s e l f . p a c k e t r e c e i v e r = \

71 b lks2 . demod pkts ( s e l f . demod c lass (∗∗ demod kwargs ) ,

72 a c c e s s c o d e=None ,

73 c a l l b a c k=s e l f . r x c a l l b a c k ,

74 th r e sho ld=−1)

75

76 # Carr ier Sensing Blocks

77 alpha = 0.001

78 thresh = 30 # in dB, w i l l have to ad j u s t

79 s e l f . probe = gr . probe avg mag sqrd c ( thresh , alpha )

80

81 # Disp lay some in format ion about the se tup

82 i f s e l f . v e rbose :

83 s e l f . p r i n t v e r b a g e ( )

84

85 # connect b l o c k input to channel f i l t e r

86 s e l f . connect ( s e l f , s e l f . c h a n n e l f i l t e r )

87

88 # connect the channel input f i l t e r

89 # to the c a r r i e r power d e t e c t o r

84

Page 94: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

90 s e l f . connect ( s e l f . c h a n n e l f i l t e r , s e l f . probe )

91

92 # connect channel f i l t e r to the packe t r e c e i v e r

93 s e l f . connect ( s e l f . c h a n n e l f i l t e r ,

94 s e l f . p a c k e t r e c e i v e r )

95

96 def b i t r a t e ( s e l f ) :

97 return s e l f . b i t r a t e

98

99 def samples per symbol ( s e l f ) :

100 return s e l f . samples per symbol

101

102 def c a r r i e r s e n s e d ( s e l f ) :

103 ”””

104 Return True i f we t h ink c a r r i e r i s p re sen t .

105 ”””

106 #return s e l f . probe . l e v e l ( ) > X

107 return s e l f . probe . unmuted ( )

108

109 def c a r r i e r t h r e s h o l d ( s e l f ) :

110 ”””

111 Return curren t s e t t i n g in dB .

112 ”””

113 return s e l f . probe . th r e sho ld ( )

114

115 def s e t c a r r i e r t h r e s h o l d ( s e l f , t h r e s h o l d i n d b ) :

116 ”””

117 Set c a r r i e r t h r e s h o l d .

118

119 @param t h r e s h o l d i n d b : s e t d e t e c t i on t h r e s h o l d

85

Page 95: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

120 @type t h r e s h o l d i n d b : f l o a t (dB)

121 ”””

122 s e l f . probe . s e t t h r e s h o l d ( t h r e s h o l d i n d b )

123

124

125 def add opt ions ( normal , expert ) :

126 ”””

127 Adds rece i v e r−s p e c i f i c op t i ons

128 to the Options Parser

129 ”””

130 i f not normal . has opt ion ( ”−−b i t r a t e ” ) :

131 normal . add opt ion ( ”−r ” , ”−−b i t r a t e ” ,

132 type=” e n g f l o a t ” , d e f a u l t =100e3 ,

133 help = \

134 ” s p e c i f y b i t r a t e [ d e f a u l t=%d e f a u l t ] . ” )

135 normal . add opt ion ( ”−v” , ”−−verbose ” ,

136 ac t i on=” s t o r e t r u e ” , d e f a u l t=False )

137 expert . add opt ion ( ”−S” , ”−−samples−per−symbol” ,

138 type=” i n t ” , d e f a u l t =2,

139 help = \

140 ” s e t samples /symbol [ d e f a u l t=%d e f a u l t ] ” )

141 expert . add opt ion ( ”” , ”−−l og ” ,

142 ac t i on=” s t o r e t r u e ” , d e f a u l t=False ,

143 help = \

144 ”Log a l l par t s o f f low graph ” + \

145 ” to f i l e s (CAUTION: l o t s o f data ) ” )

146

147 # Make a s t a t i c method to c a l l b e f o r e i n s t a n t i a t i o n

148 add opt ions = stat icmethod ( add opt ions )

149

86

Page 96: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

150

151 def p r i n t v e r b a g e ( s e l f ) :

152 ”””

153 Prin t s in format ion about the r e c e i v e path

154 ”””

155 print ”\nReceive Path : ”

156 print ” modulation : %s ” \

157 % ( s e l f . demod c lass . name )

158 print ” b i t r a t e : %sb/ s ” \

159 % ( eng notat ion . num to str ( s e l f . b i t r a t e ) )

160 print ” samples /symbol : %3d” \

161 % ( s e l f . samples per symbol )

87

Page 97: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

C.2.5. Source Code for usrp receive path.py

1 #

2 # Copyright 2009 Free Sof tware Foundation , Inc .

3 #

4 # This f i l e i s par t o f GNU Radio

5 #

6 # GNU Radio i s f r e e so f tware ; you can r e d i s t r i b u t e i t and/or modify

7 # i t under the terms o f the GNU General Pub l i c License as pub l i s h ed by

8 # the Free Sof tware Foundation ; e i t h e r ve r s i on 3 , or ( at your opt ion )

9 # any l a t e r ve r s i on .

10 #

11 # GNU Radio i s d i s t r i b u t e d in the hope t ha t i t w i l l be u s e fu l ,

12 # but WITHOUT ANY WARRANTY; wi thout even the imp l i ed warranty o f

13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

14 # GNU General Pub l i c License f o r more d e t a i l s .

15 #

16 # You shou ld have r e c e i v ed a copy o f the GNU General Pub l i c License

17 # along wi th GNU Radio ; see the f i l e COPYING. I f not , wr i t e to

18 # the Free Sof tware Foundation , Inc . , 51 Frank l in S t ree t ,

19 # Boston , MA 02110−1301 , USA.

20 #

21

22 from gnuradio import gr

23 import us rp opt i on s

24 import r e c e i v e p a t h

25 from p i c k b i t r a t e import p i c k r x b i t r a t e

26 from gnuradio import eng notat ion

27

28 def a d d f r e q o p t i o n ( par s e r ) :

29 ”””

88

Page 98: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

30 Hackery t ha t has the −f / −−f r e q opt ion

31 s e t both t x f r e q and r x f r e q

32 ”””

33 def r f r e q c a l l b a c k ( option , op t s t r , value , pa r s e r ) :

34 par s e r . va lue s . r x f r e q = value

35 # parser . va l u e s . t x f r e q = va lue

36

37 i f not par s e r . has opt ion ( ’−−f r e q ’ ) :

38 par s e r . add opt ion ( ’−f ’ , ’−−f r e q ’ , type=” e n g f l o a t ” ,

39 ac t i on=” c a l l b a c k ” , c a l l b a c k=r f r e q c a l l b a c k ,

40 help = \

41 ” s e t Tx and/ or Rx frequency to FREQ [ d e f a u l t=%d e f a u l t ] ” ,

42 metavar=”FREQ” )

43

44 def add opt ions ( parser , expert ) :

45 a d d f r e q o p t i o n ( par s e r )

46 us rp opt i on s . add rx opt i ons ( par s e r )

47 r e c e i v e p a t h . r e c e i v e p a t h . add opt ions ( parser , expert )

48 expert . add opt ion ( ”” , ”−−rx−f r e q ” , type=” e n g f l o a t ” ,

49 d e f a u l t=None ,

50 help=” s e t Rx frequency to FREQ [ d e f a u l t=%d e f a u l t ] ” ,

51 metavar=”FREQ” )

52 par s e r . add opt ion ( ”−v” , ”−−verbose ” , a c t i on=” s t o r e t r u e ” ,

53 d e f a u l t=False )

54

55 class u s r p r e c e i v e p a t h ( gr . h i e r b l o c k 2 ) :

56

57 def i n i t ( s e l f , demod class , r x ca l l back , opt ions ) :

58 ’ ’ ’

59 See below f o r what op t i ons shou ld ho ld

89

Page 99: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

60 ’ ’ ’

61 gr . h i e r b l o c k 2 . i n i t ( s e l f , ” u s r p r e c e i v e p a t h ” ,

62 # Input s i gna tu r e

63 gr . i o s i g n a t u r e (0 , 0 , 0 ) ,

64 # Output s i gna tu r e

65 gr . i o s i g n a t u r e (0 , 0 , 0 ) )

66 i f opt ions . r x f r e q i s None :

67 sys . s t d e r r . wr i t e (\

68 ”−f FREQ or −−f r e q FREQ or −−rx−f r e q FREQ must be s p e c i f i e d \n” )

69 raise SystemExit

70 rx path = \

71 r e c e i v e p a t h . r e c e i v e p a t h ( demod class , r x ca l l ba ck , opt ions )

72 for a t t r in d i r ( rx path ) : #forward the methods

73 i f not a t t r . s t a r t s w i t h ( ’ ’ ) and not hasa t t r ( s e l f , a t t r ) :

74 s e t a t t r ( s e l f , a t t r , g e t a t t r ( rx path , a t t r ) )

75 #setup usrp

76 s e l f . demod c lass = demod class

77 s e l f . s e t u p u s r p s o u r c e ( opt ions )

78 #connect

79 s e l f . connect ( s e l f . u , rx path )

80

81 def s e t u p u s r p s o u r c e ( s e l f , opt ions ) :

82 s e l f . u = us rp opt i on s . c r e a t e u s r p s o u r c e ( opt ions )

83 adc ra t e = s e l f . u . adc ra t e ( )

84 i f opt ions . verbose :

85 print ’USRP Source : ’ , s e l f . u

86 ( s e l f . b i t r a t e , s e l f . samples per symbol , s e l f . decim ) = \

87 p i c k r x b i t r a t e ( opt ions . b i t r a t e ,

88 s e l f . demod c lass . b i t s pe r symbo l ( ) ,

89 opt ions . samples per symbol , opt ions . decim , adc rate ,

90

Page 100: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

90 s e l f . u . g e t d e c i m r a t e s ( ) )

91

92 s e l f . u . s e t dec im ( s e l f . decim )

93

94 i f not s e l f . u . s e t c e n t e r f r e q ( opt ions . r x f r e q ) :

95 print ” Fa i l ed to s e t Rx frequency to %s ” % \

96 ( eng notat ion . num to str ( opt ions . r x f r e q ) )

97 raise ValueError , eng notat ion . num to str ( opt ions . r x f r e q )

91

Page 101: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

C.2.6. Source Code for generic usrp.py

1 #

2 # Copyright 2009 Free Sof tware Foundation , Inc .

3 #

4 # This f i l e i s par t o f GNU Radio

5 #

6 # GNU Radio i s f r e e so f tware ; you can r e d i s t r i b u t e i t and/or modify

7 # i t under the terms o f the GNU General Pub l i c License as pub l i s h ed by

8 # the Free Sof tware Foundation ; e i t h e r ve r s i on 3 , or ( at your opt ion )

9 # any l a t e r ve r s i on .

10 #

11 # GNU Radio i s d i s t r i b u t e d in the hope t ha t i t w i l l be u s e fu l ,

12 # but WITHOUT ANY WARRANTY; wi thout even the imp l i ed warranty o f

13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

14 # GNU General Pub l i c License f o r more d e t a i l s .

15 #

16 # You shou ld have r e c e i v ed a copy o f the GNU General Pub l i c License

17 # along wi th GNU Radio ; see the f i l e COPYING. I f not , wr i t e to

18 # the Free Sof tware Foundation , Inc . , 51 Frank l in S t ree t ,

19 # Boston , MA 02110−1301 , USA.

20 #

21

22 USRP1 TYPE = ’ usrp1 ’

23 USRP2 TYPE = ’ usrp2 ’

24 DUMMYTYPE = ’dummy ’

25 #usrp2 ra t e s common fo r decim and in t e r p

26 USRP2 RATES = \

27 range (4 , 128+1 , 1) + range (130 , 256+1 , 2) + range (260 , 512+1 , 4)

28 #dummy common ra t e s

29 DUMMY XRATES = range (4 , 512 , 2)

92

Page 102: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

30 DUMMY CONVERTER RATE = 100 e6

31 #dummy f r e q r e s u l t

32 class dummy freq resu l t ( ob j e c t ) :

33 def i n i t ( s e l f , t a r g e t f r e q ) :

34 s e l f . baseband freq = t a r g e t f r e q

35 s e l f . dxc f r eq = 0

36 s e l f . r e s i d u a l f r e q = 0

37 from gnuradio import gr , usrp , usrp2

38

39 ########################################################################

40 # gener i c usrp common s t u f f

41 ########################################################################

42 class g e n e r i c u s r p b a s e ( ob j e c t ) :

43

44 def i n i t ( s e l f , which=0, subdev spec=None , i n t e r f a c e=”” ,

45 mac addr=”” ,

46 f u s b b l o c k s i z e =0, fu sb nb lo ck s =0, usrpx=None ,

47 l o o f f s e t=None , ga in=None ) :

48 s e l f . l o o f f s e t = l o o f f s e t

49 #usrp op t i ons

50 s e l f . which = which

51 s e l f . subdev spec = subdev spec

52 #usrp2 op t i ons

53 s e l f . i n t e r f a c e = i n t e r f a c e

54 s e l f . mac addr = mac addr

55 #fusb op t i ons

56 s e l f . f u s b b l o c k s i z e = f u s b b l o c k s i z e

57 s e l f . f u s b n b l o c k s = fu sb nb lo ck s

58 #pick which usrp model

59 i f usrpx == ’ 0 ’ : s e l f . s e tup us rpx (DUMMYTYPE)

93

Page 103: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

60 e l i f usrpx == ’ 1 ’ or \

61 s e l f . subdev spec : s e l f . s e tup us rpx (USRP1 TYPE)

62 e l i f usrpx == ’ 2 ’ or \

63 s e l f . mac addr : s e l f . s e tup us rpx (USRP2 TYPE)

64 else : #automatic

65 try : s e l f . s e tup us rpx (USRP2 TYPE)

66 except :

67 try : s e l f . s e tup us rpx (USRP1 TYPE)

68 except : raise Exception ,

69 ’ Fa i l ed to automat i ca l l y setup a usrp dev i ce . ’

70 #pos t usrp se tup

71 i f s e l f . l o o f f s e t i s not None :

72 s e l f . s e t l o o f f s e t ( s e l f . l o o f f s e t )

73 s e l f . s e t g a i n ( gain )

74 s e l f . s e t a u t o t r ( True )

75

76 def s e tup us rpx ( s e l f , type ) :

77 ”””

78 Ca l l the appropr ia t e se tup method .

79 @param type the usrp type cons tant

80 ”””

81 s e l f . type = type

82 i f s e l f . type == USRP1 TYPE: s e l f . s e tup us rp1 ( )

83 e l i f s e l f . type == USRP2 TYPE: s e l f . s e tup us rp2 ( )

84 e l i f s e l f . type == DUMMYTYPE: s e l f . setup dummy ( )

85

86 def s t r ( s e l f ) :

87 i f s e l f . type == USRP1 TYPE:

88 return s e l f . subdev . s ide and name ( )

89 e l i f s e l f . type == USRP2 TYPE:

94

Page 104: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

90 return ’ I n t e r f a c e : %s MAC Address : %s D−Board ID : 0x%.2x ’%(

91 s e l f . u . in t e r f ace name ( ) , s e l f . u . mac addr ( ) ,

92 s e l f . u . daughterboard id ( ) )

93 e l i f s e l f . type == DUMMYTYPE: return ’Dummy USRP Device ’

94

95 def gain ( s e l f ) : return s e l f . ga in

96

97 def s e t g a i n ( s e l f , ga in=None ) :

98 # automatic gain c a l c u l a t i o n

99 r = s e l f . ga in range ( )

100 # se t gain to midpoint

101 i f gain i s None : ga in = ( r [ 0 ] + r [ 1 ] ) / 2

102 #se t gain f o r usrp

103 s e l f . ga in = gain

104 i f s e l f . type == USRP1 TYPE:

105 return s e l f . subdev . s e t g a i n ( gain )

106 e l i f s e l f . type == USRP2 TYPE:

107 return s e l f . u . s e t g a i n ( gain )

108 e l i f s e l f . type == DUMMYTYPE:

109 return True

110

111 def ga in range ( s e l f ) :

112 i f s e l f . type == USRP1 TYPE:

113 return s e l f . subdev . ga in range ( )

114 e l i f s e l f . type == USRP2 TYPE:

115 return s e l f . u . ga in range ( )

116 e l i f s e l f . type == DUMMYTYPE:

117 return (0 , 0 , 0)

118

119 def s e t c e n t e r f r e q ( s e l f , t a r g e t f r e q ) :

95

Page 105: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

120 i f s e l f . type == USRP1 TYPE:

121 return s e l f . u . tune ( s e l f . dxc , s e l f . subdev , t a r g e t f r e q )

122 e l i f s e l f . type == USRP2 TYPE:

123 return s e l f . u . s e t c e n t e r f r e q ( t a r g e t f r e q )

124 e l i f s e l f . type == DUMMYTYPE:

125 return dummy freq resu l t ( t a r g e t f r e q )

126

127 def f r e q r a n g e ( s e l f ) :

128 i f s e l f . type == USRP1 TYPE:

129 return s e l f . subdev . f r e q r a n g e ( )

130 e l i f s e l f . type == USRP2 TYPE:

131 return s e l f . u . f r e q r a n g e ( )

132 e l i f s e l f . type == DUMMYTYPE:

133 return (−10e9 , 10e9 , 100 e3 )

134

135 def s e t l o o f f s e t ( s e l f , l o o f f s e t ) :

136 i f s e l f . type == USRP1 TYPE:

137 return s e l f . subdev . s e t l o o f f s e t ( l o o f f s e t )

138 e l i f s e l f . type == USRP2 TYPE:

139 return s e l f . u . s e t l o o f f s e t ( l o o f f s e t )

140 e l i f s e l f . type == DUMMYTYPE:

141 return True

142

143 def s e t a u t o t r ( s e l f , enable ) :

144 i f s e l f . type == USRP1 TYPE:

145 return s e l f . subdev . s e t a u t o t r ( enable )

146

147 def d e l ( s e l f ) :

148 try : # Avoid weak r e f e r ence error

149 del s e l f . u

96

Page 106: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

150 del s e l f . subdev

151 except : pass

152

153 ########################################################################

154 # gener i c usrp source

155 ########################################################################

156 class g e n e r i c u s r p s o u r c e c ( g e n e r i c u s r p b a s e , gr . h i e r b l o c k 2 ) :

157 ”””

158 Create a gener i c usrp source t ha t r ep r e s en t s usrp and usrp2 .

159 Take usrp and usrp2 cons t ruc t o r arguments

160 and t r y to f i g u r e out usrp or usrp2 .

161 Provide gener i c acces s methods so the API l oo k s the same fo r both .

162 ”””

163

164 def i n i t ( s e l f , ∗∗kwargs ) :

165 gr . h i e r b l o c k 2 . i n i t ( s e l f , ” g e n e r i c u s r p s o u r c e ” ,

166 # Input s i gna tu r e

167 gr . i o s i g n a t u r e (0 , 0 , 0 ) ,

168 # Output s i gna tu r e

169 gr . i o s i g n a t u r e (1 , 1 , gr . s i z e o f g r c o m p l e x ) )

170 g e n e r i c u s r p b a s e . i n i t ( s e l f , ∗∗kwargs )

171 s e l f . connect ( s e l f . u , s e l f )

172

173 ####################################################################

174 # gener i c acces s methods

175 ####################################################################

176 def s e t dec im ( s e l f , decim ) :

177 i f decim not in s e l f . g e t d e c i m r a t e s ( ) :

178 return False

179 i f s e l f . type == USRP1 TYPE:

97

Page 107: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

180 return s e l f . u . s e t d e c i m r a t e ( decim )

181 e l i f s e l f . type == USRP2 TYPE:

182 return s e l f . u . s e t dec im ( decim )

183 e l i f s e l f . type == DUMMYTYPE:

184 return True

185

186 def g e t d e c i m r a t e s ( s e l f ) :

187 # de f a u l t f irmware w/ hb f i l t e r s

188 i f s e l f . type == USRP1 TYPE:

189 return range (8 , 256+1 , 2)

190 i f s e l f . type == USRP2 TYPE:

191 return USRP2 RATES

192 e l i f s e l f . type == DUMMYTYPE:

193 return DUMMY XRATES

194

195 def adc ra t e ( s e l f ) :

196 i f s e l f . type == USRP1 TYPE:

197 return s e l f . u . adc ra t e ( )

198 i f s e l f . type == USRP2 TYPE:

199 return s e l f . u . adc ra t e ( )

200 e l i f s e l f . type == DUMMYTYPE:

201 return DUMMY CONVERTER RATE

202

203 ####################################################################

204 # setup usrp methods

205 ####################################################################

206 def s e tup us rp1 ( s e l f ) :

207 s e l f . u = usrp . s o u r c e c ( s e l f . which ,

208 f u s b b l o c k s i z e=s e l f . f u s b b l o c k s i z e ,

209 fu sb nb lo ck s=s e l f . f u s b n b l o c k s )

98

Page 108: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

210 # determine the daughterboard subdev i c e we ’ re us ing

211 i f s e l f . subdev spec i s None :

212 s e l f . subdev spec = \

213 usrp . p i c k r x s u b d e v i c e ( s e l f . u )

214 s e l f . subdev = \

215 usrp . s e l e c t ed su bdev ( s e l f . u , s e l f . subdev spec )

216 s e l f . u . set mux ( usrp . determine rx mux value ( s e l f . u ,

217 s e l f . subdev spec ) )

218 s e l f . dxc = 0

219 # Set RX antenna to RX2

220 s e l f . subdev . s e l e c t r x a n t e n n a ( ’RX2 ’ )

221 # Added Print

222 print ”Using RX board %s ” % ( s e l f . subdev . s ide and name ( ) )

223 def s e tup us rp2 ( s e l f ) :

224 s e l f . u = \

225 usrp2 . s o u r c e 3 2 f c ( s e l f . i n t e r f a c e , s e l f . mac addr )

226

227 def setup dummy ( s e l f ) : s e l f . u = gr . n u l l s o u r c e ( gr . s i z e o f g r c o m p l e x )

228

229 ########################################################################

230 # gener i c usrp s ink

231 ########################################################################

232 class g e n e r i c u s r p s i n k c ( g e n e r i c u s r p b a s e , gr . h i e r b l o c k 2 ) :

233 ”””

234 Create a gener i c usrp s ink t ha t r ep r e s en t s usrp and usrp2 .

235 Take usrp and usrp2 cons t ruc t o r arguments

236 and t r y to f i g u r e out usrp or usrp2 .

237 Provide gener i c acces s methods so the API l oo k s the same fo r both .

238 ”””

239

99

Page 109: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

240 def i n i t ( s e l f , ∗∗kwargs ) :

241 gr . h i e r b l o c k 2 . i n i t ( s e l f , ” g e n e r i c u s r p s i n k ” ,

242 # Input s i gna tu r e

243 gr . i o s i g n a t u r e (1 , 1 , gr . s i z e o f g r c o m p l e x ) ,

244 # Output s i gna tu r e

245 gr . i o s i g n a t u r e (0 , 0 , 0 ) )

246 g e n e r i c u s r p b a s e . i n i t ( s e l f , ∗∗kwargs )

247 # sca l e 0 .0 to 1.0 input f o r usrp1

248 i f s e l f . type == USRP1 TYPE:

249 s e l f . connect ( s e l f , gr . m u l t i p l y c o n s t c c ((2∗∗15)−1) , s e l f . u )

250 else : s e l f . connect ( s e l f , s e l f . u )

251

252 ####################################################################

253 # gener i c acces s methods

254 ####################################################################

255 def s e t i n t e r p ( s e l f , i n t e r p ) :

256 i f i n t e r p not in s e l f . g e t i n t e r p r a t e s ( ) :

257 return False

258 i f s e l f . type == USRP1 TYPE:

259 return s e l f . u . s e t i n t e r p r a t e ( i n t e r p )

260 e l i f s e l f . type == USRP2 TYPE:

261 return s e l f . u . s e t i n t e r p ( i n t e r p )

262 e l i f s e l f . type == DUMMYTYPE:

263 return True

264

265 def g e t i n t e r p r a t e s ( s e l f ) :

266 i f s e l f . type == USRP1 TYPE:

267 return range (16 , 512+1 , 4)

268 i f s e l f . type == USRP2 TYPE:

269 return USRP2 RATES

100

Page 110: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

270 e l i f s e l f . type == DUMMYTYPE:

271 return DUMMY XRATES

272

273 def dac ra t e ( s e l f ) :

274 i f s e l f . type == USRP1 TYPE:

275 return s e l f . u . dac ra t e ( )

276 i f s e l f . type == USRP2 TYPE:

277 return s e l f . u . dac ra t e ( )

278 e l i f s e l f . type == DUMMYTYPE:

279 return DUMMY CONVERTER RATE

280

281 ####################################################################

282 # setup usrp methods

283 ####################################################################

284 def s e tup us rp1 ( s e l f ) :

285 s e l f . u = usrp . s i n k c ( s e l f . which ,

286 f u s b b l o c k s i z e=s e l f . f u s b b l o c k s i z e ,

287 fu sb nb lo ck s=s e l f . f u s b n b l o c k s )

288 # determine the daughterboard subdev i c e we ’ re us ing

289 i f s e l f . subdev spec i s None :

290 s e l f . subdev spec = usrp . p i c k t x s u b d e v i c e ( s e l f . u )

291 s e l f . subdev = \

292 usrp . s e l e c t ed su bdev ( s e l f . u , s e l f . subdev spec )

293 s e l f . u . set mux ( usrp . determine tx mux value ( s e l f . u ,

294 s e l f . subdev spec ) )

295 s e l f . dxc = s e l f . subdev . which ( )

296 # s e l f . subdev . s e t e n a b l e (True ) # TX always enab led

297 # Added p r i n t

298 print ”Using TX board %s ” % ( s e l f . subdev . s ide and name ( ) )

299 def s e tup us rp2 ( s e l f ) :

101

Page 111: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

300 s e l f . u = usrp2 . s i n k 3 2 f c ( s e l f . i n t e r f a c e ,

301 s e l f . mac addr )

302

303 def setup dummy ( s e l f ) :

304 s e l f . u = gr . n u l l s i n k ( gr . s i z e o f g r c o m p l e x )

102

Page 112: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

C.3. Relay Station

C.3.1. Source Code for combine.py

1 #!/ usr / b in /env python

2 #

3 # Copyright 2005 , 2006 , 2007 Free Sof tware Foundation , Inc .

4 #

5 # This f i l e i s par t o f GNU Radio

6 #

7 # GNU Radio i s f r e e so f tware ; you can r e d i s t r i b u t e i t and/or modify

8 # i t under the terms o f the GNU General Pub l i c License as pub l i s h ed by

9 # the Free Sof tware Foundation ; e i t h e r ve r s i on 3 , or ( at your opt ion )

10 # any l a t e r ve r s i on .

11 #

12 # GNU Radio i s d i s t r i b u t e d in the hope t ha t i t w i l l be u s e fu l ,

13 # but WITHOUT ANY WARRANTY; wi thout even the imp l i ed warranty o f

14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

15 # GNU General Pub l i c License f o r more d e t a i l s .

16 #

17 # You shou ld have r e c e i v ed a copy o f the GNU General Pub l i c License

18 # along wi th GNU Radio ; see the f i l e COPYING. I f not , wr i t e to

19 # the Free Sof tware Foundation , Inc . , 51 Frank l in S t ree t ,

20 # Boston , MA 02110−1301 , USA.

21 #

22

23 from gnuradio import gr , gru , m o d u l a t i o n u t i l s

24 from gnuradio import usrp

25 from gnuradio import eng notat ion

26 from gnuradio . eng opt ion import eng opt ion

27 from optparse import OptionParser

28

103

Page 113: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

29 import random , time , s t ruc t , sys

30 import numpy

31

32 # From current d i r

33 from combine path import combine path

34 import f u s b o p t i o n s

35 import bpsk

36

37 #import os

38 #pr in t os . g e t p i d ( )

39 #raw input ( ’ Attach and pre s s en ter ’ )

40

41 class my top block ( gr . top b lock ) :

42 def i n i t ( s e l f , modulator , demodulator ,

43 rx ca l l back , r x c a l l b a c k 2 , opt ions ) :

44 gr . top b lock . i n i t ( s e l f )

45 s e l f . t rxpath = combine path ( modulator , demodulator ,

46 rx ca l l back , r x c a l l b a c k 2 , opt ions )

47 s e l f . connect ( s e l f . t rxpath )

48

49 # /////////////////////////////////////////////////////////////////////

50 # main

51 # /////////////////////////////////////////////////////////////////////

52

53 global n rcvd , n r i ght , n rcvd 2 , n r i gh t 2 , data len , d a t a 2 l e n

54

55 def main ( ) :

56 global n rcvd , n r i ght , d e s t f i l e , n rcvd 2 , n r i gh t 2 ,

57 d e s t f i l e 2 , data len , d a t a 2 l e n

58

104

Page 114: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

59 n rcvd = 0

60 n r i g h t = 0

61 n rcvd 2 = 0

62 n r i g h t 2 = 0

63 data l en = 0

64 d a t a 2 l e n = 0

65

66 def send pkt ( payload=’ ’ , e o f=Fal se ) :

67 return tb . trxpath . send pkt ( payload , eo f )

68

69 def r x c a l l b a c k ( ok , payload ) :

70 print ”ok = %r , payload = ’%s ’ ” % ( ok , payload )

71

72 def r x c a l l b a c k ( ok , payload ) :

73 global n rcvd , n r i ght , d e s t f i l e

74 ( pktno , ) = s t r u c t . unpack ( ’ !H ’ , payload [ 0 : 2 ] )

75 data = payload [ 2 : ]

76 n rcvd += 1

77

78 i f ok :

79 n r i g h t += 1

80

81 # Do not wr i t e f i r s t dummy packe t ( pktno #0)

82 i f pktno > 0 :

83 d e s t f i l e . wr i t e ( data )

84 d e s t f i l e . f l u s h ( )

85

86 # Print data

87 print ”ok = %5s pktno = %4d n rcvd = %4d n r i g h t = %4d” % (

88 ok , pktno , n rcvd , n r i g h t )

105

Page 115: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

89

90 def r x c a l l b a c k 2 ( ok , payload ) :

91 global n rcvd 2 , n r i gh t 2 , d e s t f i l e 2

92 ( pktno , ) = s t r u c t . unpack ( ’ !H ’ , payload [ 0 : 2 ] )

93 data = payload [ 2 : ]

94 n rcvd 2 += 1

95

96 i f ok :

97 n r i g h t 2 += 1

98

99 # Do not wr i t e f i r s t dummy packe t ( pktno #0)

100 i f pktno > 0 :

101 d e s t f i l e 2 . wr i t e ( data )

102 d e s t f i l e 2 . f l u s h ( )

103

104 # Print data

105 print ”ok = %5s pktno = %4d n rcvd = %4d n r i g h t = %4d” % (

106 ok , pktno , n rcvd 2 , n r i g h t 2 )

107

108 mods = m o d u l a t i o n u t i l s . type 1 mods ( )

109 demods = m o d u l a t i o n u t i l s . type 1 demods ( )

110

111 par s e r = OptionParser ( o p t i o n c l a s s=eng opt ion ,

112 c o n f l i c t h a n d l e r=” r e s o l v e ” )

113 exper t g rp = par s e r . add opt ion group ( ”Expert” )

114

115 # Create op t i ons parser

116 par s e r . add opt ion ( ”−m” , ”−−modulation ” , type=” cho i c e ” ,

117 c h o i c e s=mods . keys ( ) , d e f a u l t=’gmsk ’ ,

118 help=” S e l e c t modulation from : %s [ d e f a u l t=%%d e f a u l t ] ”

106

Page 116: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

119 % ( ’ , ’ . j o i n (mods . keys ( ) ) , ) )

120 par s e r . add opt ion ( ”−s ” , ”−−s i z e ” , type=” e n g f l o a t ” ,

121 d e f a u l t =1500 ,

122 help=” s e t packet s i z e [ d e f a u l t=%d e f a u l t ] ” )

123 par s e r . add opt ion ( ”−M” , ”−−megabytes” , type=” e n g f l o a t ” ,

124 d e f a u l t =1.0 ,

125 help=” s e t megabytes to transmit [ d e f a u l t=%d e f a u l t ] ” )

126 par s e r . add opt ion ( ”” , ”−−d i s cont inuous ” , a c t i on=” s t o r e t r u e ” ,

127 d e f a u l t=False ,

128 help=” enable d i s c on t i n ou s t ransmi s s i on ( bur s t s o f 5 packets ) ” )

129 par s e r . add opt ion ( ”−w” , ”−−which” , type=” i n t ” ,

130 d e f a u l t =0,

131 help=” s e l e c t USRP board [ d e f a u l t=%d e f a u l t ] ” )

132

133 combine path . add opt ions ( parser , expe r t g rp )

134

135 for mod in mods . va lue s ( ) :

136 mod . add opt ions ( expe r t g rp )

137

138 f u s b o p t i o n s . add opt ions ( expe r t g rp )

139 ( opt ions , args ) = par s e r . p a r s e a r g s ( )

140

141 i f l en ( args ) != 0 :

142 par s e r . p r i n t h e l p ( sys . s t d e r r )

143 sys . e x i t (1 )

144

145 i f opt ions . t x f r e q i s None :

146 sys . s t d e r r . wr i t e ( ”You must s p e c i f y −f FREQ or −−f r e q FREQ\n” )

147 par s e r . p r i n t h e l p ( sys . s t d e r r )

148 sys . e x i t (1 )

107

Page 117: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

149

150 d e s t f i l e = open ( ’ r p i c . jpg ’ , ’wb ’ )

151 d e s t f i l e 2 = open ( ’ r p i c 2 . jpg ’ , ’wb ’ )

152

153 # Bui ld the graph

154 tb = my top block (mods [ opt ions . modulation ] ,

155 demods [ opt ions . modulation ] , r x ca l l back ,

156 r x c a l l b a c k 2 , opt ions )

157

158 r = gr . e n a b l e r e a l t i m e s c h e d u l i n g ( )

159 i f r != gr .RT OK:

160 print ”Warning : f a i l e d to enable r ea l t ime schedu l ing ”

161

162 # Sta r t f l ow graph

163 tb . s t a r t ( )

164 print ”Ready to r e c e i v e packets ”

165 raw input ( ’ Press Enter to s t a r t t ransmi t t i ng packets ’ )

166 d e s t f i l e . c l o s e ( )

167 d e s t f i l e 2 . c l o s e ( )

168

169

170 print ” Transmitt ing packets \n”

171

172 s o u r c e f i l e = open ( ’ r p i c . jpg ’ , ’ r ’ )

173 data = s o u r c e f i l e . read ( )

174 s o u r c e f i l e . c l o s e ( )

175 da ta l en = len ( data )

176

177 s o u r c e f i l e 2 = open ( ’ r p i c 2 . jpg ’ , ’ r ’ )

178 data 2 = s o u r c e f i l e 2 . read ( )

108

Page 118: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

179 s o u r c e f i l e 2 . c l o s e ( )

180 d a t a 2 l e n = len ( data 2 )

181

182 des t xored = open ( ’ xored . jpg ’ , ’wb ’ )

183 de s t no t xo r ed = open ( ’ not xored . jpg ’ , ’wb ’ )

184

185 # Convert hex s t r i n g ( data & data 2 ) to decimal

186 data dec = numpy . f r omst r ing ( data , numpy . u int8 )

187 data 2 dec = numpy . f r omst r ing ( data 2 , numpy . u int8 )

188

189 # XOR data & data 2 and wr i t e to f i l e ( xored )

190 i f data l en < d a t a 2 l e n :

191 xored = data dec ˆ data 2 dec [ 0 : da ta l en ]

192 xored = xored . t o s t r i n g ( )

193

194 des t xored . wr i t e ( xored )

195 des t xored . c l o s e ( )

196

197 de s t no t xo r ed . wr i t e ( data 2 [ da ta l en : ] )

198 de s t no t xo r ed . c l o s e ( )

199

200 e l i f data l en > d a t a 2 l e n :

201 xored = data dec [ 0 : d a t a 2 l e n ] ˆ data 2 dec

202 xored = xored . t o s t r i n g ( )

203

204 des t xored . wr i t e ( xored )

205 des t xored . c l o s e ( )

206

207 de s t no t xo r ed . wr i t e ( data [ d a t a 2 l e n : ] )

208 de s t no t xo r ed . c l o s e ( )

109

Page 119: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

209

210 else :

211 xored = data dec ˆ data 2 dec

212 xored = xored . t o s t r i n g ( )

213 des t xored . wr i t e ( xored )

214 des t xored . c l o s e ( )

215

216 # Generate and send packe t s

217 source xored = open ( ’ xored . jpg ’ , ’ r ’ )

218 sourc e no t xo r ed = open ( ’ not xored . jpg ’ , ’ r ’ )

219 nbytes = i n t (1 e6 ∗ opt ions . megabytes )

220 n = 0

221 pktno = 0

222 p k t s i z e = i n t ( opt ions . s i z e )

223

224 # Send dummy f i r s t packe t ( pktno #0)

225 data = ( p k t s i z e − 2) ∗ chr ( pktno & 0 x f f )

226 payload = s t r u c t . pack ( ’ !H ’ , pktno & 0 x f f f f ) + data

227 send pkt ( payload )

228 n += len ( payload )

229 sys . s t d e r r . wr i t e ( ’ . ’ ) # Outputs ” . . . . . . . . ” on the screen

230 pktno += 1

231

232 # Send second packe t ( pktno #1) wi th sma l l e s t data l e n g t h va lue

233 # ( da t a l en or da t a 2 l e n )

234 data = s t r u c t . pack ( ’ ! I ’ , min ( data len , d a t a 2 l e n ) & 0 x f f f f f f )

235 payload = s t r u c t . pack ( ’ !H ’ , pktno & 0 x f f f f ) + data

236 send pkt ( payload )

237 n += len ( payload )

238 sys . s t d e r r . wr i t e ( ’ . ’ ) # Outputs ” . . . . . . . . ” on the screen

110

Page 120: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

239 pktno += 1

240

241 while n < nbytes :

242 data = source xored . read ( p k t s i z e − 2)

243 i f data == ’ ’ :

244 break ;

245

246 payload = s t r u c t . pack ( ’ !H ’ , pktno & 0 x f f f f ) + data

247 send pkt ( payload )

248 n += len ( payload )

249 sys . s t d e r r . wr i t e ( ’ . ’ ) # Outputs ” . . . . . . . . ” on the screen

250 i f opt ions . d i s cont inuous and pktno % 5 == 4 :

251 time . s l e e p (1 )

252 pktno += 1

253

254 pktno = 7001

255

256 while n < nbytes :

257 data = sourc e no t xo r ed . read ( p k t s i z e − 2)

258 i f data == ’ ’ :

259 break ;

260

261 payload = s t r u c t . pack ( ’ !H ’ , pktno & 0 x f f f f ) + data

262 send pkt ( payload )

263 n += len ( payload )

264 sys . s t d e r r . wr i t e ( ’ . ’ ) # Outputs ” . . . . . . . . ” on the screen

265 i f opt ions . d i s cont inuous and pktno % 5 == 4 :

266 time . s l e e p (1 )

267 pktno += 1

268

111

Page 121: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

269 print ”\nFinished t ransmi t t i ng packets ”

270 time . s l e e p (5 ) # Let the l a s t packe t f i n i s h sending

271 send pkt ( eo f=True )

272

273 # Stop rb f l ow graph

274 raw input ( ’ Press Enter to end ’ )

275 source xored . c l o s e ( )

276 sourc e no t xo r ed . c l o s e ( )

277 tb . stop ( )

278

279 i f name == ’ ma in ’ :

280 try :

281 main ( )

282 except KeyboardInterrupt :

283 pass

112

Page 122: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

C.3.2. Source Code for combine path.py

1 #

2 # Copyright 2005 ,2006 ,2007 Free Sof tware Foundation , Inc .

3 #

4 # This f i l e i s par t o f GNU Radio

5 #

6 # GNU Radio i s f r e e so f tware ; you can r e d i s t r i b u t e i t and/or modify

7 # i t under the terms o f the GNU General Pub l i c License as pub l i s h ed by

8 # the Free Sof tware Foundation ; e i t h e r ve r s i on 3 , or ( at your opt ion )

9 # any l a t e r ve r s i on .

10 #

11 # GNU Radio i s d i s t r i b u t e d in the hope t ha t i t w i l l be u s e fu l ,

12 # but WITHOUT ANY WARRANTY; wi thout even the imp l i ed warranty o f

13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

14 # GNU General Pub l i c License f o r more d e t a i l s .

15 #

16 # You shou ld have r e c e i v ed a copy o f the GNU General Pub l i c License

17 # along wi th GNU Radio ; see the f i l e COPYING. I f not , wr i t e to

18 # the Free Sof tware Foundation , Inc . , 51 Frank l in S t ree t ,

19 # Boston , MA 02110−1301 , USA.

20 #

21

22 from gnuradio import gr , gru , b lks2

23 from gnuradio import usrp

24 from gnuradio import eng notat ion

25

26 import copy

27 import sys

28

29 # from current d i r

113

Page 123: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

30 from p i c k b i t r a t e import p i c k t x b i t r a t e

31 from p i c k b i t r a t e import p i c k r x b i t r a t e

32 from grc gnurad io import usrp as g r c u s rp

33

34 # /////////////////////////////////////////////////////////////

35 # combine path

36 # /////////////////////////////////////////////////////////////

37

38 class combine path ( gr . h i e r b l o c k 2 ) :

39 def i n i t ( s e l f , modu lator c la s s , demod class ,

40 rx ca l l back , r x c a l l b a c k 2 , opt ions ) :

41 ’ ’ ’

42 See below f o r what op t i ons shou ld ho ld

43 ’ ’ ’

44 gr . h i e r b l o c k 2 . i n i t ( s e l f , ” combine path ” ,

45 # Input s i gna tu r e

46 gr . i o s i g n a t u r e (0 , 0 , 0 ) ,

47 # Output s i gna tu r e

48 gr . i o s i g n a t u r e (0 , 0 , 0 ) )

49

50 # make a copy so we can d e s t r u c t i v e l y modify

51 opt ions = copy . copy ( opt ions )

52

53 s e l f . v e rbose = opt ions . verbose

54 # re c e i v e r ’ s cen ter f requency

55 s e l f . r x f r e q = opt ions . r x f r e q

56 # re c e i v e r ’ s gain

57 s e l f . r x g a i n = opt ions . r x ga in

58 # tranmi t t e r ’ s cen ter f requency

59 s e l f . t x f r e q = opt ions . t x f r e q

114

Page 124: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

60 # d i g i t a l ampl i tude sen t to USRP

61 s e l f . tx ampl i tude = opt ions . tx ampl i tude

62 # daughterboard to use

63 s e l f . t x subdev spec = opt ions . tx subdev spec

64 # daughterboard to use

65 s e l f . r x subdev spec = opt ions . rx subdev spec

66 # des i r ed b i t ra t e

67 s e l f . b i t r a t e = opt ions . b i t r a t e

68 # in t e r p o l a t i n g ra t e f o r the USRP ( pre l im )

69 s e l f . i n t e r p = opt ions . i n t e r p

70 # Decimating ra t e f o r the USRP ( pre l im )

71 s e l f . decim = opt ions . decim

72 # des i r ed samples /baud

73 s e l f . samples per symbol = opt ions . samples per symbol

74 # usb in f o f o r USRP

75 s e l f . f u s b b l o c k s i z e = opt ions . f u s b b l o c k s i z e

76 # usb in f o f o r USRP

77 s e l f . f u s b n b l o c k s = opt ions . f u sb nb lo ck s

78 # increment s t a r t o f whi tener XOR data

79 s e l f . u s e w h i t e n e r o f f s e t = opt ions . u s e w h i t e n e r o f f s e t

80 s e l f . which = opt ions . which

81 # t h i s c a l l b a c k i s f i r e d when the r e ’ s a packe t a v a i l a b l e

82 s e l f . r x c a l l b a c k = r x c a l l b a c k

83 # t h i s c a l l b a c k i s f i r e d when the r e ’ s a packe t a v a i l a b l e

84 s e l f . r x c a l l b a c k 2 = r x c a l l b a c k 2

85 # the modu la t o r c l a s s we are us ing

86 s e l f . m o d u l a t o r c l a s s = modu la to r c l a s s

87 # the demodu la to r c l a s s we ’ re us ing

88 s e l f . demod c lass = demod class

89

115

Page 125: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

90 i f s e l f . t x f r e q i s None :

91 sys . s t d e r r . wr i t e (\

92 ”−f FREQ or −−f r e q FREQ or −−tx−f r e q FREQ ” + \

93 ”must be s p e c i f i e d \n” )

94 raise SystemExit

95

96 i f s e l f . r x f r e q i s None :

97 sys . s t d e r r . wr i t e (\

98 ”−f FREQ or −−f r e q FREQ or −−rx−f r e q FREQ ” + \

99 ”must be s p e c i f i e d \n” )

100 raise SystemExit

101

102 # Set up USRP s ink ; a l s o ad j u s t s in terp ,

103 # samples per symbo l , and b i t r a t e

104 s e l f . s e t u p u s r p s i n k ( )

105 s e l f . s e t u p u s r p s o u r c e ( )

106

107 # copy the f i n a l answers back in t o op t i ons

108 # for use by modulator

109 opt ions . samples per symbol = s e l f . samples per symbol

110 opt ions . b i t r a t e = s e l f . b i t r a t e

111 opt ions . i n t e r p = s e l f . i n t e r p

112 opt ions . decim = s e l f . decim

113

114 # Get mod kwargs

115 mod kwargs = \

116 s e l f . m o d u l a t o r c l a s s . ex t ra c t kwarg s f r om opt i on s ( opt ions )

117 demod kwargs = \

118 s e l f . demod c lass . ex t ra c t kwarg s f r om opt i on s ( opt ions )

119

116

Page 126: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

120 ####### Transmitter

121 s e l f . pa cke t t r an sm i t t e r = \

122 b lks2 . mod pkts ( s e l f . m o d u l a t o r c l a s s (∗∗mod kwargs ) ,

123 a c c e s s c o d e=None ,

124 msgq l imit =4,

125 pad fo r u s rp=True ,

126 u s e w h i t e n e r o f f s e t=opt ions . u s e w h i t e n e r o f f s e t )

127

128 s e l f . amp = gr . m u l t i p l y c o n s t c c (1 )

129 s e l f . amp 2 = gr . m u l t i p l y c o n s t c c (1 )

130 s e l f . s e t t x amp l i tude ( s e l f . tx ampl i tude )

131

132 # Disp lay some in format ion about the se tup

133 i f s e l f . v e rbose :

134 s e l f . p r i n t v e r b a g e ( )

135

136 # Create and se tup transmi t path f l ow graph

137 s e l f . connect ( s e l f . packe t t ransmi t t e r , s e l f . amp,

138 ( s e l f . usink , 0 ) )

139 s e l f . connect ( s e l f . packe t t ransmi t t e r , s e l f . amp 2 ,

140 ( s e l f . usink , 1 ) )

141

142 ####### Receiver

143 # Design f i l t e r to g e t a c t ua l channel we want

144 sw decim = 1

145 c h a n c o e f f s = gr . f i r d e s . low pass ( 1 . 0 , # gain

146 sw decim ∗ s e l f . samples per symbol , # sampling ra t e

147 1 . 0 , # midpoint o f t rans . band

148 0 . 5 , # width o f t rans . band

149 gr . f i r d e s .WIN HANN) # f i l t e r type

117

Page 127: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

150

151 # Decimating channel f i l t e r

152 s e l f . c h a n f i l t = \

153 gr . f f t f i l t e r c c c ( sw decim , c h a n c o e f f s )

154 s e l f . c h a n f i l t 2 = \

155 gr . f f t f i l t e r c c c ( sw decim , c h a n c o e f f s )

156

157 # Packet r e c e i v e r

158 s e l f . p a c k e t r e c e i v e r = \

159 b lks2 . demod pkts ( s e l f . demod c lass (∗∗ demod kwargs ) ,

160 a c c e s s c o d e=None ,

161 c a l l b a c k=s e l f . r x c a l l b a c k ,

162 th r e sho ld=−1)

163

164 s e l f . p a c k e t r e c e i v e r 2 = \

165 b lks2 . demod pkts ( s e l f . demod c lass (∗∗ demod kwargs ) ,

166 a c c e s s c o d e=None ,

167 c a l l b a c k=s e l f . r x c a l l b a c k 2 ,

168 th r e sho ld=−1)

169

170 # Carr ier Sensing Blocks

171 alpha = 0.001

172 thresh = 30 # in dB, w i l l have to ad j u s t

173

174 i f opt ions . l og rx power == True :

175 s e l f . probe = gr . probe avg mag sqrd c f ( thresh , alpha )

176 s e l f . power s ink = gr . f i l e s i n k ( gr . s i z e o f f l o a t ,

177 ” rxpower . dat” )

178 s e l f . connect ( s e l f . c h a n f i l t , s e l f . probe ,

179 s e l f . power s ink )

118

Page 128: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

180 else :

181 s e l f . probe = gr . probe avg mag sqrd c ( thresh , alpha )

182 s e l f . connect ( s e l f . c h a n f i l t , s e l f . probe )

183

184 # Disp lay some in format ion about the se tup

185 i f s e l f . v e rbose :

186 s e l f . p r i n t v e r b a g e ( )

187

188 s e l f . connect ( ( s e l f . usource , 0 ) , s e l f . c h a n f i l t ,

189 s e l f . p a c k e t r e c e i v e r )

190 s e l f . connect ( ( s e l f . usource , 1 ) , s e l f . c h a n f i l t 2 ,

191 s e l f . p a c k e t r e c e i v e r 2 )

192

193 def s e t u p u s r p s i n k ( s e l f ) :

194 ”””

195 Creates a USRP sink , determines

196 the s e t t i n g s f o r b e s t b i t r a t e ,

197 and a t t a che s to the t r an smi t t e r ’ s subdev i c e .

198 ”””

199

200 dac ra t e = 128 e6

201 i n t e r p r a t e s = [ 1 6 , 20 , 24 , 28 , 32 , 36 , 40 , 44 ,

202 48 , 52 , 56 , 60 , 64 , 68 , 72 , 76 ,

203 80 , 84 , 88 , 92 , 96 , 100 , 104 , 108 ,

204 112 , 116 , 120 , 124 , 128 , 132 , 136 ,

205 140 , 144 , 148 , 152 , 156 , 160 , 164 ,

206 168 , 172 , 176 , 180 , 184 , 188 , 192 ,

207 196 , 200 , 204 , 208 , 212 , 216 , 220 ,

208 224 , 228 , 232 , 236 , 240 , 244 , 248 ,

209 252 , 256 , 260 , 264 , 268 , 272 , 276 ,

119

Page 129: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

210 280 , 284 , 288 , 292 , 296 , 300 , 304 ,

211 308 , 312 , 316 , 320 , 324 , 328 , 332 ,

212 336 , 340 , 344 , 348 , 352 , 356 , 360 ,

213 364 , 368 , 372 , 376 , 380 , 384 , 388 ,

214 392 , 396 , 400 , 404 , 408 , 412 , 416 ,

215 420 , 424 , 428 , 432 , 436 , 440 , 444 ,

216 448 , 452 , 456 , 460 , 464 , 468 , 472 ,

217 476 , 480 , 484 , 488 , 492 , 496 , 500 ,

218 504 , 508 , 512 ]

219

220 ( s e l f . b i t r a t e , s e l f . samples per symbol ,

221 s e l f . i n t e r p ) = \

222 p i c k t x b i t r a t e ( s e l f . b i t r a t e ,

223 s e l f . m o d u l a t o r c l a s s . b i t s pe r symbo l ( ) ,

224 s e l f . samples per symbol , s e l f . i n t e rp ,

225 dac rate , i n t e r p r a t e s )

226

227 s e l f . us ink = grc us rp . d u a l s i n k c ( which=s e l f . which )

228 s e l f . us ink . s e t i n t e r p r a t e ( s e l f . i n t e r p )

229 s e l f . us ink . s e t f r e q u e n c y a (2 . 475 e9 , verbose=True )

230 s e l f . us ink . s e t f r e q u e n c y b (450 e6 , verbose=True )

231 s e l f . us ink . s e t g a i n a (0 )

232 s e l f . us ink . s e t g a i n b (0 )

233 s e l f . us ink . s e t e n a b l e a ( True )

234 s e l f . us ink . s e t e n a b l e b ( True )

235 # enab l e Auto Transmit/Receive sw i t ch ing

236 s e l f . us ink . s e t a u t o t r a ( True )

237 s e l f . us ink . s e t a u t o t r b ( True )

238 print s e l f . i n t e r p

239 def s e t u p u s r p s o u r c e ( s e l f ) :

120

Page 130: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

240 ”””

241 Creates a USRP source ,

242 determines the s e t t i n g s f o r b e s t b i t r a t e ,

243 and a t t a che s to the r e c e i v e r ’ s subdev i c e .

244 ”””

245

246 adc ra t e = 64 e6

247 dec im rate s = [ 8 , 10 , 12 , 14 , 16 , 18 , 20 , 22 ,

248 24 , 26 , 28 , 30 , 32 , 34 , 36 , 38 ,

249 40 , 42 , 44 , 46 , 48 , 50 , 52 , 54 ,

250 56 , 58 , 60 , 62 , 64 , 66 , 68 , 70 ,

251 72 , 74 , 76 , 78 , 80 , 82 , 84 , 86 ,

252 88 , 90 , 92 , 94 , 96 , 98 , 100 , 102 ,

253 104 , 106 , 108 , 110 , 112 , 114 , 116 ,

254 118 , 120 , 122 , 124 , 126 , 128 , 130 ,

255 132 , 134 , 136 , 138 , 140 , 142 , 144 ,

256 146 , 148 , 150 , 152 , 154 , 156 , 158 ,

257 160 , 162 , 164 , 166 , 168 , 170 , 172 ,

258 174 , 176 , 178 , 180 , 182 , 184 , 186 ,

259 188 , 190 , 192 , 194 , 196 , 198 , 200 ,

260 202 , 204 , 206 , 208 , 210 , 212 , 214 ,

261 216 , 218 , 220 , 222 , 224 , 226 , 228 ,

262 230 , 232 , 234 , 236 , 238 , 240 , 242 ,

263 244 , 246 , 248 , 250 , 252 , 254 , 256 ]

264

265 ( s e l f . b i t r a t e , s e l f . samples per symbol ,

266 s e l f . decim ) = \

267 p i c k r x b i t r a t e ( s e l f . b i t r a t e ,

268 s e l f . demod c lass . b i t s pe r symbo l ( ) ,

269 s e l f . samples per symbol , s e l f . decim ,

121

Page 131: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

270 adc rate ,

271 dec im rate s )

272

273 s e l f . usource = grc us rp . d u a l s o u r c e c (

274 which=s e l f . which ,

275 rx ant a=”TX/RX” , rx ant b=”RX2” ,

276 r x s o u r c e a=”A” , r x s o u r c e b=”B” ,

277 )

278 s e l f . usource . s e t d e c i m r a t e ( s e l f . decim )

279 s e l f . usource . s e t f r e q u e n c y a (2 . 425 e9 , verbose=True )

280 s e l f . usource . s e t f r e q u e n c y b (400 e6 , verbose=True )

281 s e l f . usource . s e t g a i n a (0 )

282 s e l f . usource . s e t g a i n b (0 )

283

284 def s e t tx amp l i tude ( s e l f , ampl ) :

285 ”””

286 Se t s the t ransmi t ampl i tude sen t to the USRP

287 @param : ampl 0 <= ampl < 32767. Try 8000

288 ”””

289 s e l f . tx ampl i tude = max( 0 . 0 , min ( ampl , 32767 . 0 ) )

290 s e l f . amp . s e t k ( s e l f . tx ampl i tude )

291 s e l f . amp 2 . s e t k ( s e l f . tx ampl i tude )

292

293 def send pkt ( s e l f , payload=’ ’ , e o f=Fal se ) :

294 ”””

295 Ca l l s the t r an sm i t t e r method to send a packe t

296 ”””

297 return s e l f . pa cke t t r an sm i t t e r . send pkt ( payload , eo f )

298

299 def b i t r a t e ( s e l f ) :

122

Page 132: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

300 return s e l f . b i t r a t e

301

302 def samples per symbol ( s e l f ) :

303 return s e l f . samples per symbol

304

305 def i n t e r p ( s e l f ) :

306 return s e l f . i n t e r p

307

308 def decim ( s e l f ) :

309 return s e l f . decim

310

311 def c a r r i e r s e n s e d ( s e l f ) :

312 ”””

313 Return True i f we t h ink c a r r i e r i s p re sen t .

314 ”””

315 #return s e l f . probe . l e v e l ( ) > X

316 return s e l f . probe . unmuted ( )

317

318 def c a r r i e r t h r e s h o l d ( s e l f ) :

319 ”””

320 Return curren t s e t t i n g in dB .

321 ”””

322 return s e l f . probe . th r e sho ld ( )

323

324 def s e t c a r r i e r t h r e s h o l d ( s e l f , t h r e s h o l d i n d b ) :

325 ”””

326 Set c a r r i e r t h r e s h o l d .

327

328 @param t h r e s h o l d i n d b : s e t d e t e c t i on t h r e s h o l d

329 @type t h r e s h o l d i n d b : f l o a t (dB)

123

Page 133: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

330 ”””

331 s e l f . probe . s e t t h r e s h o l d ( t h r e s h o l d i n d b )

332

333 def add opt ions ( normal , expert ) :

334 ”””

335 Adds t ransmi t t e r−s p e c i f i c op t i ons

336 to the Options Parser

337 ”””

338 a d d f r e q o p t i o n ( normal )

339 i f not normal . has opt ion ( ’−−b i t r a t e ’ ) :

340 normal . add opt ion ( ”−r ” , ”−−b i t r a t e ” ,

341 type=” e n g f l o a t ” ,

342 d e f a u l t =100e3 ,

343 help=” s p e c i f y b i t r a t e [ d e f a u l t=%d e f a u l t ] . ” )

344 normal . add opt ion ( ”−T” , ”−−tx−subdev−spec ” ,

345 type=”subdev” ,

346 d e f a u l t=None ,

347 help=” s e l e c t USRP Tx s i d e A or B” )

348 normal . add opt ion ( ”” , ”−−tx−amplitude ” ,

349 type=” e n g f l o a t ” ,

350 d e f a u l t =3000 , metavar=”AMPL” ,

351 help = \

352 ” s e t t r an smi t t e r d i g i t a l amplitude : 0 ” + \

353 ”<= AMPL < 32768 [ d e f a u l t=%d e f a u l t ] ” )

354 normal . add opt ion ( ”−v” , ”−−verbose ” ,

355 ac t i on=” s t o r e t r u e ” , d e f a u l t=False )

356

357 normal . add opt ion ( ”−w” , ”−−which” , type=” i n t ” ,

358 d e f a u l t =0,

359 help=” s e l e c t USRP board [ d e f a u l t=%d e f a u l t ] ” )

124

Page 134: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

360 expert . add opt ion ( ”−S” , ”−−samples−per−symbol” ,

361 type=” i n t ” , d e f a u l t =2,

362 help=” s e t samples /symbol [ d e f a u l t=%d e f a u l t ] ” )

363 expert . add opt ion ( ”” , ”−−tx−f r e q ” , type=” e n g f l o a t ” ,

364 d e f a u l t=None ,

365 help = \

366 ” s e t transmit f requency to FREQ [ d e f a u l t=%d e f a u l t ] ” ,

367 metavar=”FREQ” )

368 expert . add opt ion ( ”− i ” , ”−−i n t e r p ” , type=” intx ” ,

369 d e f a u l t=None ,

370 help = \

371 ” s e t fpga i n t e r p o l a t i o n ra t e ” + \

372 ” to INTERP [ d e f a u l t=%d e f a u l t ] ” )

373 expert . add opt ion ( ”” , ”−−l og ” , a c t i on=” s t o r e t r u e ” ,

374 d e f a u l t=False ,

375 help = \

376 ”Log a l l par t s o f f low graph ” + \

377 ” to f i l e (CAUTION: l o t s o f data ) ” )

378 expert . add opt ion (\

379 ”” , ”−−use−whitener−o f f s e t ” , a c t i on=” s t o r e t r u e ” ,

380 d e f a u l t=False ,

381 help = \

382 ”make s e q u e n t i a l packets use d i f f e r e n t whitening ” )

383

384 ”””

385 Adds rece i v e r−s p e c i f i c op t i ons to the Options Parser

386 ”””

387 normal . add opt ion ( ”−R” , ”−−rx−subdev−spec ” ,

388 type=”subdev” ,

389 d e f a u l t=None , he lp=” s e l e c t USRP Rx s i d e A or B” )

125

Page 135: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

390 normal . add opt ion ( ”” , ”−−rx−gain ” ,

391 type=” e n g f l o a t ” ,

392 d e f a u l t=None , metavar=”GAIN” ,

393 help = \

394 ” s e t r e c e i v e r gain in dB [ d e f a u l t=midpoint ] . ” + \

395 ” See a l s o −−show−rx−gain−range ” )

396 normal . add opt ion ( ”” , ”−−show−rx−gain−range ” ,

397 ac t i on=” s t o r e t r u e ” , d e f a u l t=False ,

398 help = \

399 ” p r i n t min and max Rx gain ” + \

400 ” a v a i l a b l e on s e l e c t e d daughterboard ” )

401

402 expert . add opt ion ( ”” , ”−−rx−f r e q ” , type=” e n g f l o a t ” ,

403 d e f a u l t=None ,

404 help = \

405 ” s e t Rx frequency to FREQ [ d e f a u l t=%d e f a u l t ] ” ,

406 metavar=”FREQ” )

407 expert . add opt ion ( ”−d” , ”−−decim” , type=” intx ” ,

408 d e f a u l t=None ,

409 help = \

410 ” s e t fpga decimation ra t e to ” + \

411 ”DECIM [ d e f a u l t=%d e f a u l t ] ” )

412 expert . add opt ion ( ”” , ”−−log−rx−power” ,

413 ac t i on=” s t o r e t r u e ” ,

414 d e f a u l t=False ,

415 help = \

416 ”Log r e c e i v e s i g n a l power to ” + \

417 ” f i l e (CAUTION: l o t s o f data ) ” )

418

419 # Make a s t a t i c method to c a l l b e f o r e i n s t a n t i a t i o n

126

Page 136: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

420 add opt ions = stat icmethod ( add opt ions )

421

422 def p r i n t v e r b a g e ( s e l f ) :

423 ”””

424 Prin t s in format ion about the t ransmi t path

425 ”””

426 print ”\nTransmit Path : ”

427 print ”Tx amplitude %s ” \

428 % ( s e l f . tx ampl i tude )

429 print ” modulation : %s ” \

430 % ( s e l f . m o d u l a t o r c l a s s . name )

431 print ” b i t r a t e : %sb/ s ” \

432 % ( eng notat ion . num to str ( s e l f . b i t r a t e ) )

433 print ” samples /symbol : %3d” \

434 % ( s e l f . samples per symbol )

435 print ” i n t e r p : %3d” \

436 % ( s e l f . i n t e r p )

437 print ”Tx Frequency : %s ” \

438 % ( eng notat ion . num to str ( s e l f . t x f r e q ) )

439

440 ”””

441 Prin t s in format ion about the r e c e i v e path

442 ”””

443 print ”\nReceive Path : ”

444 print ”Rx gain : %g” \

445 % ( s e l f . gain , )

446 print ” modulation : %s ” \

447 % ( s e l f . demod c lass . name )

448 print ” b i t r a t e : %sb/ s ” \

449 % ( eng notat ion . num to str ( s e l f . b i t r a t e ) )

127

Page 137: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

450 print ” samples /symbol : %3d” \

451 % ( s e l f . samples per symbol )

452 print ”decim : %3d” \

453 % ( s e l f . decim )

454 print ”Rx Frequency : %s ” \

455 % ( eng notat ion . num to str ( s e l f . r x f r e q ) )

456

457 def a d d f r e q o p t i o n ( par s e r ) :

458 ”””

459 Hackery t ha t has the −f / −−f r e q opt ion

460 s e t both t x f r e q and r x f r e q

461 ”””

462 def f r e q c a l l b a c k ( option , op t s t r , value , pa r s e r ) :

463 par s e r . va lue s . r x f r e q = value

464 par s e r . va lue s . t x f r e q = value

465

466 i f not par s e r . has opt ion ( ’−−f r e q ’ ) :

467 par s e r . add opt ion ( ’−f ’ , ’−−f r e q ’ ,

468 type=” e n g f l o a t ” ,

469 ac t i on=” c a l l b a c k ” , c a l l b a c k=f r e q c a l l b a c k ,

470 help = \

471 ” s e t Tx and/ or Rx frequency ” + \

472 ” to FREQ [ d e f a u l t=%d e f a u l t ] ” ,

473 metavar=”FREQ” )

128

Page 138: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

BIBLIOGRAPHY

[1] A. Csete. Simple DVB with Gstreamer and GNU Radio.

http://www.oz9aec.net/index.php/gnu-radio/gnu-radio-blog/361-simple-dvb-with-gstreamer-

and-gnu-radio. August 2010.

[2] ArWest Communications. http://www.arwestcom.com/. April 2011

[3] B. Sklar. Digital Communications: Fundamentals and Applications. Prentice Hall, 2nd ed., Jan-

uary 2001.

[4] D. O. Wu. Real-world Transmission with GNU Radio and USRP.

http://www.wu.ece.ufl.edu/. April 2011.

[5] DSP log. http://www.dsplog.com/. April 2011.

[6] E. Blossom. Exploring GNU Radio Revision v1.1.

http://www.gnu.org/software/gnuradio/doc/exploring-gnuradio.html. November 2004.

[7] Ettus Research LLC. http://www.ettus.com/. April 2011.

[8] GNU Radio. http://gnuradio.org/redmine/wiki/gnuradio. April 2011.

[9] GNU Radio Mailing Lists. http://www.gnu.org/software/gnuradio/mailinglists.html. April 2011.

[10] GNU Radio v3.2 documentation. http://www.reynwar.net/gnuradio/sphinx/index.html. April

2011.

[11] J. G. Proakis. Digital Communications. McGraw-Hill Professional, 4th ed., 2000.

[12] Josh Knows. http://www.joshknows.com/. April 2011.

[13] K. W. Rudd and C. Anderson. Software-Defined Radio. IEEE Computing Society. June 2010.

[14] M. Wu, D. Wubben, A. Dekorsy. BER-based Power Allocation for Amplify-and-Forward and

Decode-and-Forward Relaying Systems. 7th International Wireless Communications and Mobile

Computing Conference (IWCMC 2011). March 2011.

[15] Python v2.7.1 documentation. http://docs.python.org/index.html. May 2011.

[16] R. Morrow. Wireless network coexistence. McGraw-Hill Professional, 2004.

[17] S. Fu. Digital Communications. September 2009.

[18] Ubuntu. http://www.ubuntu.com/. December 2010.

129

Page 139: A Bidirectional Two-Hop Relay Network Using GNU Radio and USRP

[19] Wikipedia. http://en.wikipedia.org/. April 2011.

130