RTL-SDR Workbook 030815 ToC Only

12
Bob Stewart Kenneth Barlee Dale Atkinson Louise Crockett MATLAB & Simulink RTL-SDR and the u s i n g Using MATLAB & Simulink and the RTL-SDR Bob Stewart Kenneth Barlee Dale Atkinson Louise Crockett Table of Contents Preview Only August 9th 2015 www.desktopSDR.com

description

L-SDR Workbook 030815 ToC Only

Transcript of RTL-SDR Workbook 030815 ToC Only

Page 1: RTL-SDR Workbook 030815 ToC Only

Bob StewartKenneth BarleeDale AtkinsonLouise Crockett

SOFTWAREDEFINED RADIO

MATLAB &Simulink RTL-SDR

andthe

using

The availability of the RTL-SDR device for less than $20 brings software defi ned radio (SDR) to the home and work

desktops of EE students, professional engineers and the maker community. The RTL-SDR can be used to acquire

and sample RF (radio frequency) signals transmitted in the frequency range 25MHz to 1.75GHz, and the MATLAB

and Simulink environment can be used to develop receivers using fi rst principles DSP (digital signal processing)

algorithms. Signals that the RTL-SDR hardware can receive include: FM radio, UHF band signals, ISM signals, GSM,

3G and LTE mobile radio, GPS and satellite signals, and any that the reader can (legally) transmit of course! In this

book we introduce readers to SDR methods by viewing and analysing downconverted RF signals in the time and

frequency domains, and then provide extensive DSP enabled SDR design exercises which the reader can learn from.

The hands-on SDR design examples begin with simple AM and FM receivers, and move on to the more challenging

aspects of PHY layer DSP, where receive fi lter chains, real-time channelisers, and advanced concepts such as

carrier synchronisers, digital PLL designs and QPSK timing and phase synchronisers are implemented. In the

book we will also show how the RTL-SDR can be used with SDR transmitters to develop complete communication

systems, capable of transmitting payloads such as simple text strings, images and audio across the lab desktop.

Stay tuned (with SDR of course!) the Wireless Revolution is Just Beginning!

Using MATLAB & Sim

ulink and the RTL-SDRSOFTW

ARE SOFTW

ARE DEFINED

RADIO

Bob Stewart

Kenneth BarleeDale AtkinsonLouise Crockett

Table of Contents Preview Only

August 9th 2015

www.desktopSDR.com

Page 2: RTL-SDR Workbook 030815 ToC Only

Table o

ly

Software Defined Radio using MATLAB® & Simulink®

and the RTL-SDR

Robert W. StewartKenneth W. Barlee

Dale S. W. AtkinsonLouise H. Crockett

Department of Electronic and Electrical EngineeringUniversity of StrathclydeGlasgow, Scotland, UK

1st Edition

f Contents Preview On

August 9th 2015

www.desktopSDR.com

Page 3: RTL-SDR Workbook 030815 ToC Only

Table o

iew Only

f Contents Prev

August 9th 2015

www.desktopSDR.com

Page 4: RTL-SDR Workbook 030815 ToC Only

Table o

ly

Table of Contents

1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1 Real Time Desktop Software Defined Radio . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2 What is the RTL-SDR? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.3 What Do I Need to Get Started? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.4 The Aim and Objectives of this Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71.5 How to Use This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81.6 The Evolution of the Software Defined Radio Architecture . . . . . . . . . . . . . 81.7 RTL-SDR Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121.8 Interfacing with the RTL-SDR from MATLAB and Simulink . . . . . . . . . . 171.9 Practicalities and Some Challenges of (Low Cost) Desktop SDR . . . . . . . . . 201.10 The Structure of the Book and Format of the Exercises. . . . . . . . . . . . . . . . . 22

