# How to compute the lap count in the data

8 views (last 30 days)

Show older comments

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)]);

##### 2 Comments

Cris LaPierre
on 17 Apr 2023

### Accepted Answer

Mathieu NOE
on 17 Apr 2023

hello

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

filename='result.csv';

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))));

plot(turns);

hold on

plot(LOCS_all,PKS_all,'dr');

title(['Turns completed = ' num2str(turns_completed)]);

xlabel('Samples');

ylabel('Turns');

##### 14 Comments

Mathieu NOE
on 18 Apr 2023

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!