Guided Tutorial Hardware Considerations

= Tutorial: Using GNU Radio with Hardware =

Objectives

 * How GNU Radio helps bridge Simulation and the Real World
 * What can you do and what are you allowed to do?

Prerequisites

 * General familiarity with C++ and Python
 * Radio/RF knowledge
 * Our Suggested Reading list
 * The ARRL Handbook
 * Previous tutorials recommended:
 * A brief introduction to GNU Radio, SDR, and DSP
 * Intro to GR usage- GRC and flowgraphs
 * If looking to follow the QPSK example
 * Programming GR in Python
 * Programming GR in C++

-

Introduction
So far, all of the tutorials in this series have centered around using GNU Radio as a prototyping &amp; development platform through simulation. One of the great strengths of GNU Radio, though, is how easy it is to move from simulation to real-world operation. In this tutorial, we will build on what you have learned so far and create real-world transmitters and receivers.

In this tutorial, we will take your previous QPSK design and turn it into an actual transmitter. We will also build, from-scratch, a complete FM radio receiver that you can use to listen to your local FM radio stations. Along the way, we will discuss a number of considerations that come into play when working with real-world radio hardware.

Simulation vs The Real World
Moving from simulation to real-world operation also brings a host of new challenges to solve. Radio and RF engineering are very complex disciplines, and there is a lot to consider when building an actual radio system. Along with the challenges of designing the theory of your communications system, you now also need to deal with impairments from your radio hardware, issues introduced by your physical medium (i.e., your wireless or wire channel), synchronization design, etc.,

To learn more about real-world radio impairments, a great place to start is the ~45 minute tutorial that Matt Ettus teaches at conferences occasionally. You can find a video of it, here: YouTube: Practical Software Radio

If you want to learn more about radio impairments directly in GNU Radio, check out the hardware impairments models in /gr-channels/grc.

What Will I Need?
If you don't have an actual radio to work with, this tutorial is still useful. The first section (transmitting) will teach you the basics of interfacing with hardware. For the second section, you can simply use a recording from an actual radio, with nothing done to the data. We have provided a recording, which you can find in the gr-tutorials repository. You can use this in your flowgraph to simulate hardware passing you samples.

This tutorial will be most useful, though, if you have a piece of radio hardware that you can use with GNU Radio. There is a large and growing number of vendors that provide hardware with GNU Radio drivers, and they span from very cheap $20 receivers to very high-performance tens-of-thousands-of-dollars sort of systems.

The entirety of this tutorial is possible with a simple, low-bandwidth, half-duplex radio. Most of the tutorial is possible if you have a radio that can only receive, assuming it can tune to the FM bands. Most countries allot between ~87 MHz and ~108 MHz to FM broadcast radio, with some slight variations on the floor and ceiling of the range.

Legal Considerations
In this tutorial, we will be transmitting a signal, but you should directly connect your transmit antenna port to a spectrum analyzer or some other test equipment. If you do not directly wire your radio to a receiving device, you will be emitting over-the-air. If you do not have any sort of load attached to your transmit antenna, and you accidentally transmit with too much power, you risk high-power reflections damaging your radio hardware. In general, you should err on the side of caution and put an attenuator in your signal path before directly connecting any cables to antenna inputs on any hardware.

You are responsible for complying with the relevant laws in your country / state of operation. Not transmitting is the only guaranteed way to stay legal. Transmitting directly to an analyzer is another great option. If you do not have an analyzer, I recommend simply reading through the transmit section, but not actually executing the flowgraph.

Using UHD Blocks
The author is using an Ettus Research USRP B200, which uses the USRP Hardware Driver (UHD). Thus, the hardware sources &amp; sinks used in this tutorial will be gr-uhd blocks.

If you are using a different piece of hardware, you should use the blocks suggested by your hardware vendor. Another set of popular HW blocks, for example, is gr-osmosdr.

In order to use UHD blocks, you must have UHD installed. There are a number of ways to install UHD, which you can read about here. If you built GNU Radio from source, you should have seen gr-uhd in the list of components being built in the CMake output. If GNU Radio successfully found your UHD installation, you should see a group of UHD blocks in the GNU Radio Companion:



Picking Up QPSK Again
We will start by hacking on the QPSK flowgraph that you made in the Python programming tutorial. In that tutorial, you demodulated a QPSK signal and saved it to a file. Here, instead of demodulating the signal, we are going to transmit the QPSK signal you were generating.

Let's start by removing your demodulator and file sink, but leave the QT Constellation Sink. This should leave you with a flowgraph that looks something like this:



You can also just download the flowgraph pictured above to get rolling

If you execute that flowgraph, you should be looking at the symbol constellation for your QPSK signal (with some simulated noise).



Moving from Simulation to Real TX
Now, let's move from simulation to actually transmitting! First, connect your transmit antenna to your spectrum analyzer (if you intend to actually transmit).

Since we are no longer simulating a signal, let's get rid of that noise source and the adder. Because we have an actual hardware source providing rate control, we no longer require the throttle block. In fact, keeping the throttle block in the flowgraph might actually impede its performance by conflicting with the rate of the UHD device (for more on this, read this note in the FAQ).

