Search code examples
calgebratrilateration

Issues implementing the Fang Algorithm for TDOA Trilateration


I've been following this paper (notably the section on Fang's Method) in an attempt to achieve a solution to the problem of trilateration using the TDOA technique.

I'm hoping that someone experienced in Fang / TDOA can lend me a helping hand. For some reason, my implementation is returning incorrect roots to the final quadratic. Here's the code I've written so far:

#include <stdio.h>
#include <math.h>

struct Point {
    double x;
    double y;
};

inline double sqr(double n) {
    return n * n;
}

// r1 and r2 are the TDOA of the sound impulse to p1 and p2, respectively
void fang(double r1, double r2) {
    // transmitter coords
    Point tx = {0.7, -0.1};

    // receiver coordinates
    Point p0 = {0, 0};
    Point p1 = {1.7320508075688772, 0};
    Point p2 = {0.8660254037844388, 1.5};

    // linear coefficients
    double g = ((r2 * (p1.x/r1)) - p2.x) / p2.y;
    double h = (sqr(p2.x) + sqr(p2.y) - sqr(r2) + r2 * r1 * sqr(1 - (p1.x / r1))) / (2 * p2.y);

    // quadratic coefficents
    double d = -(1 - sqr(p1.x / r1) + sqr(g));
    double e = p1.x * (1 - sqr(p1.x / r1)) - (2 * g * h);
    double f = (sqr(r1) / 4) * sqr(1 - sqr(p1.x / r1)) - sqr(h);

    double result_x = (-e - sqrt(sqr(e) - (4 * d * f))) / (2 * d);
}

int main() {
    // these values have been calculated a-priori, from the known transmitter coords
    double r1 = 0.32977743096231715;
    double r2 = 0.90148404145971694;
    fang(r1, r2);
}

Ultimately I'd expect the x_result to be equal to the transmitter's x coordinate (tx.x == 0.7), but frustratingly the result is ≈0.237.

An outline of my exact problem (and it's solution, where the two hyperbolas intersect) can be viewed geometrically in the below graph:

TDOA graph

Any help would be hugely appreciated!


Solution

  • The paper gives the following calculation for h with the Fang method:

    Calculation for <code>h</code>

    Your code incorrectly squares the entire (1 - (p1.x / r1)) expression, instead of just the (p1.x / r1) part. Moreover, you use the wrong values (p2 and p1) instead of the correct ones (p3 and p2). To fix, simply change h to:

    double h = (sqr(p3.x) + sqr(p3.y) - sqr(r3) + r3 * r2 * (1 - sqr(p2.x / r2))) / (2 * p3.y);