GNU Radio: Tools for Exploring the Radio Frequency Spectrum
GNU Radio provides a library of signal processing primitives and the glue to tie it all together. The programmer builds a radio by creating a graph (as in graph theory) where the nodes are signal processing primitives and the edges represent the data flow between them. The signal processing primitives are implemented in C++. Conceptually, primitives process infinite streams of data flowing from their input ports to their output ports. Primitives' attributes include the number of input and output ports they have as well as the type of data that flows through each. The most frequently used types are short, float and complex.
Some primitives have only output ports or input ports. These serve as data sources and sinks in the graph. There are sources that read from a file or ADC, and sinks that write to a file, digital-to-analog converter (DAC) or graphical display. About 100 primitives come with GNU Radio. Writing new primitives is not difficult.
Graphs can be constructed and run in C++, but the easy way to glue everything together is with Python. Listing 1 is the “Hello World” of GNU Radio. It generates two sine waves and outputs them to the sound card, one on the left channel, one on the right.
Listing 1. Hello World (Dial Tone Output)
#!/usr/bin/env python from GnuRadio import * def build_graph (): sampling_freq = 32000 ampl = 8192 fg = gr_FlowGraph () src0 = GrSigSourceS ( sampling_freq, GR_SIN_WAVE, 350, ampl) src1 = GrSigSourceS ( sampling_freq, GR_SIN_WAVE, 440, ampl) sink = GrAudioSinkS () fg.connect (src0, sink) fg.connect (src1, sink) return fg if __name__ == '__main__': fg = build_graph () fg.start () # fork thread(s) and return raw_input ('Press Enter to quit: ') fg.stop ()
We start by creating a flow graph to hold the primitives and connections between them. The two sine waves are generated by the GrSigSourceS calls. The S suffix indicates that the source produces shorts. One sine wave is at 350Hz, and the other is at 440Hz. Together, they sound like the US dial tone.
GrAudioSinkS is a sink that writes its input to the sound card. It takes one or two streams of shorts as its input. We connect the three primitives together using the connect method of the flow graph. Once the graph is built, we start it. Calling start forks one or more threads to run the computation described by the graph and returns control immediately to the caller. In this case, we simply wait for any keystroke.
Listing 2 shows a somewhat simplified but complete broadcast FM receiver. It includes control of the RF front end and all required signal processing. This example uses an RF front end built from a cable modem tuner and a 20M sample/sec analog-to-digital converter.
Listing 2. Simple Broadcast FM Receiver
#!/usr/bin/env python # simple broadcast FM receiver from GnuRadio import * # # return a gr_FlowGraph # def build_graph (IF_freq): input_rate = 20e6 CFIR_decimate = 125 RFIR_decimate = 5 fm_demod_gain = 2200 quad_rate = input_rate / CFIR_decimate audio_rate = quad_rate / RFIR_decimate volume = 1.0 src = GrHighSpeedADCSourceS (input_rate) # compute FIR filter taps for channel selection channel_coeffs = \ gr_firdes.low_pass ( 1.0, # gain input_rate, # sampling rate 250e3, # low pass cutoff freq 8*100e3, # width of trans. band gr_firdes.WIN_HAMMING) # input: short; output: complex chan_filter = \ GrFreqXlatingFIRfilterSCF (CFIR_decimate, channel_coeffs, IF_freq) # input: complex; output: float fm_demod = \ GrQuadratureDemodCF (volume * fm_demod_gain) # compute FIR filter taps for audio filter width_of_transition_band = audio_rate / 32 audio_coeffs = \ gr_firdes.low_pass ( 1.0, # gain quad_rate, # sampling rate audio_rate/2 - width_of_transition_band, width_of_transition_band, gr_firdes.WIN_HAMMING) # input: float; output: short audio_filter = \ GrFIRfilterFSF (RFIR_decimate, audio_coeffs) final_sink = GrAudioSinkS () fg = gr_FlowGraph () fg.connect (src, chan_filter) fg.connect (chan_filter, fm_demod) fg.connect (fm_demod, audio_filter) fg.connect (audio_filter, final_sink) return fg if __name__ == '__main__': # connect to RF front end rf_front_end = microtune_eval_board () if not rf_front_end.board_present_p (): raise IOError, 'RF front end not found' # set gain and radio station frequency rf_front_end.set_AGC (300) rf_front_end.set_RF_freq (100.1e6) IF_freq = rf_front_end.get_output_freq () fg = build_graph (IF_freq) fg.start () # fork thread(s) and return raw_input ('Press Enter to quit: ') fg.stop ()
Like the Hello World example, we build a graph, connect the primitives together and start it. In this case, our source is the high-speed ADC, GrHighSpeedADC. We follow it with GrFreqXlatingFIRfilterSCF, a finite impulse response (FIR) filter that selects the FM station we're looking for and translates it to baseband (0Hz, DC). With the 20M sample/sec converter and cable modem tuner, we're really grabbing something in the neighborhood of a 6MHz chunk of the spectrum. This single chunk may contain ten or more FM stations, and GrFreqXlatingFIRfilterSCF allows us to select the one we want. In this case, we select the one at the exact center of the IF of the RF front end (5.75MHz). The output of GrFreqXlatingFIRfilterSCF is a stream of complex samples at 160,000 samples/second. We feed the complex baseband signal into GrQuadratureDemodCF, the block that does the actual FM demodulation. GrQuadratureDemodCF works by subtracting the angle of each adjacent complex sample, effectively differentiating the frequency. The output of GrQuadratureDemodCF contains the left-plus-right FM mono audio signal, the stereo pilot tone at 19kHz, the left-minus-right stereo information centered at 38kHz and any other sub-carriers above that. For this simplified receiver, we finish off by low pass filtering and decimating the stream, keeping only the left-plus-right audio information, and send that to the sound card at 32,000 samples/sec. See the GNU Radio Wiki for discussions and tutorials on signal processing.
Special Reports: DevOps
Have projects in development that need help? Have a great development operation in place that can ALWAYS be better? Regardless of where you are in your DevOps process, Linux Journal can help!
With deep focus on Collaborative Development, Continuous Testing and Release & Deployment, we offer here the DEFINITIVE DevOps for Dummies, a mobile Application Development Primer, advice & help from the experts, plus a host of other books, videos, podcasts and more. All free with a quick, one-time registration. Start browsing now...
- Non-Linux FOSS: Code Your Way To Victory!
- Vagrant Simplified
- Dealing with Boundary Issues
- System Status as SMS Text Messages
- Libreboot on an X60, Part I: the Setup
- Disney's Linux Light Bulbs (Not a "Luxo Jr." Reboot)
- Bluetooth Hacks
- Linux and the Internet of Things
- New Products
- October 2015 Issue of Linux Journal: Raspberry Pi