2 Open the Box! First SDR with MATLAB and Simulink . . . . . . . . . . . . . . . . . . 252.1 Getting Started: Hardware and Software Checklist . . . . . . . . . . . . . . . . . . . . 252.2 Getting Started: Installing the RTL-SDR Hardware Support Package. . . . . 272.3 Getting Started: Book Support Files and the MATLAB Environment. . . . . 312.4 Running the First Desktop RTL-SDR Receiver Designs . . . . . . . . . . . . . . . . 342.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

3 Radio Frequency Spectrum Viewing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433.1 Different Signals, Different Frequencies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443.2 Spectrum Usage and Allocations Around the Globe . . . . . . . . . . . . . . . . . . . 453.3 Working with a Suitable Antenna . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463.4 Go Forth and Explore the Spectrum! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493.5 Spectral Viewing—Spectrum Analyser and Waterfall Plots . . . . . . . . . . . . . 523.6 Spectral Viewing—RTL-SDR Tuner GUI Controls . . . . . . . . . . . . . . . . . . . . 533.7 Engineering Requirements—Eyeball Radio Tuning & More . . . . . . . . . . . . 543.8 FM Radio Stations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593.9 Mobile (Cell) Phone Signals—2G, 3G and 4G. . . . . . . . . . . . . . . . . . . . . . . . . 633.10 433MHz: Key Fobs and Wireless Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743.11 Digital Video & Audio Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783.12 Using Multiple RTL-SDRs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823.13 Sweeping the Spectrum: Receiving from 25MHz to 1.75GHz. . . . . . . . . . . . 883.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

4 Getting Started with MATLAB and Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . 974.1 Introducing MATLAB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 984.2 MATLAB Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054.3 Plotting in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1094.4 MATLAB Arrays, Matrices, and Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . 1134.5 MATLAB System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1194.6 Introducing Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1234.7 Creating Simulink Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1254.8 Variables and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

f Contents Preview On

August 9th 2015

www.desktopSDR.com

i

Page 5: RTL-SDR Workbook 030815 ToC Only

ii Software Defined Radio Using MATLAB & Simulink and the RTL-SDRTable o

iew Only

4.9 Generating Frequency Domain Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1484.10 Sampling Rates, Samples and Frames. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1504.11 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1574.12 Working with Input and Output Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1594.13 Saving and Re-importing RTL-SDR Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1634.14 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

5 Complex Signals, Spectra and Quadrature Modulation . . . . . . . . . . . . . . . . . . . 1695.1 Real and Complex Signals — it’s all Sines and Cosines . . . . . . . . . . . . . . . . . 1705.2 Real and Complex Signals in the Frequency Domain . . . . . . . . . . . . . . . . . . 1715.3 Modulation using Complex Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1805.4 Quadrature Modulation and Demodulation. . . . . . . . . . . . . . . . . . . . . . . . . . 1845.5 Quadrature Amplitude Modulation using Complex Notation. . . . . . . . . . . 1885.6 Quadrature Amplitude Demodulation using Complex Notation . . . . . . . . 1895.7 Spectral Representation for Complex Demodulation. . . . . . . . . . . . . . . . . . . 1905.8 Overview of RTL-SDR Reception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1945.9 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

6 Amplitude Modulation (AM) Theory and Simulation . . . . . . . . . . . . . . . . . . . . 1976.1 Amplitude Modulation—An Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 1976.2 AM-DSB-SC: Double Sideband Suppressed Carrier AM . . . . . . . . . . . . . . . 1976.3 AM-DSB-TC: Double Sideband Transmitted Carrier AM . . . . . . . . . . . . . . 2046.4 AM-SSB: Single Sideband AM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2116.5 AM-VSB: Vestigial Sideband AM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2196.6 Theoretical AM Demodulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2216.7 Non-Coherent AM Demod: The Envelope Detector . . . . . . . . . . . . . . . . . . . 2226.8 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