Changing our program from being a simulation to an operational software radio is now as easy as dropping a new sink block into your flowgraph. Find the 'UHD Sink' block, add it to your flowgraph, and connect it to your modulator's output.

Configuring the UHD Sink Parameters
It is generally a good idea to create variable blocks for parameters you may tweak down the road. Let's go ahead and make a few for our radio parameters. Copy &amp; paste your samp_rate variable block twice, and create variables for freq and gain. Set freq and gain to something that your hardware supports. I chose 2.421 GHz for the center frequency. Set your gain to something in the lower range of your device. I set mine to 30 dB, which is 1/3 of the available TX gain in my device.

Now, let's use our new variable blocks as actual transmit parameters in the UHD sink. Open up the properties for the UHD sink block (double-click on it, or right click and then click 'Properties'), and you should see a property window come up with a list of parameters to tweak. There are a lot more properties here than we need to touch. Scroll down to the bottom of the list. Set-up the following properties accordingly:


 * Samp Rate (Sps): samp_rate
 * Ch0: Center Freq (Hz: freq
 * Ch0: Gain (dB): gain
 * Ch0: Antenna: &quot;TX/RX&quot;
 * Ch0: Bandwidth (Hz): samp_rate

A very important aspect to keep in mind is that a value of 1.0 going into the UHD sink will saturate the digital-to-analog converter completely. Any value larger than 1 will cause clipping, which means the amplitude is 'clipped off' above of 1.0. In general, you want to back off from 1.0. In this example, we'll simply multiply the output of the multiplier by 0.5 to make sure we are definitely in the right amplitude range.

At this point, your flowgraph is ready to run! It should look like this:



If you have had trouble following the instructions above, download the flowgraph here and compare it to your own

A Working Software Radio Transmitter
If you execute your flowgraph, you should see the usual initialization and GUI launch - except, instead of everything being simulation, you are actually transmitting your QPSK signal. Because we have our TX signal going to both the 'QT Constellation Sink' and 'UHD Sink', we can look at the symbol constellation as GNU Radio sees it, and the constellation as the analyzer sees it (if your analyzer has a QPSK demod function).

If we look at just the spectrum on the analyzer, you should be able to clearly see your signal at the frequency you selected. If you can't see it, double-check your parameters, and also try turning up your radio gain.



Congratulations! You have built a complete QPSK software radio transmitter!

Building an FM Radio Receiver
In this section, we will build a working FM radio receiver that you can use to listen to your local FM broadcast stations. This content is based on the tutorial slide series prepared by Balint Seeber, available here: files.ettus.com/tutorials

Creating a Software Radio Spectrum Analyzer
One of the more basic (but also incredibly useful) things you can do in GNU Radio with a receiver is simply plot a real-time FFT of what your receiver sees. It's a software radio spectrum analyzer. This is also a great first step because it will verify that your hardware is working correctly, and we can use it to identify broadcast stations in your area that we will tune to later on.

Create a new flowgraph. Drop a 'UHD: USRP Source' and 'QT GUI Sink' into the canvas. In the previous section, we used static variables for frequency and gain. Here, let's make them changeable on-the-fly. Find the 'QT GUI Range', and add two of them to your flowgraph. Open each of them, and fill out the following fields as appropriate for your hardware. Mine are filled out, below, to match the capabilities of the USRP B200:

QT GUI Range 1:


 * ID: freq
 * Label: freq
 * Default Value: 1e9
 * Start: 70e6
 * Stop: 6e9
 * Step: 10e6

QT GUI Range 2:


 * ID: gain
 * Label: gain
 * Default Value: 0
 * Start: 0
 * Stop: 74
 * Step: 1

Change your 'samp_rate' variable block value to something interesting. I set mine to 32 MSps. Now, open up the properties for the 'UHD: USRP Source'. Just as we used our variable blocks in the previous section for our radio parameters, we will do the same here. Use the 'ID's from the 'range blocks', above, to set the 'UHD Source' parameters:


 * Samp Rate (Sps): samp_rate
 * Ch0: Center Freq (Hz): freq
 * Ch0: Gain (dB): gain
 * Ch0: Antenna: &quot;TX/RX&quot;
 * Ch0: Bandwidth (Hz): samp_rate

When you are done, your flowgraph should look something like this:



You can also download this flowgraph if you want the reference file.

You should be all set!

Using Your Software Radio Spectrum Analyzer
Go ahead and run your flowgraph. You should see your received spectrum in real-time. Play with the sliders and values, and you should see everything update in real-time, as well. The default GNU Radio QT FFT sink has a number of useful features, such as averaging and peak-hold. Take some time to get familiar with them, now.

Building a Broadcast FM Receiver
Now that you have a basic receiver application running with a real-time display of your received data, let's build a useful application! Leave your current flowgraph in place. We will simply add more functionality into the flowgraph rather than replacing what is already there.

First, as you might have guessed, we will need to add a few more blocks to the flowgraph. Locate and add the following blocks:


 * 1x Rational Resampler
 * 1x WBFM Receiver
 * 1x Audio Sink
 * 2x Variable

Our flow of data will be the USRP Source -> Rational Resampler -> WBFM Receiver --> Audio Sink. Go ahead and connect the blocks together in that order. GNU Radio will likely highlight a lot of fields in red, indicating errors. We will now go through and configure the parameters, which will resolve these.

First, let's set-up those two new variable blocks you added. Create the following two variables with them:

Variable:


 * ID: audio_interp
 * Value: 4

Variable:


 * ID: audio_rate
 * Value: 48e3

Let's also go back and tune our sample rate variable, which we had previously. Change that variable to 250e3 (250 kHz).

Variable: samp_rate


 * ID: samp_rate
 * Value: 250e3

Now, let's configure your other three new blocks.

Audio Sink

This block is fairly straight-forward. It takes a stream of samples and plays them through your speakers. This block is installed with the gr-audio library, which is part of the core GNU Radio distribution. There are a few common / default rates that sound cards use - one of the most common is 48 kHz, which is what we will use, here (hence your configuration of the 'audio_rate' variable, above).

Open up the parameters of the Audio Sink. Make the following changes:


 * Sample Rate: audio_rate
 * OK to Block: &quot;No&quot;

Now, we just need to turn the samples from your hardware into something that you can play through your speakers! Let's go two blocks back up the chain, next.

Rational Resampler

The USRP Source is streaming at a rate of 'samp_rate', which we just set to 250e3. The Audio Sink is configured for a sample (consumption) rate of 'audio rate', which is 48e3. But, we have a problem: our source sample rate isn't an integer multiple of the audio rate: 250000 / 48000 = 5.208. To fix this problem, we will 'resample' the stream. Think of resampling here as impedance-matching. We have to convert from the rate of the input stream to the rate of the output stream.

The rational resampler block allows you to adjust the rate of a data stream. It does this by interpolating or decimating to achieve a desired sample rate. Open up the properties of this block, and let's tune the parameters.

Set the 'Type' to 'Complex-&gt;Complex', thus telling the block to use complex taps. Now we need to tune the actual rates. There is some intelligence built into this block - if we give it the different non-divisable sample rates, the code will calculate the GCD and make it all 'just work' for us. We know the incoming rate is 250 kSps, so let's enter that into 'Decimation'. But, this parameter must be an integer, and our 'samp_rate' is a float. Thus, we need to use the Python function 'int' to convert it.

Now we need to set the 'Interpolation' rate. There is a 'gotcha' here, because the WBFM block further down our chain will actually perform an additional decimation-by-four, so we need to actually tell this block to output a rate four times what we want. We know what we actually want is 'audio_rate', so use your 'audio_interp' variable to set the actual rate.

At this point, you should have made these settings:


 * Type: Complex-&gt;Complex (Complex Taps)
 * Interpolation: audio_rate * audio_interp
 * Decimation: int(samp_rate)

By leaving the 'Taps' parameter blank, we tell the block that we want it to compute the taps automatically. The 'Fractional BW' parameter affects the shape of the low-pass filter that is generated when no filter taps are supplied. By leaving this set to zero, we will just use a reasonable default.

Note: Depending on the accuracy of the clock references in your hardware, you may need to tune the Interpolation field, above. This is one of the many hardware impairments that you may need to deal with. When I first ran this flowgraph, I heard a lot of noise. I then tuned that Interpolation rate, and it cleaned up. My final numbers were:


 * Interpolation: int(1.05 * (audio_rate * audio_interp))
 * Decimation: int(samp_rate)

Now, let's configure our last block!

WBFM Receiver

At this point, configuring our last block is pretty simple. Open up the block's parameters. There are only two parameters to set: the rate of incoming samples, and the decimation rate. Well, we know both of these. We just configured the output rate of the rational resampler, and we know that rate divided-by-four equals our 'audio_rate'. Thus, you want:


 * Quadrature Rate: audio_rate * audo_interp
 * Audio Decimation: audio_interp

A Working Software Radio Broadcast FM Receiver
And that should be it! You should now no longer see any red in your flowgraph, and you should be able to build your flowgraph without errors. The final result should look like this:



You can also download this flowgraph, for reference.

Executing your flowgraph should bring up a GUI that looks something like what you see below. Changing the frequency and tuning to a local broadcast FM station should play the demodulated radio through your computer speakers.



Conclusion
Congratulations! You have successfully built real-word transmitter and receiver software radios using GNU Radio! While these applications were relatively simple, they do demonstrate the flexibility and power that GNU Radio provides when it comes to interfacing directly with hardware. It also demonstrated how easy it is to move an application from simulation to real-world operation.

Quiz
1. Given a radio source that streams data at 1 Msps to an audio device working at 32 kHz, what are the interpolation and decimation values required by the rational_resampler to resample the incoming data stream?

2. What if the audio device was at 44.1 kHz? Are there reasonable interpolation and decimation integers to satisfy this resampling? How could you use the PFB arbitrary resampler instead?

-