← Back to Speed Detector

Speed Detector 2.0

Real-Time GPS Speed Measurement Using WebAssembly

ABSTRACT

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.

1. Introduction

1.1 Problem Statement

Traditional speed measurement applications face several challenges:

1.2 Solution Overview

Speed Detector 2.0 addresses these challenges through:

2. Technical Architecture

2.1 System Components

┌─────────────────────────────────────────────────────────┐ │ Browser Environment │ ├─────────────────────────────────────────────────────────┤ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ index.html │◄──►│ speed.js │◄──►│ speed.wasm │ │ │ │ (UI) │ │ (Glue) │ │ (C++) │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │ │ │ │ ▼ │ │ ┌─────────────────────────────────────────────────┐ │ │ │ Geolocation API (GPS Data) │ │ │ └─────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────┘

2.2 Technology Stack

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

3. Core Algorithm

3.1 Haversine Formula

The system uses the Haversine formula to calculate great-circle distances between GPS coordinates:

d = 2r · arcsin(√(sin²(Δφ/2) + cos(φ₁)·cos(φ₂)·sin²(Δλ/2)))

Where:

3.2 Speed Calculation

Speed is derived from consecutive GPS samples:

v = d / (t₂ - t₁)

Where v = velocity (m/s), d = Haversine distance, and t₁, t₂ = timestamps of consecutive samples.

3.3 GPS Jitter Filtering

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

4. Implementation Details

4.1 C++ Core Module

// 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
}

4.2 Compilation Command

emcc speed.cpp \
  -O3 \
  -s EXPORTED_FUNCTIONS='["_add_sample","_ms_to_kmh","_reset"]' \
  -s EXPORTED_RUNTIME_METHODS='["cwrap"]' \
  -o speed.js

4.3 JavaScript Integration

// 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,     []);

5. Performance Analysis

5.1 WebAssembly vs JavaScript

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

6. Deployment

6.1 Server Requirements

6.2 Nginx Configuration

server {
    listen 80;
    server_name example.com;

    root /var/www/speed_detector_2.0;
    index index.html;

    location ~ \.wasm$ {
        types { application/wasm wasm; }
    }
}

7. Future Enhancements

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

8. Conclusion

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.