7 Frequency Tuning and Simple Synchronisation . . . . . . . . . . . . . . . . . . . . . . . . . 2277.1 Selecting a Frequency Band: Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2277.2 The Synchronisation Problem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2307.3 Demodulation of AM Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2337.4 Coherent Demodulation and Carrier Synchrony . . . . . . . . . . . . . . . . . . . . . . 2347.5 Introduction to Phase Locked Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2377.6 Discrete Time PLL Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2447.7 PLL Behaviours, Parameters and Characteristics . . . . . . . . . . . . . . . . . . . . . . 2487.8 PLL Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2567.9 PLL Performance in Noise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2647.10 Carrier Synchronisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2657.11 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

8 Desktop AM Transmission and Reception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2718.1 Transmitting AM Signals with the USRP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2728.2 Implementing Non-Coherent AM Receivers with the RTL-SDR . . . . . . . . 2828.3 Implementing Coherent AM Receivers with the RTL-SDR . . . . . . . . . . . . . 3038.4 Audio Multiplexing with the USRP and RTL-SDR . . . . . . . . . . . . . . . . . . . . 3108.5 Alternative Ways to Make AM Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3208.6 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321

f Contents Prev

August 9th 2015

www.desktopSDR.com

ii

Page 6: RTL-SDR Workbook 030815 ToC Only

iiiTable of ContentsTable o

ly

9 Frequency Modulation (FM) Theory and Simulation . . . . . . . . . . . . . . . . . . . . 3239.1 The History of the FM Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3239.2 The Mathematics of FM & the Modulation Index . . . . . . . . . . . . . . . . . . . . . 3259.3 FM Signal Bandwidth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3289.4 Theoretical FM Demodulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3419.5 Non-Coherent FM Demodulation: The Complex Differentiation

Discriminator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3429.6 Non-Coherent FM Demodulation: The Complex Delay Line

Discriminator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3469.7 Coherent FM Demodulation: The Phase Locked Loop . . . . . . . . . . . . . . . . . 3499.8 Demodulating Signals from Commercial FM Radio Stations . . . . . . . . . . . . 3529.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356

10 Desktop FM Transmission and Reception. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35710.1 Transmitting Mono WFM Signals with the USRP . . . . . . . . . . . . . . . . . . . . . 35810.2 Implementing Mono FM Receivers with RTL-SDR and Simulink. . . . . . . . 36610.3 Transmitting Stereo WFM Signals with the USRP . . . . . . . . . . . . . . . . . . . . . 38110.4 Implementing Stereo FM Receivers with RTL-SDR and Simulink . . . . . . . 38710.5 Manipulating the MPX: Transmitting AM Signals with FM Transmitters. 39910.6 Manipulating the MPX: Audio Multiplexing with FM Transmitters. . . . . . 40510.7 Alternative Ways to Make FM Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41310.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415

11 Digital Communications Theory and Simulation . . . . . . . . . . . . . . . . . . . . . . . 41711.1 Digital Modulation Schemes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41711.2 Pulse Shaping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42711.3 Digital Up and Downconversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43211.4 Carrier Synchronisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43811.5 Timing Errors and Symbol Recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44811.6 Symbol Timing Synchronisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45411.7 Digital Receiver Design: Joint Carrier and Timing Synchronisation . . . . . . 46711.8 Coarse Frequency Synchronisation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47011.9 Phase Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47711.10 Differential Encoding and Decoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47811.11 Synchronisation with a Unique Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48811.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491

12 Desktop Digital Communications: QPSK Transmission and Reception . . . . 49312.1 Pulse Shaping with Real Time QPSK Transmitter and Receiver Designs . . 49412.2 Coarse Frequency Synchronisation in a Real-time System . . . . . . . . . . . . . . 50212.3 Carrier and Timing Synchronisation with the RTL-SDR . . . . . . . . . . . . . . . 50512.4 Developing a Simple Communications Protocol . . . . . . . . . . . . . . . . . . . . . . 51312.5 ASCII Encoding and Decoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51512.6 Data and Frame Synchronisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51812.7 Frame Synchronisation: Offset Estimation and Correction Methods . . . . . 52712.8 ASCII Message Transmission and Reception . . . . . . . . . . . . . . . . . . . . . . . . . 53812.9 Transmitting Images Across the Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544

