Friday, February 2, 2018

Building a Software FM Receiver with an RTL-SDR and GNU Radio

A Short Practical Introduction to Software Defined Radio



Back in the 2012, the guys from the Osmocom team discovered how to hack a $20 DVB-TV dongle into a wide band radio receiver. Running on a Realtek RTL2832U chip and commonly called an RTL-SDR, this dongle is currently the cheapest Software Defined Radio receiver in the market. For a fraction of the cost of dedicated radio hardware or high-end SDR receivers, one can monitor law enforcement/emergency/air traffic communications, track aircraft and ship positions, download satellite data, analyze mobile phone traffic, reverse engineer data signals and explore other interesting areas of analog and digital wireless communications. 


RTL-SDR Dongle (Photo from rtl-sdr.com)

RTL-SDR is supported by a huge online community with hundreds of free software and tutorials for various radio applications. In this post, we take a few steps back and explore the fundamentals of Software Defined Radio and how it works by building an FM broadcast receiver with an RTL-SDR dongle and GNURadio.

GNU Radio is an open source toolkit for developing signal-processing and software-based radio applications. It works with various radio transmitter and receiver hardware, and also has a simulation mode. Widely used for wireless communication research and applications development, it has libraries for C++ and Python, as well as a visual programming environment, GNU radio companion, which we will use for the following examples.

Radio Theory in a Nutshell

Radio signals are electromagnetic waves that propagate at a frequency between 3 kilohertz to 300 gigahertz. They are naturally generated by atmospheric and astronomical conditions. Artificially generated radio signals are used for voice, video and data communications over a wireless medium.

Any radio system has a transmitter that sends a signal, and a receiver that receives it.



Radio Transmitters

A radio transmitter has an oscillator that rapidly changes the current in a circuit between two peak voltage points generating a continuous wave. This current is connected to an antenna and transmitted into the air as an electromagnetic wave called a carrier signal. A signal source, typically either sound, video or data, is encoded into the carrier signal through a modulator circuit.




How often the current changes over time is referred as the frequency and amplitude refers to the peak voltage values of a wave. A carrier signal oscillates at a consistent frequency and amplitude mathematically described as a sine wave.



A signal source also demonstrates wave behavior. However, unlike the carrier wave, it has a variable frequency and amplitude. Below is an actual sound wave as seen in Audacity.




Modulation is the process of encoding a signal source into a carrier signal using either of the two following methods:

  • Amplitude Modulation: The source signal is encoded by varying the amplitude of the carrier signal.
  • Frequency Modulation: The source signal is encoded by varying the frequency of the carrier signal.





The Radio Frequency Spectrum

We can describe a radio signal as a carrier signal encoded with a source signal, and transmitted via an antenna as an electromagnetic wave with a frequency between 3 khz and 300 ghz. Radio signals exhibit different behaviors depending on its frequency (e.g. lower frequencies propagate farther and higher frequencies can penetrate physical obstacles). A specific range of frequencies are chosen for a particular application based on these behaviors.  Below is a quick table of various applications in the radio frequency spectrum.




Radio Receivers

It is also useful to view frequency as a virtual location in the radio spectrum that separates one signal from another. Transmitters propagate at different frequencies from each other.  A common example are the various broadcast radio station frequencies available on on a car stereo.

A receiver antenna picks up several radio signals from broad range of frequencies depending on its design. The tuner circuit in a radio receiver is essentially a filter that amplifies a specific frequency from the antenna and ignores all the others. After a signal has been selected, a demodulation circuit separates the source from the carrier signal. The source signal is processed depending on the type: audio signals are amplified and sent to a speaker, videos are decoded and displayed, and data is processed into information.



Radio devices can either be unidirectional and can only transmit (e.g. broadcast radio or TV transmitters), or receive (e.g car radio, GPS receiver) signals, or bidirectional and can both transmit and receive signals (e.g. two-way radio, Wifi interface)

In traditional hardware-based radio systems, each step in the above processed is handled by a dedicated set of hardware components. Everything is hard-wired and designed to operate for a specific application. Each radio device can only operate within a limited range of the radio spectrum and process a specific type of source signal.  A car radio for example can only receive the broadcast FM band between 88 - 108 megahertz and process audio.

Software-Defined Radio and the RTL-SDR Dongle

