atan2 giving larger than expected negative outputs
1 view (last 30 days)
Show older comments
I'm writing a simple script that takes the x,y coordinates of a tracked object at 10fps and finds the directed angle trajectory for the path of vectors taken. I'm using atan2 to do this but for 10 points taken in a second, I'm getting larger negative values than what seems logical for a real time tracking output. I attached my first 10 coordinate pairs and the first 10 angle outputs. I'm just not understanding how within a second, my tracked object can turn ~-100 degrees 10 times in a second.
x = [339.00, 339.98, 337.52, 336.76, 336.10, 335.76, 335.45, 335.39]
y = [342.35, 329.89, 323.63, 321.97, 320.32, 319.69, 319.41, 318.79]
traj = [-85.5028, -111.4534, -114.5998, -111.8014, -118.3550, -137.9108, -97.7652, -113.9625, -101.3099, 26.5432]
.
fid = fopen('') ;
data = textscan(fid, '%f %f %f %f %f %f %f %f %f %f %f %f %f %f', 'HeaderLines',2);
data = cell2mat(data);
dx = [diff(data(:,4))];
dy = [diff(data(:,5))];
angle = atan2d(dy,dx);
traj = diff(angle);
fclose(fid);
3 Comments
John D'Errico
on 26 Apr 2020
I explained in my answer. atan2d does not give you the CHANGE in slope from step to step. As you are doing things, it produces the slope of the line connecting consecutive points (measured in degrees). The slope of that line is actually relatively constant.
Answers (1)
John D'Errico
on 26 Apr 2020
Edited: John D'Errico
on 26 Apr 2020
What you don't understand is the geometry, and perhaps how to interpret what atan2d tells you.
(Was it really necessary to post a PICTURE OF NUMBERS?????? You honestly could not paste in a block of text?)
Point#1: (339.00, 342.35)
Point#2: (339.98, 329.89)
Those are the (x,y) coordinates of a successive pair of points. What angle does the line that connects those two points lie at? From your pasted in picture of numbers, I might guess it lies at an angle of around 90 to 100 degrees or so.
xy1 = [339.00, 342.35; 339.98, 329.89];
dxy = diff(xy);
atan2d(dxy(2),dxy(1))
ans =
-85.503
So the vector connecting those two points lies at an angle of -85.5 degrees.
Sadly, I have no desire to type in your table of numbers. However, Look at the next (x,y) pair, compared to the 2nd pair. It lies at fairly close to the SAME angle.
Things are NOT turning by -90 degrees at every tep. They lie at roughly the SAME inclination, step after step. It varies by 5 or 10 degrees between most of those steps.
It was only that final step shown where anything of any real significance happens. And why is that? Probably because the object is slowing down. If you computed the distance between each pair of points, the distance traveled seems to be slowing down. So at the very end, what you are seeing is now turning into almost random jitter in the measurement.
2 Comments
John D'Errico
on 26 Apr 2020
If you want to then find how the angle of inclination changes, then yes. Just diff, applied to the sequence of angles from atan2d will do it.
But again, let me point out a problem you have at the end, if you are looking at the change in inclination.
sqrt(diff(x).^2 + diff(y).^2)
ans =
12.498 6.726 1.8257 1.7771 0.71589 0.41773 0.6229
See that the successive points are getting closer to each other as you go along. If we assume these are measurements taken at a roughly constant time step, that means whatever you are tracking is slowing down. At the very end, the change in angle is now quite significant, but that just reflects the limits of your ability to measure the true location at a point in time. Measurement error is starting to become significant there, so that final angle as measured is now down into the random noise of your process.
See Also
Categories
Find more on Trajectory and Scenario Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!