./jerome{m}jack in

~/Labs/Sonar Lab.md · lab · 4 min · updated

Sonar Lab

2026 · Rust · WebAssembly · WebGL2 · Web Audio · How this site works

A laptop already carries a stereo transmitter and a coherent receiver sitting 25–35 cm apart on the same clock. The Sonar Lab uses them for active acoustic sensing — no camera, no external hardware, and nothing uploaded. Everything runs locally inside a 79 KB zero-dependency Rust WebAssembly DSP engine paired with a WebGL2 3D architectural viewer.

It opens live on a built-in 3D image-source room acoustics simulator driving the exact same Rust matched-filter pipelines as real hardware, so you can drag a virtual hand across the ultrasonic radar scope or inspect a 3D wireframe room scan immediately — then press arm mic to switch to your real speakers and microphone.

sonar · liveSwitch between Silent Radar (Mode 1: drag the virtual hand or arm your mic and wave in front of the laptop) and Room Echolocation (Mode 2: fire 0° and +90° broadband pings, orbit the 3D wireframe room, and replay the captured acoustic wavefront at 1/250× the speed of sound).open ↗

Mode 1: Silent ultrasonic 2D motion radar

Above ~17.5 kHz, most laptop speakers and microphones still pass usable energy while human adults hear almost nothing. Rather than blasting a loud click, the radar emits continuous Hann-windowed linear frequency-modulated (LFM) chirps and compresses every echo back into a sharp pulse via O(N \log N) analytic matched filtering:

  • Time-division stereo chirps. The Left speaker transmits an up-chirp (17.5 \rightarrow 21.3\text{ kHz}), waits for echoes to clear, and then the Right speaker transmits a down-chirp (21.3 \rightarrow 17.5\text{ kHz}) in alternating 34 ms slots. Separating the two speakers in both time and sweep slope gives complete L/R isolation — and lets the receiver tell which speaker it heard without knowing your OS audio latency.
  • Zero-config sub-sample sync. The loudest signal at the microphone is always the direct speaker-to-mic path across the chassis. Locking onto that direct peak with 3-point parabolic interpolation and baseband-demodulating by e^{-j 2\pi f_c n / F_s} cancels arbitrary browser/OS buffer latency to sub-millimeter precision on every frame.
  • Complex baseband clutter cancellation. Walls, desk monitors, and chairs are 20–30 dB louder than a human hand, so raw envelopes are useless for motion tracking. Subtracting a slow exponential moving average in the complex baseband domain cancels stationary room architecture cleanly while leaving moving targets glowing in isolation.
  • Multiplicative bistatic imaging & range-rate Doppler. Knowing the speaker baseline b, every pixel (x, z) in front of the screen defines an exact speaker-to-target-to-mic path for Left and Right. Multiplying \sqrt{E_L(r_L) \cdot E_R(r_R)} collapses each speaker's range ellipse into a localized 2D target blip, while smoothed range-rate tracking measures radial approach/recession velocity without carrier-phase aliasing.

If your speakers roll off steeply above 18 kHz, switch the band selector to 14–18 kHz (high-band) or 4–12 kHz (classic audible sonar with 2.1 cm range resolution).

Mode 2: Broadband room echolocation & 3D architecture

Ultrasonic frequencies lose too much energy in air and off soft furniture to map walls three meters away. Switch to room echolocation and the lab changes physics completely:

  • Coherent broadband pings. A single scan transmits 3× Left up-chirps followed by 3× Right down-chirps sweeping 1.4 \leftrightarrow 15.6\text{ kHz} (42 ms each, 380 ms apart). Pulse compression delivers ~1.2 cm theoretical range resolution, and coherently aligning and averaging all three pings boosts wall echo SNR by +4.8 dB.
  • 360° bistatic floorplan & orthogonal 0° + 90° fusion. One stereo baseline can tell left from right, but a single axis has an unavoidable front/back mirror ambiguity (a wall at +z and a wall at -z produce the exact same L/R path difference). Clicking ping 0° maps your front, left, and right walls while honestly marking the back wall as an estimate; turning the laptop 90° clockwise and pressing +90° fuse rotates the acoustic baseline so multiplicative coincidence erases mirror ghosts and locks all four walls in 360°.
  • ISO 3382 Schroeder RT60 & DRR. Integrating the squared impulse tail backward from 320 ms (excluding the direct speaker-to-mic spike) yields the room's Energy Decay Curve, Direct-to-Reverberant Ratio, and T_{20}-extrapolated reverberation time (RT60).
  • WebGL2 3D wireframe & slow-motion wavefront replay. The strongest cardinal wall planes and specular reflectors assemble into an orbitable 3D wireframe box over the projected acoustic floorplan. Press replay wave to watch an acoustic sphere expand from the laptop at 1/250× the speed of sound (~1.37 m/s), illuminating walls, reflector beacons, and the stereo RIR playhead at the exact millisecond each echo returned.

Honest hardware limits

Just like the Modem Lab, real-world browser acoustics live or die on two things:

  1. Raw microphone audio. By default getUserMedia enables voice echo cancellation (AEC), noise suppression (NS), and automatic gain control (AGC) — which actively hunt down and erase sonar chirps. Sonar Lab requests all three off, though some Bluetooth headsets and OS drivers still inject hidden DSP. Use your laptop's built-in speakers and mic with headphones unplugged.
  2. Specular mirrors, not lidar. A two-speaker, one-mic laptop is a low-aperture bistatic sonar, not a millimeter-wave radar or laser scanner. Hard flat surfaces perpendicular to the speakers (walls, windows, monitors, closed doors) return sharp echoes that lock cleanly; soft curtains, clutter, and oblique surfaces scatter diffusely into the RT60 tail. The estimated 3D room box and ceiling height are best-fit acoustic reconstructions, not surveyor blueprints.

Built with

  • Zero-dependency Rust → WebAssembly (sonar.wasm, 79 KB): in-place radix-2 FFT, analytic Hann LFM matched filters, baseband direct-path sub-sample lock, complex clutter canceller, ISO 3382 Schroeder RT60, 2D/360° bistatic back-projection, and a 3D image-source acoustic room simulator.
  • Full-duplex AudioWorklet: synchronized stereo TX playback and mono RX capture on the audio thread (sonar-worklet.js).
  • WebGL2 + Canvas 2D: custom GLSL floorplan illumination shader, Fresnel wavefront shell, dynamic wall flash shaders, and Aliens-style phosphor motion scope.

More in Labs · sister acoustic experiment in Modem Lab · browse Notes.