The heart of Software Defined Radio (SDR) devices are Analog-to-Digital Converters that convert raw radio signals into digital data and tuner chips that operate on a wider range of frequencies.  Although it's necessary to use specific antennas and hardware-based amplifiers and filters to work with some signal types, most of the heavy-lifting is done using software, either by programming the hardware chips, or processing the resulting data on a computer. This allows a single device for various application making it a cheaper and more flexible alternative to pure hardware-based systems.

The RTL-SDR dongle was originally designed to convert analog radio signals to digital data  for Digital TV and FM Radio broadcast.  With a modified hardware driver, it can effectively receive signals between 25 megahertz to 1.6 gigahertz.  It has an 8-Bit sampling rate, which is relatively low compared to more expensive SDR hardware, but decent enough to be useful for a variety of applications.
  
Building A Software Radio

This post does not cover installation and configuration of the RTL-SDR driver and GNU Radio for your respective platform.  There are several helpful tutorials on the web so this assumes that the requisite driver have been installed and GNU Radio with GrOsmoSDR is up and running. To work on the examples in this post, I used a Noolec Nano3 dongle with GNU Radio Companion 3.7.9. Now that we have that out of the way, let's open GNU Radio companion and build our receiver.

Applications in GNU Radio Companion are built using blocks in a flowgraph. Each block represents a signal processing component. Blocks are chosen from a list on the right side of the interface and drag into the workspace and connected together.

Clicking on File->New brings a menu of options for the type of GUI to use for a new flowgraph. Pick QT GUI for our radio application.




By default, each flowgraph has an Options block where global parameters are set. Notice that our new flowgraph has an Options block with ID set as top_block and Generate Options set to QT GUI that we selected earlier. Also notice that there is a Variable block with a default samp_rate set to 32000. As the name suggests, Variable blocks define variables that are used to set parameters of other blocks in the flowgraph.

There are three general types of blocks:
  • Source blocks inputs data into the application. (e.g. File Source blocks which inputs data from a file)
  • Sink blocks outputs data or a signal from the the application. (e.g. Audio Sink block writes an audio output as a .wav file)
  • Processing blocks manipulate data between source and sink blocks
At a minimum, it requires one source block and one sink block for a flowgraph to run. With these two block types we can already start building our radio receiver by connecting to the RTL-SDR dongle, capturing a range of radio signals and converting it to digital data.

Capturing Raw Radio Signals

Let's begin by searching for the RTL-SDR Source block and dragging it to the workspace.




The RTL-SDR Source block connects our flowgraph to the RTL-SDR dongle, tunes it to a configured frequency, captures a portion of the radio spectrum and brings it to the flowgraph as digital data.  The captured portion is within a range of frequencies defined by a configured sample rate, The range of frequencies, or frequency band,  is centered around the frequency where the dongle is tuned, and we refer to this as the center frequency.  In this case we are capturing a portion of the spectrum with a 1 Mhz bandwidth centered at the frequency of 96 Mhz. 

  • First we change the value of the samp_rate variable by double-clicking on the Variable block. Python expressions are valid parameter values in GNU Radio, so we enter 1e6 as the samp_rate
  • Now we can configure the RTL-SDR Source block, setting the Sample Rate to samp_rate and the Frequency to 96e3. With these parameter values we are now capturing a portion of the radio spectrum between 95.5 Mhz to 96.5 Mhz with 96 Mhz as the center frequency.
  • We can examine our sampled signal by adding a QT GUI Sink block which gives us several instruments for visualizing signals. 
To run the flowgraph click on the play button on top of the interface:


The Frequency Display tab shows the strength of various signals (and noise) within the captured frequency band. Radio signals would typically have a peak strength on the display.



Noticed that the frequency range is centered at 0 and not on the 95 mhz center frequency. Contrary to what we are likely to expect, raw digital samples of radio signals don't contain frequency data. The digital sample of any given frequency band is represented as complex numbers with 0 Mhz as the center frequency. (There is some mathematics behind this that I am completely not qualified to explain). To visualize the frequencies, we need to set a relative reference to the center frequency of the captured frequency band.
  • Create a center_frequency Variable block since we need this value in several instances across the flowgraph. 
  • Edit the RTL-SDR Source and change the Frequency parameter to center_frequency
  • Set the reference point of the QT GUI block to reflect the captured frequency band by changing the center frequency parameter to center_frequency.
When we run the flowgraph, we get a visualization that reflects the actual frequency of the captured band. Keep in mind that the center frequency of visualization instruments should be consistent with the sampled frequency band from the hardware source. Unless you just want to observe the behavior of raw signals without frequency reference points. 

Tuning to a Signal

