Advertisement
jovenb

Computing device location based on signal strength and timing

Jan 22nd, 2025
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.97 KB | Cybersecurity | 0 0
  1. // Mock data for cell towers (coordinates and signal strength in dBm)
  2. const towers = [
  3.   { id: "Tower1", x: 0, y: 0, signalStrength: -50 }, // Stronger signal means closer
  4.   { id: "Tower2", x: 1000, y: 0, signalStrength: -60 },
  5.   { id: "Tower3", x: 500, y: 1000, signalStrength: -70 }
  6. ];
  7.  
  8. // Function to convert signal strength (RSSI in dBm) to distance (meters)
  9. function signalToDistance(signalStrength) {
  10.   const referenceSignal = -50; // Signal strength at 1 meter
  11.   const pathLossExponent = 2; // Typical value for free space
  12.   return Math.pow(10, (referenceSignal - signalStrength) / (10 * pathLossExponent));
  13. }
  14.  
  15. // Calculate the device location using trilateration
  16. function calculateLocation(towers) {
  17.   // Convert signal strength to distances
  18.   const distances = towers.map(tower => ({
  19.     id: tower.id,
  20.     x: tower.x,
  21.     y: tower.y,
  22.     distance: signalToDistance(tower.signalStrength)
  23.   }));
  24.  
  25.   // Simple trilateration formula (for educational purposes)
  26.   // A real-world application requires robust algorithms
  27.   const A = 2 * (distances[1].x - distances[0].x);
  28.   const B = 2 * (distances[1].y - distances[0].y);
  29.   const C =
  30.     Math.pow(distances[0].distance, 2) -
  31.     Math.pow(distances[1].distance, 2) -
  32.     Math.pow(distances[0].x, 2) +
  33.     Math.pow(distances[1].x, 2) -
  34.     Math.pow(distances[0].y, 2) +
  35.     Math.pow(distances[1].y, 2);
  36.  
  37.   const D = 2 * (distances[2].x - distances[1].x);
  38.   const E = 2 * (distances[2].y - distances[1].y);
  39.   const F =
  40.     Math.pow(distances[1].distance, 2) -
  41.     Math.pow(distances[2].distance, 2) -
  42.     Math.pow(distances[1].x, 2) +
  43.     Math.pow(distances[2].x, 2) -
  44.     Math.pow(distances[1].y, 2) +
  45.     Math.pow(distances[2].y, 2);
  46.  
  47.   const x = (C * E - F * B) / (E * A - B * D);
  48.   const y = (C * D - A * F) / (B * D - A * E);
  49.  
  50.   return { x, y };
  51. }
  52.  
  53. // Calculate and display the estimated location
  54. const deviceLocation = calculateLocation(towers);
  55. console.log("Estimated Device Location:", deviceLocation);
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement