How to compute the lap count in the data

8 views (last 30 days)
I want to compute the lap count of a car, means how many times a car has completed a lap. I have coordinates of the car attached as a csv file and size of the scene is height = 0.33 width = 0.342. I take the starting point as a first row of the csv file attached. The coordinates looks like this
Now I want to compute how many laps the car has taken, my logic to compute the laps is given below, it is that if the car has reached its starting point after crossing the vertical line than make it a one count of lap, now the thing is that this game has different level in the first level car start from one starting point and go in anticlockwise direction and in the next level it has another starting point and it goes in clockwise direction. I am attaching my logic as a function, in this data the lap count should be 11 but my function is giving me the count of 19. What should be done to avoid false lap count.
I call the attached function as :
filename='C:\Users\Windows 10\re.csv'
data = readtable(filename);
starting_y = data.YCoordinate(1);
width = 0.342;
height = 0.33;
lap_count = count_laps(data, width, height, starting_y, 0.001);
disp(['Lap Count: ', num2str(lap_count)]);
Cris LaPierre
Cris LaPierre on 17 Apr 2023
Have you figured out what about your data or code has caused it to come up with 19 as the number of laps?
MakM on 17 Apr 2023
Yes, the logic seems to be not that robust, since a lap is defined as moving over a line along a Y coordinate, often the car crosses that line twice, once going down, and once going up, the line for the lap is defined using the start point, sometimes the car doesn't trigger the lap because it doesn't go down far enough,the car teleports to the other side of the track, with a 14-16 second gap in observations because of level change.

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 17 Apr 2023
this would be my suggestion :
transform your cartesian data into polar ones and count how many complete revolution your car has done (in both directions)
the plot below is the angle (theta) value obtained after cart2pol conversion , divided by 2pi to have it in turns
then there is a bit of further processing to count only the completed revs (therefore the use of floor) that are plotted as red diamonds
so here we can see you have done 2 , 2 , 2 , 5 laps = 11 laps total
hope it helps
data = readtable(filename);
x = data.XCoordinate;
y = data.YCoordinate;
samples = numel(x);
center_x = mean(x);
center_y = mean(y);
[theta,r] = cart2pol(x-center_x,y-center_y);
theta = theta - theta(1); % when we start , this is our reference (finish) line (so theta must start at zero)
turns = unwrap(theta)/(2*pi); % convert theta in radians into turns
% find the positive and negative peaks (to know when we change direction)
[PKSpos,LOCSpos] = findpeaks(turns,'MinPeakDistance',1000,'MinPeakWidth',1000);
[PKSneg,LOCSneg] = findpeaks(-turns,'MinPeakDistance',1000,'MinPeakWidth',1000);
% round values to nearest integer (only completed turns must be taken into
% account)
PKSpos = floor(PKSpos);
PKSneg = -floor(PKSneg);
% merge locs and pks data and add start / end points
start_p = turns(1);
end_p = turns(samples);
end_p = floor(end_p)*sign(end_p);
LOCS_all = [1;LOCSpos;LOCSneg;samples];
PKS_all = [start_p;PKSpos;PKSneg;end_p];
% sort indexing in ascending order
[LOCS_all,I] = sort(LOCS_all);
PKS_all = PKS_all(I);
turns_completed = sum(abs((diff(PKS_all))));
hold on
title(['Turns completed = ' num2str(turns_completed)]);
Mathieu NOE
Mathieu NOE on 18 Apr 2023
ok glad you found the ultimate solution !
Mathieu NOE
Mathieu NOE on 18 Apr 2023
and FYI, the last diamond is at -1 and not -2 because the last point at not reached / passed -2 (is above -2)

Sign in to comment.

More Answers (0)


Find more on Conway's Game of Life 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!