f Contents Preview On

August 9th 2015

www.desktopSDR.com

iii

Page 7: RTL-SDR Workbook 030815 ToC Only

iv Software Defined Radio Using MATLAB & Simulink and the RTL-SDRTable o

iew Only

12.10 Transmitting Data Using FM Transmitters . . . . . . . . . . . . . . . . . . . . . . . . . . 55012.11 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560

Appendix A: Hardware Setup and Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . 565A.1 The RTL-SDR Hardware Support Package . . . . . . . . . . . . . . . . . . . . . . . . . . . 565A.2 The USRP Hardware Support Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567A.3 RTL-SDR Frequency Error Correction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569A.4 Troubleshooting—more information can be found at desktopSDR.com. . 573

Appendix B: Common Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577Appendix C: Digital Filtering and Multirate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579

C.1 Filter Classes and Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579C.2 Filter Specification and Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580C.3 Computation and Trade-offs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582C.4 Multirate Filtering: The Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582C.5 Decimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584C.6 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585

Appendix D: PLL Design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587D.1 Digital Type 2 PLL Linear Model and Z-Domain Transfer Function . . . . . 587D.2 Analogue Type 2 PLL Linear Model and S-Domain Transfer Function. . . 590D.3 Extraction of Digital PLL Parameters Based on Analogue PLL

Equivalence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591D.4 Phase Detector Gain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596D.5 Oscillator Gain. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600

Appendix E: AM and FM Transmitters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601E.1 Upconverting AM Radio Signals with the Ham It Up. . . . . . . . . . . . . . . . . . 601E.2 Building an ‘RT4’ 433.9MHz AM Transmitter . . . . . . . . . . . . . . . . . . . . . . . . 606E.3 Using the Raspberry Pi as an FM Transmitter . . . . . . . . . . . . . . . . . . . . . . . . 613

f Contents Prev

August 9th 2015

www.desktopSDR.com

iv

Page 8: RTL-SDR Workbook 030815 ToC Only

vTable o

ly

Software Defined Radio (SDR) Simulation and Design Exercises

The titles of the various simulation exercises in this workbook are given below.

1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Open the Box! First SDR with MATLAB and Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

• Verify Software Setup: MATLAB and Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 • Verify Software Setup: RTL-SDR Hardware Support Package . . . . . . . . . . . . . . . . . . . . . . . 28 • Verify Hardware Setup: RTL-SDR Hardware Support Package . . . . . . . . . . . . . . . . . . . . . . 29 • MATLAB (and Simulink) Working Environment Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 • First Use of the RTL-SDR: Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 • First Use of the RTL-SDR: MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

3 Radio Frequency Spectrum Viewing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 • Opening the Spectrum Viewing Receiver Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 • An Introduction to Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 • Changing the Tuner Gain of the RTL-SDR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 • Searching for FM Radio Stations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 • Exploring the Mobile Spectrum: 2G GSM (800–1000MHz) . . . . . . . . . . . . . . . . . . . . . . . . . 63 • Exploring the Mobile Spectrum: 3G UMTS (800–900MHz) . . . . . . . . . . . . . . . . . . . . . . . . 65 • Exploring the Mobile Spectrum: 4G LTE-A (700–900MHz) . . . . . . . . . . . . . . . . . . . . . . . . 67 • Exploring the Mobile Spectrum: Challenges! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 • Searching for Key Fob and Wireless Sensor Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 • Searching for DVB-T Digital TV Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 • Searching for DAB Digital Audio Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 • Exploring the Spectrum with Multiple RTL-SDRs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 • Sweeping the Radio Frequency Spectrum: 25MHz to 1.75GHz . . . . . . . . . . . . . . . . . . . . . . 89

