# How do I identify subsets of data and divide the data into the subsets

4 views (last 30 days)
Bill P on 13 Dec 2018
Commented: Chris Turnes on 8 Jan 2019
Right now I have code that takes data from two IMU's attached to each side of a knee joint and finds the angle in the knee while someone performs a squat. The Output is the angle on each plane between the two IMUs (the main one I am concerned with is the blue line which is the angle assuming the knee can only rotate on one axis). Plot attached.
I am wondering how I can identify and divide the data so I can analyze each "Repitition" of the exercise on its own. In the plot attached there would be three seperate repetitions I would want to look at.

Mark Sherstan on 13 Dec 2018
Edited: Mark Sherstan on 13 Dec 2018
Look at the function islocalmin and islocalmax. You can specify minimum prominence to filter out some of the noise at the bottom of your data to get just one minimum or one maximum for each of the trials. Your data looks fairly good so you might be able to just find the time each minimum happens and divide it by 2 to get that range and analyze accordingly.
If you post a .mat file with your data I could help to implment the functions.

Bill P on 13 Dec 2018
Hi Mark, thanks for the response. I want to be able to capture and analyze if each repition takes a different length of time, or if there is a hiccup (someone faltering on the way up or down), an example being following (i took out the unnecessary other traces):
Would the local min and max work in this scenario? .mat file of angle and time for this example attached
Mark Sherstan on 14 Dec 2018
Here you go :) Let me know if you have any questions (code is commented).
% Load in data and clear out start and end noise manually
jointAngle(1:600) = 0;
jointAngle(3560:end) = 0;
% Locate only the minimums (start and end of a cycle)
jointAngle2 = jointAngle;
jointAngle2(jointAngle2>10) = NaN;
idx = islocalmin(jointAngle2,'FlatSelection','all','MinSeparation',100);
idx = find(idx);
% Seperate out the data
ii = 1;
for jj = 1:2:length(idx)-1
out.jointAngle{ii} = jointAngle(idx(jj):idx(jj+1));
out.time{ii} = time(idx(jj):idx(jj+1));
ii = ii + 1;
end
% Plot the results
figure(1)
hold on
plot(time,jointAngle)
plot(time(idx),jointAngle(idx),'*r')
legend('Data','Seperation Points')
hold off
figure(2)
for ii = 1:length(out.time)
subplot(2,2,ii)
plot(out.time{ii},out.jointAngle{ii})
title(num2str(ii),'FontSize',16)
end

Chris Turnes on 17 Dec 2018
Mark's answer is a great way of doing this. In addition, you might consider looking at the ischange function:
% Look for at most 9 changes, since there are 4 segments of interest and 5 other segments
tf = ischange(jointAngle, 'SamplePoints', time, 'MaxNumChanges', 9);
plot(time, jointAngle, time(tf), jointAngle(tf), '*r')
This doesn't give you exactly the same results, as it basically finds the middle points of the rising and falling edges. But if all you need is to analyze the widths relative to each other, then it might also work.
To make it more robust, you could instead use the Threshold option from ischange:
% Use the threshold factor to find changes instead
tf = ischange(jointAngle, 'SamplePoints', time, 'Threshold', 1e5);
It's a bit harder to intuitively guess that the value for Threshold should be, but if you find a good one that works, it will (hopefully) work across multiple traces.

Show 1 older comment
Chris Turnes on 7 Jan 2019
Sorry, just to clarify -- do you need to just extract the portions between the detections (like in my image above), or do you need the detections to be at the minima rather than the midway point in the slope? Or both?
Bill P on 8 Jan 2019
The detections would need to be at the minima if possible
Chris Turnes on 8 Jan 2019
You can try telling ischange to look for piecewise linear changes rather than piecewise constant changes. This gets a bit closer, I think:
tf = ischange(jointAngle, 'linear', 'SamplePoints', time, 'Threshold', 1e4);
If that's not quite precise enough, then Mark's approach may be the better option for you. Or, perhaps you could mix the two; but I don't think there's anything built-in that makes this task particularly simple.