Identifying Vehicle Reversing I am trying to identify if a vehicle is reversing. I have the position of the vehicle, the direction the vehicle is facing (as an angle), and the speed of the vehicle at different time points.  Can someone help me understand exactly when is the vehicle reversing. Any help will be greatly appreciated. 
 A: To figure out if you are reversing, all you really need to know is velocity and heading.
If velocity is in the same direction as heading, then you are going forward. If velocity is in the opposite direction as heading then you are in reverse.
From code you posted in an earlier version of your question, it appears you are calculating speed by first calculating velocity by numerical differentiation of the position data, and then taking the absolute value to get speed. 
Instead of throwing the velocity information away and just saving the speed, you should keep the velocity data. Or, if the code you posted doesn't actually show what you were doing, you could do the calculation I thought you were doing and calculate it from the change in position coordinates
$$\vec{v}\approx\left(\frac{\Delta X}{\Delta t},\frac{\Delta Y}{\Delta t}\right)$$
Now you have velocity, you can compare it with heading. We know that the dot product of two vectors has the value
$$\vec{a}\cdot\vec{b}=|\vec{a}|\ |\vec{b}|\cos \theta$$
where $\theta$ is the angle between the two vectors. And $\cos\theta$ is positive for angles between $-\pi/2$ and $\pi/2$, but negative for other angles between $-\pi$ and $\pi$.
Therefore if you take $\vec{v}\cdot\vec{h}$ (where $\vec{h}$ is the heading vector), and you get a positive value then you are going forward and if you get a negative value you are going in reverse.
Practically, there could be some difficulty because you only have an estimate of the velocity, not an exact value for it. You might also have some noise in the instruments that measure position or velocity. This could cause challenges if velocity and/or heading is changing quickly, or if the speed is near zero. But solving these challenges gets into the realm of engineering rather than physics.
A: If I have read your problem correctly, you are given: position in Cartesian coordinates as $x_i$, $y_i$, the speed of the vehicle $s_i \ge 0$ and the heading angle $\theta_i$ all at the time $t_i$ where $0 \le i \le N$ where you have $N+1$ data points.
As stated, your problem leaves several issues unresolved. To avoid several exchanges in the comments, I'll list those issues here and indicate the assumptions I've made concerning each while preparing this answer:


*

*When you say vehicle is reversing do you mean changing gears so that the vehicle's motion is in the direction opposite to the direction of its heading? I will assume that the vehicle changed gears to travel in the opposite direction but did not otherwise `turn around'.

*Are your data points ordered such that $t_{i+1} > t_i$? I will assume that they are.

*Let $\Delta t_j = t_j - t_{j-1}, \quad 1 \le j \le N$ be the time interval between data points. Are all $\Delta t_j$ equal? I will assume that they are not.

*If (the largest) $\Delta t_j$ is not significantly shorter than the time it would take to stop the vehicle, change gears and then accelerate in the opposite direction, then the resolution of your data is insufficient to determine if the vehicle did in fact change gears and reverse its direction every time it does so. I will assume that $\Delta t_j$ is sufficiently small so that all such events can be identified.

*You have not specified your coordinate system. I'll define my own: let's assume then that $x$ is East with unit vector $\hat{x}$, $y$ is North with unit vector $\hat{y}$, and heading angle $\theta$ is measured from north in a clockwise sense. 

*Let $\Delta x_j = x_j - x_{j-1}$ and $\Delta y_j = y_j - y_{j-1}$, then let $\Delta \vec{R}_j = \Delta x_j \hat{x} + \Delta y_j \hat{y}$ be the change in position during the time interval $\Delta t_j$. If the magnitude of $\Delta \vec{R}_j$, $|\Delta \vec{R}_j|$, is zero, then I'll assume that the vehicle was in fact stationary over that time period.


We can make a first order estimate of the vehicle's velocity $\vec{V}_j$ midway between times $t_{j-1}$ and $t_{j}$, $1 \le j \le N$, as
$$\vec{V}_j = \frac{\Delta \vec{R}_j}{\Delta t_j} = v_j \hat{V_j}$$
where $v_j = |\vec{V}_j| = \sqrt{\vec{V}_j\cdot \vec{V}_j}$ is the magnitude of the velocity vector $\vec{V}_j$ given by
$$v_j = \frac{\sqrt{\Delta x_j^2 + \Delta y_j^2}}{\Delta t_j}$$
If $v_j > 0$ then $\hat{V_j} = {\vec{V}_j}/{v_j}$ is a unit vector in the direction of motion:
$$\hat{V}_j = \frac{\vec{V_j}}{v_j} = \frac{\Delta \vec{R}_j}{v_j\Delta t_j} = \frac{\Delta x_j \hat{x} + \Delta y_j \hat{y}}{v_j\Delta t_j}$$
The first order approximation of the heading angle,  $\bar\theta_j$, at a point midway between the data times $t_{j-1}$ and $t_j$ is given by the average of the headings at those times:
$$\bar\theta_j = \frac{\theta_{j-1} + \theta_j}{2} $$
so that a unit vector directed in the forward direction of the vehicle at that midpoint, $\hat{h}_j$, is given by
$$\hat{h}_j = \sin\bar\theta_j \hat{x} + \cos\bar\theta_j \hat{y}$$
If the vehicle did in fact travel in reverse gear, its velocity unit vector should point in the direction opposite to the vehicle's heading vector. Then if
$$\hat{V}_j \cdot \hat{h}_j \approx -1 < 0$$
the vehicle was traveling in reverse.
If, on the other hand,
$$\hat{V}_j \cdot \hat{h}_j \approx +1 > 0$$
the vehicle was traveling forward.
