Loop through multiple gait strides and time normalize
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello, I collected a gait sample with multiple strides and I am needed to separate each stride and time normalize them. I have them already separated out for right and left side. Any suggestions on how to code the loop to pick out each stride of the gait cycle and time normalize each one?
I've attached an excel file that my data looks like with the R/L side.
Thanks!
2 commentaires
Julius
le 7 Fév 2022
You might want to check dr. Kuhman github https://github.com/dkuhman/biomech_gait_analysis
Réponses (1)
Mathieu NOE
le 7 Fév 2022
hello
see my code below - it will extract all cycles of data and perform averaging (the black dotted lines)
clc
clearvars
data = readmatrix('examplegait.xlsx');
time = data(:,1);
ForceLT = data(:,2);
ForceRT = data(:,3);
zcr_threshold = 1; % zero crossin detection threshold / your value here
%% ForceLT processing
[t0_pos1,s0_pos1,t0_neg1,s0_neg1]= crossing_V7(ForceLT,time,zcr_threshold,'linear'); % positive (pos) and negative (neg) slope crossing points
nn = min(numel(t0_pos1),numel(t0_neg1));
for ci = 1:nn
% best solution is to interpolate the data on new time axis;
new_t1 = linspace(t0_pos1(ci),t0_neg1(ci),100);
new_ForceLT(:,ci) = interp1(time,ForceLT,new_t1');
new_t1 = new_t1 - new_t1(1); % put time axis start at zero
end
new_ForceLT_mean = mean(new_ForceLT,2);
figure(1);
plot(new_t1,new_ForceLT,new_t1,new_ForceLT_mean,'*-k');
title('ForceLT');
%% ForceRT processing
[t0_pos1,s0_pos1,t0_neg1,s0_neg1]= crossing_V7(ForceRT,time,zcr_threshold,'linear'); % positive (pos) and negative (neg) slope crossing points
nn = min(numel(t0_pos1),numel(t0_neg1));
for ci = 1:nn
% best solution is to interpolate the data on new time axis;
new_t1 = linspace(t0_pos1(ci),t0_neg1(ci),100);
new_ForceRT(:,ci) = interp1(time,ForceRT,new_t1');
new_t1 = new_t1 - new_t1(1); % put time axis start at zero
end
new_ForceRT_mean = mean(new_ForceRT,2);
figure(2);
plot(new_t1,new_ForceRT,new_t1,new_ForceRT_mean,'*-k');
title('ForceRT');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [t0_pos,s0_pos,t0_neg,s0_neg] = crossing_V7(S,t,level,imeth)
% [ind,t0,s0,t0close,s0close] = crossing_V6(S,t,level,imeth,slope_sign) % older format
% CROSSING find the crossings of a given level of a signal
% ind = CROSSING(S) returns an index vector ind, the signal
% S crosses zero at ind or at between ind and ind+1
% [ind,t0] = CROSSING(S,t) additionally returns a time
% vector t0 of the zero crossings of the signal S. The crossing
% times are linearly interpolated between the given times t
% [ind,t0] = CROSSING(S,t,level) returns the crossings of the
% given level instead of the zero crossings
% ind = CROSSING(S,[],level) as above but without time interpolation
% [ind,t0] = CROSSING(S,t,level,par) allows additional parameters
% par = {'none'|'linear'}.
% With interpolation turned off (par = 'none') this function always
% returns the value left of the zero (the data point thats nearest
% to the zero AND smaller than the zero crossing).
%
% check the number of input arguments
error(nargchk(1,4,nargin));
% check the time vector input for consistency
if nargin < 2 | isempty(t)
% if no time vector is given, use the index vector as time
t = 1:length(S);
elseif length(t) ~= length(S)
% if S and t are not of the same length, throw an error
error('t and S must be of identical length!');
end
% check the level input
if nargin < 3
% set standard value 0, if level is not given
level = 0;
end
% check interpolation method input
if nargin < 4
imeth = 'linear';
end
% make row vectors
t = t(:)';
S = S(:)';
% always search for zeros. So if we want the crossing of
% any other threshold value "level", we subtract it from
% the values and search for zeros.
S = S - level;
% first look for exact zeros
ind0 = find( S == 0 );
% then look for zero crossings between data points
S1 = S(1:end-1) .* S(2:end);
ind1 = find( S1 < 0 );
% bring exact zeros and "in-between" zeros together
ind = sort([ind0 ind1]);
% and pick the associated time values
t0 = t(ind);
s0 = S(ind);
if ~isempty(ind)
if strcmp(imeth,'linear')
% linear interpolation of crossing
for ii=1:length(t0)
%if abs(S(ind(ii))) > eps(S(ind(ii))) % MATLAB V7 et +
if abs(S(ind(ii))) > eps*abs(S(ind(ii))) % MATLAB V6 et - EPS * ABS(X)
% interpolate only when data point is not already zero
NUM = (t(ind(ii)+1) - t(ind(ii)));
DEN = (S(ind(ii)+1) - S(ind(ii)));
slope = NUM / DEN;
slope_sign(ii) = sign(slope);
t0(ii) = t0(ii) - S(ind(ii)) * slope;
s0(ii) = level;
end
end
end
% extract the positive slope crossing points
ind_pos = find(sign(slope_sign)>0);
t0_pos = t0(ind_pos);
s0_pos = s0(ind_pos);
% extract the negative slope crossing points
ind_neg = find(sign(slope_sign)<0);
t0_neg = t0(ind_neg);
s0_neg = s0(ind_neg);
else
% empty output
ind_pos = [];
t0_pos = [];
s0_pos = [];
% extract the negative slope crossing points
ind_neg = [];
t0_neg = [];
s0_neg = [];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Addition:
% % Some people like to get the data points closest to the zero crossing,
% % so we return these as well
% [CC,II] = min(abs([S(ind-1) ; S(ind) ; S(ind+1)]),[],1);
% ind2 = ind + (II-2); %update indices
%
% t0close = t(ind2);
% s0close = S(ind2);
end
1 commentaire
Voir également
Catégories
En savoir plus sur Numbers and Precision 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!