Skip to content
HN On Hacker News ↗

Simulating Airband AM Radios

▲ 64 points • 5 comments • by shellpipe • 2w ago • HN discussion ↗

Pangram verdict · v3.3

We believe that this entire text is human-written.

0 %

AI likelihood · overall

Human
100% human-written 0% AI-generated
SEGMENTS · HUMAN 1 of 1
SEGMENTS · AI 0 of 1
WORD COUNT 1,718
PEAK AI % 0% · §1
Analyzed
Sep 29
backend: pangram/v3.3
Segments scanned
1 windows
avg 1718 words each
Distribution
100 / 0%
human / AI fraction
Verdict
Human
Pangram v3.3

Article text · 1,718 words · 1 segments analyzed

Human AI-generated
§1 Human · 0%

Warning: Contains many annoying sounds. In a slight departure from my usual code monkey content, let’s talk about airplanes! And radios! For the last several years, I’ve spent most Saturdays playing (and occasionally working on) BMS, a modern1 combat flight sim where you and your friends blow stuff up in virtual F-16s. Any co-op game with over 30 people is a blast, but air combat is especially fun because it’s such a team sport. Flights swirl in vicious dogfights and play deadly games of whack-a-mole with enemy air defenses, all just to give a few jets a couple of seconds over the target to drop their bombs. None of it is scripted, and everyone has to work together to come back alive. You might imagine this involves a lot of talking, and so BMS ships with a voice chat app called IVC. To add to the immersion, it simulates the radios in your virtual cockpit. You don’t join a chat room, you tune to a radio frequency. Your signal fades as your jet gets further from whoever you’re talking to, or if you’re both flying low, you can be blocked by terrain entirely. Surprisingly, airplane radios—even many military ones—are still simple AM sets that operate in the VHF and UHF bands. One of the reasons that’s persisted through decades of technological advances is that AM radio doesn’t have a “capture effect”. When two people transmit on the same frequency, you can still (sorta) hear both parties, unlike FM where the louder signal mutes or “captures” the quieter one. Here’s what it sounds like when fighter pilots talk over each other, captured during a Red Flag training exercise in Nevada: So imagine my… curiosity when IVC just makes this sound whenever people step on each other: That bugs me more than it should. So when a buddy set out to build an IVC replacement with better UX and modern audio codecs, I wanted to contribute some realistic AM radio dynamics. Like these: How? Let’s dive in. Radio 101: path loss, decibels, SNR So you want to talk to someone over the radio. Let’s set aside the black magic of antenna design—take it as a given that if you cut the right length of wire and wiggle the electrons in it, some of them will magically shear off into space as electromagnetic waves. Even if those waves don’t run into anything,2 they get weaker as a square of radius rr from the transmitting antenna, simply because they spread out as they travel. Waves emanating from some source S spread out as they travel. (Wikipedia) This is true of all waves—sound, radio, light.3 And because different distances from the source produce such wildly different power levels, our senses need to work on logarithmic scales. They’re actually pretty astonishing—the roar of a jet engine is a million times louder than the quietest whisper, and you can hear both. A room can be a million times darker than a sunny day, yet you can see in both. The radio frequency (RF) world is no different. Because it would be annoying to work with such a wide range of numbers, we often describe signal strength in a logarithmic scale called decibels, abbreviated as dB. One of the first things we’d like to describe in decibels is the signal-to-noise ratio, or SNR. SNR: 40 dB Volume Decibels are always a unitless ratio between two values, where 0 dB means "equal", and every ±3 dB roughly doubles or halves the power. So when discussing SNR, 0 dB means the signal is equal in power to the noise. Some noise is man-made, some comes from atmospheric events like thunderstorms, and some comes from outer space. More noise comes from the imperfections in your radio’s electrical components. And even if you could somehow remove all of those noises, you’d still hear thermal energy vibrating the electrons in your receiver. (We call this phenomenon thermal noise and it’s our theoretical minimum.) But no matter where it comes from, noise is always there! We can never get rid of it; we only hope the received signal is louder than the noise by the time it reaches us. Amplitude modulation So what waves should you broadcast? The frequencies that make up your voice (and everything else you hear) are between 20 and 20,000 cycles per second, or Hertz. But the lower the frequency, the longer the wavelength, and good antennas are at least a quarter of the wavelength they receive. To pick up a 10 kHz signal, we’d need over 7 kilometers of wire.4 Instead, let’s shift our voice onto some higher carrier frequency that we can actually transmit. A simple approach is to modulate the carrier wave’s amplitude by that of our voice. We might call this amplitude modulation, or AM for short. modulation index: 0.90 Our voice is modulated onto a higher-frequency carrier by multiplying it by some modulation index k. The frequency of the carrier wave is completely unchanged. Notice how the outlined shape of the resulting AM signal—its envelope—is a mirrored copy of our voice. Hmm… What’s in a radio? Glossing over how you tune your radio to different frequencies (the answer might shock you!), we have a few other problems to solve if we want to hear an AM signal: We need to filter out all the frequencies we don’t care about, passing only the band of frequencies we actually want. (Let’s call that the passband.) The signal is probably very weak by the time we get it (see above), so we need to amplify it. Lastly, we use an envelope detector to extract, or demodulate, our voice back out of the AM signal. Filtering How do we let some frequencies through and block others out? Let’s talk circuits 101. In the analog world, we deal in: Current (I), which represents electrons flowing through our circuit Voltage (V), which represents the electric potential energy between two parts of a circuit, and Resistance (R), which represents the reluctance of some part of a circuit to let current flow through it. They can behave in unintuitive ways, but they are always proportional to each other according to Ohm’s law: V=IRV = IR With this knowledge, we can make one of the simplest useful analog circuits: a voltage divider. The squiggly bits represent resistors, and the inverted dashed triangle at the bottom represents ground, or 0 volts, to which we compare all our other voltages. Because resistors oppose the flow of current, our output voltage will be some fraction of the input voltage, based on the ratio of the two resistors. Drag the slider to change that ratio, and observe how: Vout=Vin⋅R2R1+R2V_{out} = V_{in} \cdot \frac{R_2}{R_1 + R_2} What if we swap out one of the resistors for something more interesting? This symbol represents a capacitor, a part that stores electric charge between two plates, and can discharge it later. If we swap R2R_2 out for one, we get the behavior: Vout(t)=Vin⋅(1−e−t/RC)+V0⋅e−t/RCV_{out}(t) = V_{in} \cdot (1 − e^{−t/RC}) + V_0 \cdot e^{−t/RC} As time tt goes by, VoutV_{out} asymptotically approaches VinV_{in} at a rate determined by the resistance RR multiplied by the capacitance CC. Because of this, we call RCRC the time constant, or τ\tau. A little more math shows us that every 3τ3 \tau, the voltage advances 95% of the way from V0V_0 to VinV_{in}, where V0V_0 is just whatever our starting voltage was. This makes capacitors useful for all sorts of applications, but the thing we care about today is how one behaves when given an alternating input (like a wave from an antenna!) instead of a constant one. Because the capacitor is constantly charging and discharging, its resistance5 decreases as frequency increases. This makes our RC circuit a low-pass filter that lets low frequencies through and shrinks—or attenuates—high frequencies. And if we just swap the resistor and capacitor, we have a high-pass filter. Instead of the capacitor shunting high frequencies to ground, it’s letting high frequencies through from VinV_{in}, and so low frequencies are attenuated. Filter design can get amazingly complex—there are whole textbooks on this stuff—but the basics don’t have to be. We’ll find that it’s surprisingly easy to do this in the digital world as well. Automatic gain control We also need to amplify our signal. But how much? Even though we’ll receive signals with wildly different strengths, it would be great if our radio had a consistent volume level when someone is talking. We want to control the amplification, or gain, automatically. How do we do that? More filters! If we take the magnitude of our signal, then low-pass it, we end up with a curve that gives a rough measure of its volume. Attack Decay We have a slight conundrum, though—we want the filter to respond quickly when someone starts talking, but to taper off slowly, so that the measured volume doesn’t drop whenever they pause to take a breath. To do this we build a filter that responds differently when the signal rises (call this attack) and when the signal falls (call this decay). I also added a period of silence to our AM transmission—after all, pilots aren’t talking on the radio 100% of the time. Try moving the sliders around until the filtered magnitude: Rises to the peak volume quickly when a transmission starts Stays flat-ish for the entire transmission, but Decays back to 0 quickly-ish when the transmission stops And lo, you’ve measured the volume of our signal! If we divide whatever comes in by this value, the user should always hear a similar volume. AM demodulation Notice how if you drag the sliders around some more, we get something that looks suspiciously like the envelope. In fact, this sort of filter is sometimes called an “envelope detector”. To demodulate our AM signal back into the transmitted voice, we just need to low-pass the signal with one of them. Squelch Scroll back up to our SNR demo, and drag the slider all the way to the left until you hear only static. Isn’t that unpleasant? Nobody likes hearing static for extended periods of time, so radios quickly grew a feature called squelch, which mutes your speakers whenever the signal falls below a certain volume. Good thing we just came up with a way to measure it! SNR: 40 dB