We are now capturing a portion of the radio spectrum as digital data. Since this is part of the FM Broadcast band, there should be at least one station transmitting within these range of frequencies. These signals are usually strong and appear as peaks in our Frequency Display.

The next step is to pick, or tune to, a specific broadcast station. This is done by shifting the frequency band and placing the station signal at the center and eliminating all the other signals.

  • Create a QT GUI Entry block for the frequency of target signal. This block creates a text entry form for setting variable values. Enter target_frequency as ID. To test the flowgraph, pick a local radio station broadcasting between 95.5 to 96.5 mhz and enter the frequency as the default value. In this case it's 96.3 Mhz

  • Create a Multiply block and connect the input to the output of the RTL-SDR Source. This block takes the sampled frequency band as input and combines it with a sine wave signal. The output is a frequency band with a center frequency shifted based on the frequency of the sine wave. 
  • Create a Signal Source block to generate the sine wave signal. Edit the block, choose Sine as the Waveform, and enter center_frequency  - target_frequency as the Frequency parameter. 
  • The difference between the center frequency and the target frequency is equal to the sine wave frequency required to shift the target frequency to the center as shown in the example below. 


  • Connect the Signal Source output to the Multiply block input.


  • Add another QT GUI Sink block. To view the shifted frequency band change the Frequency parameter to target_frequency.



  • Connect the GUI Sink block it to the output of the Multiply block. Run the flowgraph and observe how the frequency range has shifted.
Filtering

We isolate the target signal at the center by eliminating the rest of the frequency range with a Low Pass Filter block.  As the name suggests, this block filters out any signal above a cutoff frequency starting from the center commonly called the channel width. The filters transition width is the point along the channel width when filtering starts to 'roll-off'.

  • Create the following Variable blocks for the filter parameters: channel_width with a default value of 150e3, transition_width with a default value of 10e3 and a working_samp_rate of  400e3.
  • At this point in the flowgraph we are already dealing with our target signal which is only a segment of our captured frequency band. It's no longer necessary to have a high sample rate to work with this data, so we reduce it to lower rate defined in working_samp_rate. The process of reducing the sample rate is called decimation.
  • We create a Low Pass Filter block and set the Cutoff Frequency to channel_width, the Transition Width to transition_width and Decimation to int(samp_rate/working_sample_rate).
  • We connect the output of the Multiply block to the Low Pass Filter block to isolate the target frequency signal and reduce the sample rate. 
Connect another QT GUI Sink to the output of the Low Pass Filter. Run the flowgraph and scroll to the top Frequency Display to see the filtered signal.


Demodulation

The last step is to extract the audio source signal and send it to the computer's audio hardware. 
  • Create a WBFM Reciever block and connect the input to the output of the Low Pass Filter. This block separates the source signal from the carrier signal. 
  • Set the Quadrature Rate, which is the sample rate of the input signal, to working_samp_rate
We finally have an audio signal that we can send to the audio hardware. However, a typical sound card runs at a lower sample rate than the 400k output of the WBFM Reciever. The Rational Resampler block changes the sample rate of a signal. It has an Interpolation parameter that is divided with a Decimation parameter. The result of which is the factor by which a sample rate is changed. An Interpolation value that's lower than the Decimation value will result in a slower sample rate (e.g. decimation of 2 and interpolation of 1 will be 1/2x slower). An Interpolation value that's higher than the Decimation value will result in a slower sample rate (e.g. decimation of 1 and interpolation of 2 will be 2x slower).
  • Create a Rational Resampler block and set the type to Float. The WBFM Reciever outputs floating point values to represent the audio signal. Set the Interpolation to 48k and the Decimation to int(working_sample_rate) changing the data type of the variable to integer.
  • Connect the output of the WBFM Reciever to the input of the Rational Resampler.
  • Create an Audio Sink block and set the Sample Rate to 48k.
Our final flowgraph should look like this:


Run the flowgraph. You should hear a broadcast radio station if you are tuned to the correct frequency. To change the frequency, just enter a value in the entry form above the Frequency Display. There is no audio processing post-modulation so the volume is rather low. You might need to wear earphones to hear the audio.

We've hardly scratched the tip of the Software Defined Radio iceberg, but this exercise has gone through the basic steps common to all radio reciever applications. SDR provides an inexpensive platform for learning signal processing, radio theory and prototyping both hardware and software based radio systems. Hopefully, this will lead you to further explore the potential of this technology.


Again, comments, questions and corrections are always welcome.



No comments:

Post a Comment