4 Getting Started with MATLAB and Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 • MATLAB Orientation and Using the Command Window . . . . . . . . . . . . . . . . . . . . . . . . . 98 • MATLAB Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 • Functions in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 • Writing Your Own Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 • MATLAB Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 • MATLAB Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 • Matrices in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 • MATLAB Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 • An Introduction to System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 • Simulink Orientation and the Simulink Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 • Building and Simulating a First System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 • Manipulating Blocks and Wires . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 • Commenting Blocks Out, and Commenting Through Blocks . . . . . . . . . . . . . . . . . . . . . . . 139 • Working with Variables and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 • Setting up Variables as a Model Loads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

f Contents Preview On

August 9th 2015

www.desktopSDR.com

v

Page 9: RTL-SDR Workbook 030815 ToC Only

vi Software Defined Radio Using MATLAB & Simulink and the RTL-SDRTable o

iew Only

• Writing Simulation Results to the Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145• Frequency Domain Plots: Spectrum Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149• Upsampling and Downsampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151• Conversion Between Samples and Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156• Real and Complex Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157• Write and Read .mat Data Files (General Case) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159• Read and Write Audio Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162• Saving and Importing RTL-SDR Data in MATLAB and Simulink . . . . . . . . . . . . . . . . . . . 164

5 Complex Signals, Spectra and Quadrature Modulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169• Generating Frequency Domain Plots with MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . 173• Generating Frequency Domain Plots with Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174• Plotting Complex Spectra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175• Magnitude and Phase Spectra of Two Real Cosine Waves . . . . . . . . . . . . . . . . . . . . . . . . . . 177• Complex Spectra for Two Real Sine Waves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178• ‘Perfect’ Complex Demodulation: Two Sine Waves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191• ‘Perfect’ Complex Demodulation: Generic Bandpass Signal . . . . . . . . . . . . . . . . . . . . . . . . 193• Modulation, Demodulation, and Frequency Offset Correction . . . . . . . . . . . . . . . . . . . . . 194

6 Amplitude Modulation (AM) Theory and Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197• AM-DSB-SC Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201• AM-DSB-TC Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208• AM-SSB Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

7 Frequency Tuning and Simple Synchronisation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227• ‘Perfect’ Modulation and Demodulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235• Modulation and Demodulation (out of synchrony) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236• Phase Detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245• Loop Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246• Phase Detector and Loop Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246• Type 2 PLL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247• PLL Linear Model: Steady State Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255• PLL Linear Model: Effect of Damping Ratio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255• Challenge: Design of a Type 2 PLL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262• Type 2 PLL: Performance in Noise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264• Coherent Receiver for AM-DSB-TC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266• Coherent Receiver for AM-DSB-SC (Costas Loop) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268

8 Desktop AM Transmission and Reception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271• USRP: AM-DSB-SC Modulator and Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273• USRP: AM-DSB-TC Modulator and Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277• USRP: AM-SSB Modulator and Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280• RTL-SDR: Envelope Detector for AM-DSB-TC Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284• RTL-SDR: MATLAB Envelope Detector for AM-DSB-TC Signals . . . . . . . . . . . . . . . . . . 292• RTL-SDR: Envelope Detector for AM-DSB-SC Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297• RTL-SDR: Complex Demodulator for USRP AM-SSB Signals . . . . . . . . . . . . . . . . . . . . . . 300• RTL-SDR: PLL Demodulator for AM-DSB-TC Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304• RTL-SDR: Costas Demodulator for AM-DSB-SC Signals . . . . . . . . . . . . . . . . . . . . . . . . . . 307

f Contents Prev

August 9th 2015

www.desktopSDR.com

vi

Page 10: RTL-SDR Workbook 030815 ToC Only

viiSoftware Defined Radio (SDR) Simulation and Design ExercisesTable o

ly

• FDM AM: FDM MPX’er, AM Modulator and USRP Transmitter . . . . . . . . . . . . . . . . . . . 312 • FDM AM: RTL-SDR AM Receiver and Demultiplexer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315

9 Frequency Modulation (FM) Theory and Simulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 • Narrowband FM (NFM) Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 • Wideband FM (WFM) Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 • Stereo FM Encoder and Multiplexer Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

