[This post is originally from my blog. I'm bringing this spectrum analyzer to Darren Kitchen's Hak5 event at OMSI this Saturday (2013/05/25).]
Here's some details of my radio spectrum analyzer hack at Maker Faire. But first, a quick video of the hack in action:
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 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!