How do I plot the average curve from hundreds of vehicle speed curves and further show only the average curve?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
ADJE JEREMIE ALAGBE
le 6 Mai 2023
Commenté : Mathieu NOE
le 11 Mai 2023
I am attaching an excel file which contains vehicle trajectories data. Trajectory data were extracted form a video. They consist of the video frame numbers, each vehicle's ID, the record time, the vehicle position x and y coordinates, and the speeds were calculted based on the coordinates and time. In the excel file, the first collumn contains the tracking frame numbers (fram_num), the second collumn the vehicle's ID (veh_ID), the third collumn the tracking time (time), the fourth and fifth collumns respectively the x and y cordinates of the vehicle position (X_position, Y_position), then the last two collumns are the speeds of the vehicle respectively in the x and y directions. You can see that only three vehicles' data are present in the file, that is three different IDs. This is just a small sample for demonstration convenience. Otherwise, the original data have samples from hundreds of vehicles, so hundreds of different vehicle IDs.
First, I want to plot in the same figure all the vehicles' speeds (X_speed) against the vehicle position (X_position).
Second, I want to smoothen the courves.
Third, I want to plot a single average curve of all the hundreds curves (note that in this example only three curves will exist), that should also be smooth.
Then, if possible, in a separate figure, eliminate all other curves and show only the average curve.
For my first preoccupation, I have this code that plots all the vehicles' speed curves:
T = readtable('VehicleSpeed.xlsx','Sheet','SpeedAvg');
N = max(T.veh_id);
hold on
for k = 1:N
Tk = T(T.veh_id == k,:);
plot(Tk.X_position, Tk.X_speed);
end
hold off
xlabel('Distance (m)')
ylabel('Speed (m/s)')
The generated figure is bellow:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1375934/image.jpeg)
Then I need your valuable help for the other preoccupations, such as smoothing, average curve plotting and smoothing, and showing only the average curve in another figure while the previous figure that includes all curves with the average also exists.
Thank you in advance, and have nice weekend!
0 commentaires
Réponse acceptée
Mathieu NOE
le 9 Mai 2023
Modifié(e) : Mathieu NOE
le 9 Mai 2023
hello
try this
as every vehicule will have a different range of x data (pos) , you need to first interpolate the data before you can average them
hope it helps !
%% step 1 : retrieve raw data, plot, store in cells for further processing
pos_low = 0;
pos_high = Inf;
T = readtable('VehicleSpeed.xlsx','Sheet','SpeedAvg');
[vid,ia,ic] = unique(T.veh_id);
hold on
for k = 1:numel(vid)
idx = T.veh_id == vid(k);
pos{k} = T.X_position(idx);
size(pos{k})
vel{k} = T.X_speed(idx);
size(vel{k})
plot(pos{k}, vel{k});
% define low and high range limits for future common pos axis
pos_low = max(pos_low,pos{k}(1));
pos_high = min(pos_high,pos{k}(end));
end
hold off
xlabel('Distance (m)')
ylabel('Speed (m/s)')
%% step 2 : create common unique x axis and interpolate data, average curves, smooth
pos_all = linspace(pos_low,pos_high,300); % new x axis
for k = 1:numel(vid)
vel_int(k,:) = interp1(pos{k},vel{k},pos_all);
end
vel_int_average = mean(vel_int); % average curve
vel_int_average_smoothed = smoothdata(vel_int_average,'movmedian',50); % smooth it
figure, plot(pos_all,vel_int_average,'k',pos_all,vel_int_average_smoothed,'*-');
xlabel('Distance (m)')
ylabel('Speed (m/s)')
legend('average','average smoothed');
figure, plot(pos_all,vel_int_average_smoothed,'*-');
xlabel('Distance (m)')
legend('average smoothed');
ylabel('Speed (m/s)')
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Vehicle Scenarios dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!