Radio Spectrum Analyzer at Maker Faire
Here's some details of my radio spectrum analyzer hack at Maker Faire. But first, a quick video of the hack in action:
Technical Details
This project is built around the HackRF, a software-defined radio transciever. I programmed it to sample 20MHz of radio spectrum from an antenna, do a frequency analysis on the data, and display the results on a Noritake vacuum fluorescent display. I added the tuning wheel mid-afternoon on Sunday, and it really improved the interactivity of the display. It felt really cool to spin the wheel around and watch the spectrum scroll back and forth. Too bad I didn't capture that in the video…
The HackRF's microcontroller is an ARM Cortex-M4F + Cortex-M0 "dual core" chip. Both are running at 204MHz. The M4F has hardware floating point, which drastically simplifies the signal processing code.
The ARM grabs 512 complex samples at a time from the radio analog-to-digital converter (ADC). It applies a window function that reduces artifacts from sampling arbitrary chunks of a radio signal. The windowed samples are converted to frequency domain data through a 512-point fast-Fourier transform (FFT). The frequency data that comes out of the FFT (frequency vs. a complex vector) is converted to real magnitudes. I take the logarithm of each magnitude to get values vs. frequency that resemble decibels. Then, I scale the log-magnitude data to fit nicely on the vacuum fluorescent display, which is 384 x 32 pixels. For each frequency in the scaled data, I render a bar into a frame buffer I maintain in RAM, using cute bit-shifting tricks. Then, I render marks at 1MHz intervals to provide a tick-mark scale on the display. In the left corner of the display, I render minimum and maximum sample buffer values for each of the two sampling channels – this is so I can tell if I need to turn up or down the gain on the receiver. I draw the current tuning frequency in MHz at the center of the display. Lastly, I scan out each of the pixels, one byte at a time, into the VFD's parallel 8-bit interface, using the display's "Graphic DMA" mode.
The optical quadrature encoder runs purely on interrupts. Whenever a positive- or negative-going edge is detected on either of the two optical sensors, an interrupt is generated. Based on the current and previous values of the optical sensors, the software (borrowed from PJRC's Encoder Library) decides if the wheel has moved, and if so, which way it has moved. Based on that decision, the tuning frequency is incremented or decremented.
There was a hairy bit of wiring between the HackRF and the VFD. I needed to interface the HackRF, which is a 3.3 Volt device, to the VFD, which communicates at 5 Volts. So I needed to translate several signals between those two voltages. I had an old circuit board from my Robotron-FPGA project that did exactly that, for a completely different purpose. But with enough wires and headers and disgusting rewiring, I made it work. But it wasn't pretty. I had to keep poking it and twisting it, as some signals were intermittent and sometimes needed my help.
The Faire Experience
The HackRF can tune from below 10MHz (where it's mostly amateur radio and AM radio stations), up through 6GHz. That includes almost every signal in common use. Until I added the tuning dial, I left the HackRF at about 840MHz, right in the middle of one of the cellular (mobile phone) bands. As you'd imagine, lots of cellular phones were in use at Maker Faire, so a lot of signals were jumping about on the display. People who really got the display would soon pull out their phones to make a call. Most of the time, their phones were operating in other cellular bands, but a few times we got lucky and could clearly see activity that came and went depending on whether they were in a call or not.
Late Saturday, a couple of hams (amateur radio operators) came by with their portable radio transcievers. I tuned the HackRF up to 440MHz and we could see peaks when they were transmitting. They were amused…
The first half of Sunday, I attached the optical encoder wheel and wired up the optical interrupters to the HackRF. The wheel was laser cut acrylic, cut before I left for Maker Faire. I cut a bunch of different designs, since I wasn't sure exactly how everything would fit together ahead of time. The wheel was mounted on a Lazy Susan apparatus I bought at a hardware store. I drilled the wheel assembly to my display whiteboard, and then started attaching the optical interruptors with hot glue. Unfortunately, I needed a couple of 470 Ohm through-hole resistors to control the current through the interruptors' LEDs, but I didn't bring any resistors with me. So I went begging at the Radio Shack "Learn to Solder" tent, and they were kind enough to bust open one of their $60 electronics kits to give me $0.07 worth of resistors. While I was gone, somebody had forcefully cranked the wheel and broken off several of the teeth. I should've seen that coming! Fortunately, I had lots of other options from my laser cutting binge. I settled on a sandwich, consisting of the tooth pattern cut in paper with clear thin plastic on either side.
Once I got the encoder software working, I added a frequency display in the middle of the VFD. People would walk up and give the wheel a spin, looking around for interesting signals. There's a lot of underutilized spectrum (at least where we were), so it wasn't too exciting outside the cellular and 2.4GHz bands. The environment was also quite electrically noisy, so with a fairly insensitive receiver, long-distance bands (e.g. aviation) didn't really show anything – those weaker signals were jammed by all the motors and Tesla coils nearby. I'm sure my antenna also wasn't the best…
Despite the last-minute additions and general hackiness of the project, I had a lot of fun building it and talking with people about it. Events like Maker Faire are great as a reminder that you're not the only person in the world who likes to nerd out on such obscure things!
Comments
I was one of the hams who came by on Saturday. Very impressive stuff. I signed up for the beta out - hopefully I'll have a chance to work with one.
Awesome and a useful tool for radio amateurs and other technicians and engineers! Perhaps you can write an article about it in Nuts & Volts and sell kits… Maybe you can quit your day job, too…. Good luck with your efforts!
This is great! I didn't know you could do that much DSP directly on the HackRF at its highest sample rate.
I am interested in building something like this for STEM outreach (science/tech/engineering/math) to attract highschool students to engineering careers. Is there any chance you'd be willing to open-source your software and firmware changes? I have a Jawbreaker from the HackRF beta test, and I think this would be a really cool first project to work on with it.
Sean: I'm constantly amazed at how well these little ARMs perform. I just did an LCD-based HackRF spectrum analyzer and am able to perform thousands of 512-point FFTs a second, without doing any optimization yet. I did make use of the Cortex-M0 to handle the LCD, which gave back a lot of cycles on the Cortex-M4F.
I absolutely intend to post all my work on the VFD and LCD devices to my repo, but right now, the code is a horrifying mess. I'm refactoring as I go, and will post soon on GitHub – schematics and designs for the adaptor PCBs, and alternate code for the HackRF. If you don't see something in the next week, feel free to prod me. :-)
Hi Jared, here I am, prodding. :cP I know you've probably been crazy busy. If you have a junk-filled tarball, I'd accept that too, and I could help clean up code, etc. for public release. Hopefully you can look up my e-mail address from this comment.
@Sean: Very diligent of you. :-) It'll be very soon now. I'll return to HackRF stuff as soon as I get some clear progress on my BeagleBone audio cape. I'm very close – I can talk to the audio codec via I2C, and have the Device Tree stuff pretty much figured out. Now I need an Ångström environment where I can build kernel modules that are missing in the standard distro.
Anyhow, I'll either succeed or need a break from that in the next couple of days. At which point, I'll do the minor HackRF codebase refactor I've been threatening, and can slide my PortaPack and VFD projects into that – probably in a GitHub repo of my own that depends on mossmann/hackrf.
Poking on this - sorry for being a nudge! Even unrefined, undocumented code would be a good start. Thanks =c)
@Sean: I made great progress a few weeks ago and have substantially refactored the base hackrf_usb project to be reusable in other projects like the VFD and PortaPack. I also have the PortaPack implementation building and running from SPIFI flash on a HackRF. The build system is a bit wonky (building Cortex-M0 code into a binary that's then injected into the Cortex-M4 .elf/.bin) but I think it's workable enough to commit publicly. It won't be in the [https://github.com/mossmann/hackrf/" rel="nofollow">main HackRF repo][] for a while still, as it needs testing by other people on other platforms. But you can watch my progress on [https://github.com/jboone/hackrf/tree/jboone_refactor_20130906" rel="nofollow">my branch][].
Awesome, thanks! I'll take a look. PortaPack looks really cool.
Wow really inspirational stuff,really enjoyed watching this presentation. Got to save up for and get one of those HackRF devices but living and teaching in Thailand so it's gonna take some time as the income is tight…just been introduced to software radio (currently experimenting with SDR sharp with my VLF rx). Having been a hardware designer and vintage communications receiver collector, until giving up to teach math here in Thailand, it makes me feel like I've been living in the dark ages! Thanks for posting your presentation on your wonderful radio spectrum analyser. Keep up the great work.