Real-Time GPS Speed Measurement Using WebAssembly
Speed Detector 2.0 is a lightweight, browser-based speed measurement application that leverages WebAssembly (WASM) for high-performance GPS calculations. By compiling C++ code to WASM, the system achieves near-native performance for real-time Haversine distance calculations while maintaining cross-platform compatibility across all modern web browsers.
Traditional speed measurement applications face several challenges:
Speed Detector 2.0 addresses these challenges through:
| Component | Technology | Purpose |
|---|---|---|
| Core Algorithm | C++ | High-performance distance/speed calculations |
| Compilation | Emscripten (emcc) | C++ to WebAssembly transpilation |
| Runtime | WebAssembly | Near-native execution in browser |
| Interface | HTML5/JavaScript | User interface and GPS integration |
| GPS Data | Geolocation API | Real-time position tracking |
The system uses the Haversine formula to calculate great-circle distances between GPS coordinates:
Where:
Speed is derived from consecutive GPS samples:
Where v = velocity (m/s), d = Haversine distance, and t₁, t₂ = timestamps of consecutive samples.
To eliminate false readings from GPS noise, the system implements dual-threshold filtering:
| Filter | Threshold | Rationale |
|---|---|---|
| Minimum Distance | 2.0 m | GPS accuracy typically ±2-10m; movements below 2m are likely noise |
| Minimum Speed | 0.5 m/s (1.8 km/h) | Below walking pace; filters out residual jitter |
// Key exported functions
extern "C" {
void reset(); // Reset state
double add_sample(lat, lon, timestamp); // Add GPS sample, returns speed (m/s)
double ms_to_kmh(double ms); // Convert m/s to km/h
}
emcc speed.cpp \
-O3 \
-s EXPORTED_FUNCTIONS='["_add_sample","_ms_to_kmh","_reset"]' \
-s EXPORTED_RUNTIME_METHODS='["cwrap"]' \
-o speed.js
// Initialize WASM bindings
window.addSample = Module.cwrap('add_sample', 'number', ['number','number','number']);
window.msToKmh = Module.cwrap('ms_to_kmh', 'number', ['number']);
window.resetState = Module.cwrap('reset', null, []);
| Metric | JavaScript | WebAssembly | Improvement |
|---|---|---|---|
| Haversine calculation | ~0.05ms | ~0.005ms | 10x faster |
| Memory usage | Higher (GC overhead) | Lower (linear memory) | ~30% reduction |
| Startup time | Faster | Slightly slower | Trade-off |
.wasm files: application/wasmserver {
listen 80;
server_name example.com;
root /var/www/speed_detector_2.0;
index index.html;
location ~ \.wasm$ {
types { application/wasm wasm; }
}
}
| Feature | Description | Priority |
|---|---|---|
| Kalman Filter | Advanced noise reduction using predictive filtering | High |
| Offline Support | Service Worker for PWA functionality | Medium |
| Speed Alerts | Configurable notifications for speed thresholds | Medium |
| Trip Recording | Log and export route/speed data | Low |
Speed Detector 2.0 demonstrates the effective use of WebAssembly for performance-critical geospatial calculations in web applications. By combining C++ computational efficiency with browser-based accessibility, the system provides accurate, real-time speed measurement without platform-specific development overhead.