How to split a kinematic track into a for loop?

2 ビュー (過去 30 日間)
ivy
ivy 2024 年 9 月 20 日
回答済み: Rahul 2024 年 9 月 23 日
Hi all, can anyone help me in figuring out how to split a kinematic track within a for loop in order to better catch the start and end points of each 160 trial?
figure;
plot(p.wrist_sx_speed_f)
for itrial = 1:160
[X,~] = ginput(2);
p.tab_start(itrial) = X(1);
p.tab_stop (itrial) = X(2);
end
  4 件のコメント
KSSV
KSSV 2024 年 9 月 20 日
Using ginput will be a manual job.. How did you generate this data? What is criteria for a one complete trial?
Mathieu NOE
Mathieu NOE 2024 年 9 月 20 日
if one cycle contains one peak , then maybe you can simply identify 10 consecutives peaks (with findpeaks or any alternative ) , add the previous half period signal and same , add the trailing half period signal , and repeat that until you've reached the end of your record

サインインしてコメントする。

回答 (1 件)

Rahul
Rahul 2024 年 9 月 23 日
Hi,
I believe that you’re trying to use a loop to interactively define the start and stop points of each trial based on the given kinematic data, after splitting it into observable ranges of length 10 trials.
Given the distribution of the data, I’m assuming a random sharp peak in every trial and that there are 160 trials in total.
You can split the kinematics using the “findpeaks” function to find the locations of local Maximas, which represent each trial, group 10 trials in a single vector and then repeat for remaining trials.
Here's a possible solution to how you can structure the code:
  • Generating Kinematics: A sample distribution with a trial consisting of a single randomly heighted peak, and a random occurrence in the span of 100 points, for that trial.
numTrials = 160;
trialsPerPlot = 10;
numGroups = numTrials/trialsPerPlot;
pointsPerTrial = 100; % Number of data points per trial
% Generate the data
kinematicData = [];
for i = 1:numTrials
% Create a baseline of zeros
trialData = zeros(1, trialsPerPlot);
peakHeight = 10 + 10 * rand(1);
peakPosition = randi(trialsPerPlot);
% Add a sharp peak
trialData(peakPosition) = peakHeight;
% Store the trial data
kinematicData = [kinematicData trialData];
end
plot(kinematicData);
  • Splitting Data: Based on the above description of trials and data generated, using peak locations, allocate ranges of ‘kinematicData’ as 10 peaks or trials into column vectors:
% Find Peak Locations
[~, peaks] = findpeaks(kinematicData);
% Since two consecutive peaks can be atmost 2*100 spaced
trialData = zeros(2*pointsPerTrial, numTrials);
prev = 0;
size(peaks)
for i=1:numGroups
mid = 0;
if i == numGroups
trialData(1:(numTrials*trialsPerPlot - prev), i) = kinematicData(1 + prev:end)';
else
mid = round((peaks(trialsPerPlot*i) + peaks(trialsPerPlot*(i + 1)))/2);
trialData(1:(mid-prev), i) = kinematicData(1 + prev:mid)';
end
prev = mid;
end
trialData
  • Figure Displaying: To display the speed data in each iteration of the loop. This means that each trial will allow you to select two points on the figure and store them in ‘p.tab_start’ and ‘p.tab_stop’.
  • You are looping through 160 trials and letting the user select the start and end of each. If you intend to keep the same figure visible during the entire loop, plotting inside the loop is unnecessary; just plot once outside the loop.
figure;
plot(trialData(:, 1));
title('Select Start and Stop Points for Each Trial');
% Hold the plot so you can see the data as you make selections
hold on;
for itrial = 1:160
disp(['Select start and stop points for trial ', num2str(itrial)]);
% Use ginput to get the start and stop points for each trial
[X, ~] = ginput(2);
% Save the start and stop points
p.tab_start(itrial) = X(1);
p.tab_stop(itrial) = X(2);
% Optionally, plot the start/stop points on the figure for visual feedback
plot([X(1), X(1)], ylim, 'r--'); % Start point
plot([X(2), X(2)], ylim, 'g--'); % Stop point
end
The plot of ‘trialData’ happens outside the loop. This way, the plot remains visible for all trials.
ginput(2) allows you to click twice on the plot (once for the start, once for the stop
Each trial's start and stop points are marked on the plot with red (r--) and green (g--) dashed lines for visual feedback.
For more information regarding usage of “ginput” or “findpeaks” functions in MATLAB, refer to the documentation links mentioned below:

カテゴリ

Help Center および File ExchangeDiscrete Data Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by