# Fit sine wave with a distorted time-base

If your resolution is high enough, then this is basically an angle-demodulation problem. The standard way to demodulate an angle-modulated signal is to take the derivative, followed by an envelope detector, followed by an integrator.

Since I don't know the exact numbers you're using, I'll show an example with my own numbers. Suppose my original timebase has 10 million points from 0 to 100:

```
t = 0:0.00001:100;
```

I then get the distorted timebase and calculate the distorted sine wave:

```
td = 0.02*(t+2).^3;
yd = sin(td);
```

Now I can demodulate it. Take the "derivative" using approximate differences divided by the step size from before:

```
ydot = diff(yd)/0.00001;
```

The envelope can be easily detected:

```
envelope = abs(hilbert(ydot));
```

This gives an approximation for the derivative of P(t). The last step is an integrator, which I can approximate using a cumulative sum (we have to scale it again by the step size):

```
tdguess = cumsum(envelope)*0.00001;
```

This gives a curve that's almost identical to the original distorted timebase (so, it gives a good approximation of P(t)):

You won't be able to get the constant term of the polynomial since we made our approximation from its derivative, which of course eliminates the constant term. You wouldn't even be able to find a unique constant term mathematically from just `yd`

, since infinitely many values will yield the same distorted sine wave. You can get the other three coefficients of P(t) using polyfit if you know the degree of P(t) (ignore the last number, it's the constant term):

```
>> polyfit(t(1:10000000), tdguess, 3)
ans =
0.0200 0.1201 0.2358 0.4915
```

This is pretty close to the original, aside from the constant term: 0.02*(t+2)^3 = 0.02t^3 + 0.12t^2 + 0.24t + 0.16.

You wanted the inverse mapping Q(t). Can you do that knowing a close approximation for P(t) as found so far?