sci

sonar

one emitter, one receiver, in air

Your phone already has an air sonar's parts: a speaker to emit and a microphone to listen. This page sends a short chirp, records what comes back, and runs a matched filter over the recording. Every echo turns into a distance. Point the phone at a wall, tap Start, and move your hand in front of it.

Simulated data below. Tap Start to use your microphone.
strongest echo—
system latency
—
direct-path SNR
—
sample rate
—
band
—
speed of sound
—
range resolution
—
absorption, round trip
—
pings / s · max range
—
a-scan · echo level vs range
echogram · range ↓, time →

How a ping becomes a distance

The pulse. A click carries little energy, and a long tone gives no timing. A chirp does both: a 5 ms sweep from f₀ to f₁. Correlating the recording against the chirp, the matched filter of Turin 1960, compresses each returning copy into a peak about 1/B wide, where B is the sweep's bandwidth. Range resolution is therefore set by bandwidth alone, Δr = c / 2B, independent of the pulse's length. The pulse is shaped by a Hann window. With a hard-edged window, the near-ultrasonic chirp's own sidelobes sit at −21 dB 20 cm out and bury real echoes. With Hann they are at −43 dB. The selftest measures both.

Time zero is the direct path. Between the code that schedules a ping and the air, the phone adds an output buffer, a speaker, a microphone and an input buffer. Over Bluetooth it adds a radio link as well. None of that delay is known in advance. The sound that travels straight from speaker to microphone arrives first and loudest, so the page finds that peak in every ping and times echoes from it. The unknown delays cancel. They still appear above as system latency, measured rather than assumed.

Delay to range. With the speaker and microphone s apart and a target at range d, the echo travels about 2d and the direct path travels s. An echo arriving τ after the direct path is therefore at d = (cτ + s)/2. Set the spacing slider to your phone. On most phones the bottom speaker and bottom microphone are 1–3 cm apart, and the earpiece and top microphone sit at the other end.

Baseline. A room returns echoes from every surface, all the time. Hold the phone still and tap Set baseline. For two seconds the page learns the room. After that it keeps a trailing average of it, so slow changes such as a door left open or the phone settling in your hand are absorbed after the room memory time. What remains is whatever has changed: a hand, a person walking past. Whatever stands out is learned ten times more slowly than the rest, so a hand that leaves does not leave a ghost behind. A hand held still long enough still becomes part of the room.

Two choices make the subtraction work. First, the room is subtracted before the envelope is taken, from the raw correlation including its phase. The speaker and microphone share one clock, so a wall's echo repeats sample for sample and cancels almost completely. Envelopes keep the noise's magnitude and lose its sign, so subtracting them never quite cancels. Second, what is left is averaged as a magnitude over the last few pings (averaging), not as a raw signal. A moving hand shifts by a wavelength, 1.8 cm at 19 kHz, within a few pings, and averaging the raw signal would cancel the hand along with the noise. The selftest measures all three ways of combining pings on a moving target, and the page uses the one that wins.

Phone-based acoustic sensing works the same way, from ranging between two phones (Peng et al. 2007) to tracking a finger to the centimetre (Nandakumar et al. 2016).

The medium, computed

Air sets the limits, and both are closed-form functions of the conditions you set above. The speed of sound is the ideal-gas value c = 331.3·√(1 + T/273.15) m/s. Humidity and CO₂ move it by a few tenths of a percent (Cramer 1993), which is less than one sample of range here. Absorption follows ISO 9613-1, the standard built on Bass et al. 1995. It rises roughly as f² until the oxygen and nitrogen relaxations take over, and it is why a bat's ultrasound reaches metres while a whale's call crosses an ocean.

bandΔr = c/2Babsorption at centreround trip to max range

With AirPods in

The page cannot reach your AirPods directly. It works with whatever microphone and speaker your phone gives the browser. With AirPods connected, that usually means both are the AirPods. Three things change, and the readouts above show each one.

The sample rate drops. Opening a Bluetooth headset's microphone switches the link from its music profile to the hands-free profile, which carries speech at a much lower sample rate. The page reads the rate the browser actually delivers. When near-ultrasound no longer fits under the Nyquist limit, auto falls back to the audible 1.5–7 kHz band. You will hear the pings.

The emitter is in your ear. An AirPod's driver faces down your ear canal, sealed by the tip, and very little of its sound reaches the room. The microphone sits on the stem and is tuned to your voice, with processing in the earbud that the browser cannot switch off. Expect a weak direct path, a low SNR readout, and echoes that come mostly from your own head. The latency readout will show the radio link's delay. It does not hurt the measurement, because every echo is timed from the direct path.

The better split. Use the phone speaker to emit and an AirPod to receive. Your ear is then a second station, and the geometry becomes bistatic. Each delay now describes an ellipse with the phone and your ear at its foci, not a circle, and s is the phone-to-ear distance. iPhone Safari moves input and output to the AirPods together. A browser that offers a speaker picker above (Chrome on Android or desktop) can split them. For a plain room sonar, put the AirPods in their case and use the phone alone.

More phones. Two phones can measure the distance between each other without sharing a clock. Open the sonar lobby on two or more phones and join the same room.
Where this page stops. One emitter and one receiver measure range, not bearing. Direction needs a second receiver and the time difference between the two. Phones have two or three microphones, but browsers mix them to one channel. The channels note in the status line reports what your browser delivered. Bearing is the next part, written once a browser can deliver two channels.

Sources

  1. G. L. Turin, "An introduction to matched filters," IRE Trans. Inf. Theory 6(3), 311–329 (1960). doi:10.1109/TIT.1960.1057571
  2. O. Cramer, "The variation of the specific heat ratio and the speed of sound in air with temperature, pressure, humidity, and CO₂ concentration," J. Acoust. Soc. Am. 93(5), 2510 (1993). doi:10.1121/1.405827
  3. H. E. Bass, L. C. Sutherland, A. J. Zuckerwar, D. T. Blackstock, D. M. Hester, "Atmospheric absorption of sound: Further developments," J. Acoust. Soc. Am. 97(1), 680 (1995). doi:10.1121/1.412989. Standardised as ISO 9613-1:1993.
  4. C. Peng, G. Shen, Y. Zhang, Y. Li, K. Tan, "BeepBeep: a high accuracy acoustic ranging system using COTS mobile devices," SenSys (2007). doi:10.1145/1322263.1322265
  5. R. Nandakumar, V. Iyer, D. Tan, S. Gollakota, "FingerIO: using active sonar for fine-grained finger tracking," CHI (2016). doi:10.1145/2858036.2858580

signal path: sonar.js, no dependencies, known-answer tested against synthetic echoes · sci · mino.mobi