10 Desktop FM Transmission and Reception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 • USRP: Mono FM Modulator and Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 • RTL-SDR: Mono FM Radio Receiver (Discriminator) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366 • RTL-SDR: MATLAB Mono FM Radio Receiver (Discriminator) . . . . . . . . . . . . . . . . . . . . 374 • RTL-SDR: Mono FM Radio Receiver (Complex Differentiation) . . . . . . . . . . . . . . . . . . . . 378 • RTL-SDR: Mono FM Radio Receiver (Slope Detector) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 • USRP: Stereo FM Modulator and Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 • RTL-SDR: Stereo FM Radio Receiver and Decoder (Discrim) . . . . . . . . . . . . . . . . . . . . . . . 388 • RTL-SDR: Stereo FM Radio Receiver and Decoder (PLL) . . . . . . . . . . . . . . . . . . . . . . . . . . 396 • AM in FM: Multiplexer, Modulator and USRP Transmitter . . . . . . . . . . . . . . . . . . . . . . . . 400 • AM in FM: RTL-SDR FM Receiver and AM Demodulator . . . . . . . . . . . . . . . . . . . . . . . . . 402 • FDM FM: FDM MPXer & FM MPXer, Mod and USRP Transmitter . . . . . . . . . . . . . . . . . 407 • FDM FM: RTL-SDR FM Receiver and Demultiplexer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410

11 Digital Communications Theory and Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 • Bit to Symbol Mapping and Demapping (QPSK) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422 • QPSK Symbol Mapping and Demapping: Separate I and Q . . . . . . . . . . . . . . . . . . . . . . . . . 423 • Bit to Symbol Mapping and Demapping in Noise (QPSK) . . . . . . . . . . . . . . . . . . . . . . . . . . 425 • 16-QAM Symbol Mapping and Demapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426 • Higher Order Constellations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 • Pulse Shaping and Transmission Bandwidth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 • Raised Cosine Inter-Symbol-Interference Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430 • RRC Inter-Symbol-Interference Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 • Matched Filtering of QPSK Modulated Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432 • Digital Upconverter (DUC): Filter Cascade & Modulation . . . . . . . . . . . . . . . . . . . . . . . . . 436 • Digital Downconverter (DDC): Demodulation & Filter Cascade . . . . . . . . . . . . . . . . . . . . 437 • Carrier Synchronisation for QPSK (Demodulation) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 • Carrier Synchronisation for QPSK (Baseband) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 • Matched Filtering and Maximum Effect Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 • Symbol Timing Imperfections: Sampling Phase Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 • Symbol Timing Imperfections: Sampling Frequency Error . . . . . . . . . . . . . . . . . . . . . . . . . 451 • Symbol Decisions: Sampling Phase Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452 • Symbol Decisions: Sampling Frequency Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 • Numerically Controlled Clock (NCC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 • Early Late Timing Synchronisation (Raised Cosine Pulses) . . . . . . . . . . . . . . . . . . . . . . . . . 463 • Early Late TED: Gain Coefficient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 • Early Late Timing Synchronisation (Design Task) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 • Joint Carrier and Timing Synchronisation for QPSK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467 • Coarse Frequency Correction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474

f Contents Preview On

August 9th 2015

www.desktopSDR.com

vii

Page 11: RTL-SDR Workbook 030815 ToC Only

viii Software Defined Radio Using MATLAB & Simulink and the RTL-SDRTable o

iew Only

• QPSK Synchronisation (with Coarse Frequency Correction) . . . . . . . . . . . . . . . . . . . . . . . 475• Implementation of a BPSK Differential Encoder & Decoder . . . . . . . . . . . . . . . . . . . . . . . . 481• Implementing a QPSK Differential Encoder & Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . 486

12 Desktop Digital Communications: QPSK Transmission and Reception . . . . . . . . . . . . . . 493• RRC Transmit Pulse Shaping with the USRP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495• RRC Matched Filtering in an RTL-SDR Receiver Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 499• Coarse Frequency Correction: Inspecting the Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . 502• Coarse Frequency Correction: Investigation with the Receiver . . . . . . . . . . . . . . . . . . . . . . 503• Carrier & Timing Synchronisation: Inspecting the Transmitter . . . . . . . . . . . . . . . . . . . . . 506• RTL-SDR and Theory Synchronisation Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507• Further Investigation of Real-time RTL-SDR Synchronisation . . . . . . . . . . . . . . . . . . . . . . 510• ASCII Encoding using MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515• ASCII Decoding using MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517• Numbered ASCII Frame Generator for Transmitter Designs . . . . . . . . . . . . . . . . . . . . . . . 524• Frame Synchronisation: Cross-correlation Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529• Frame Synchronisation: Matched Filter Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534• ASCII Message Tx Rx: USRP Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538• ASCII Message Tx Rx: RTL-SDR Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539• Image Tx Rx: USRP Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545• Image Tx Rx: RTL-SDR Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546• Data in FM: ASCII/ Audio Signal Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552• Data in FM: RTL-SDR FM Demod & ASCII/ Audio Receiver . . . . . . . . . . . . . . . . . . . . . . 554• Data in FM: Image/ Audio Signal Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558• Data in FM: RTL-SDR FM Demod & Image/ Audio Receiver . . . . . . . . . . . . . . . . . . . . . . 559

Appendix A:Hardware Setup and Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565• Verify Hardware Setup: USRP Hardware Support Package . . . . . . . . . . . . . . . . . . . . . . . . . 567• Finding the PPM Error of your RTL-SDR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569

Appendix B:Common Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577Appendix C:Digital Filtering and Multirate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579Appendix D:PLL Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587Appendix E:AM and FM Transmitters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601

• Ham It Up: Hardware Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601• RTL-SDR: Envelope Detector for HIU AM-DSB-TC Signals . . . . . . . . . . . . . . . . . . . . . . . 604• Build the RT4 AM Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607• Test the RT4 AM Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611• Refine your RT4 AM Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611• PiFM: Backing Up A Blank SD Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613• PiFM: Setting up a Raspberry Pi FM Radio Station . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615• PiFM: Restoring the SD Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621

f Contents Prev

August 9th 2015

www.desktopSDR.com

viii

Page 12: RTL-SDR Workbook 030815 ToC Only

Bob StewartKenneth BarleeDale AtkinsonLouise Crockett

SOFTWAREDEFINED RADIO

MATLAB &Simulink RTL-SDR

andthe

using

The availability of the RTL-SDR device for less than $20 brings software defi ned radio (SDR) to the home and work

desktops of EE students, professional engineers and the maker community. The RTL-SDR can be used to acquire

and sample RF (radio frequency) signals transmitted in the frequency range 25MHz to 1.75GHz, and the MATLAB

and Simulink environment can be used to develop receivers using fi rst principles DSP (digital signal processing)

algorithms. Signals that the RTL-SDR hardware can receive include: FM radio, UHF band signals, ISM signals, GSM,

3G and LTE mobile radio, GPS and satellite signals, and any that the reader can (legally) transmit of course! In this

book we introduce readers to SDR methods by viewing and analysing downconverted RF signals in the time and

frequency domains, and then provide extensive DSP enabled SDR design exercises which the reader can learn from.

The hands-on SDR design examples begin with simple AM and FM receivers, and move on to the more challenging

aspects of PHY layer DSP, where receive fi lter chains, real-time channelisers, and advanced concepts such as

carrier synchronisers, digital PLL designs and QPSK timing and phase synchronisers are implemented. In the

book we will also show how the RTL-SDR can be used with SDR transmitters to develop complete communication

systems, capable of transmitting payloads such as simple text strings, images and audio across the lab desktop.

Stay tuned (with SDR of course!) the Wireless Revolution is Just Beginning!

Using MATLAB & Sim

ulink and the RTL-SDRSOFTW

ARE SOFTW

ARE DEFINED

RADIO

Bob Stewart

Kenneth BarleeDale AtkinsonLouise Crockett

Table of Contents Preview Only

August 9th 2015

www.desktopSDR.com