bianchy's picture
Add files using upload-large-folder tool
d1e180f verified
Raw
History Blame Contribute Delete
20 kB
% dataVisualizer.m
% dataVisualizer visualizes joint angles and EMG signals for the Gait120 dataset.
% Kinematic data visualization uses the "kinematicsVisualizer" function, and EMG signal
% visualization uses the "EMGSignalVisualizer" function.
%
% Author: Junyo Boo (2025)
% Data DOI: https://doi.org/10.6084/m9.figshare.27677016
%
% The following script is provided under the Creative Commons Attribution 4.0
% International License (CC-BY 4.0).
%
% You are free to share and adapt this material for any purpose, even commercially,
% provided you give appropriate credit, provide a link to the license, and indicate
% if changes were made. The full license text can be found at:
%
% https://creativecommons.org/licenses/by/4.0/
%
% This file also includes the 'varycolor' function originally created by Daniel Helmick (2008),
% which is provided under a BSD-style license. See the function definition below for details.
%
%---------------------------------------------------------------------------------------
clear;
clc;
data_root = 'Gait120 Data';
kinematicsVisualizer(data_root);
EMGSignalVisualizer(data_root);
%%
function kinematicsVisualizer(data_root)
target_task_name = {'LevelWalking', 'StairAscent', 'StairDescent',"SlopeAscent", 'SlopeDescent', 'SitToStand', 'StandToSit'};
task_name_num = numel(target_task_name);
target_mot_col_idx = [8, 9, 10, 11, 13];
target_spline_normal_num = 101;
total_data_cell = cell(1,task_name_num);
total_data_subject_cell = cell(1,task_name_num);
mot_data_struct = RealDir(data_root);
task_data_num = zeros(1,task_name_num);
for subject_idx = 1:numel(mot_data_struct)
subject_trial = 0;
subject_name = mot_data_struct(subject_idx).name;
subject_folder = fullfile(mot_data_struct(subject_idx).folder, mot_data_struct(subject_idx).name, 'JointAngle');
for task_idx = 1:task_name_num
task_name = target_task_name{task_idx};
task_directory = fullfile(subject_folder, task_name);
task_struct = RealDir(task_directory);
for trial_idx = 1:numel(task_struct)
trial_name = task_struct(trial_idx).name;
trial_directory = fullfile(task_directory, trial_name);
trial_struct = RealDir(trial_directory);
for step_idx = 1:numel(trial_struct)
task_data_num(task_idx) = task_data_num(task_idx)+1;
if task_idx == 6
subject_trial = subject_trial + 1;
end
end
end
end
end
task_data_idx = ones(1,task_name_num);
for i=1:task_name_num
total_data_cell{i} = zeros(target_spline_normal_num,numel(target_mot_col_idx), task_data_num(i));
total_data_subject_cell{i} = zeros(task_data_num(i),4);
end
for subject_idx = 1:numel(mot_data_struct)
subject_name = mot_data_struct(subject_idx).name;
subject_folder = fullfile(mot_data_struct(subject_idx).folder, mot_data_struct(subject_idx).name, 'JointAngle');
for task_idx = 1:task_name_num
task_name = target_task_name{task_idx};
task_directory = fullfile(subject_folder, task_name);
task_struct = RealDir(task_directory);
for trial_idx = 1:numel(task_struct)
trial_name = task_struct(trial_idx).name;
trial_directory = fullfile(task_directory, trial_name);
trial_struct = RealDir(trial_directory);
for step_idx = 1:numel(trial_struct)
mot_file_directory = fullfile(trial_struct(step_idx).folder, trial_struct(step_idx).name);
splined_mot_data = SplineMotData(mot_file_directory, target_mot_col_idx, target_spline_normal_num);
total_data_cell{task_idx}(:,:,task_data_idx(task_idx)) = splined_mot_data;
total_data_subject_cell{task_idx}(task_data_idx(task_idx),:) = [task_data_idx(task_idx), subject_idx, trial_idx, step_idx];
task_data_idx(task_idx) = task_data_idx(task_idx) + 1;
end
end
end
fprintf("Loaded %s subject kinematics.\n", subject_name);
end
f=figure();
for task_idx = 1:task_name_num
temp_task_data = total_data_cell{task_idx};
joint_num = size(temp_task_data,2);
color_list = varycolor(joint_num);
for joint_idx = 1:joint_num
plot_idx = task_name_num*(joint_idx-1)+task_idx;
subplot(joint_num,task_name_num,plot_idx);
temp_joint_data = squeeze(temp_task_data(:,joint_idx,:));
if joint_idx == 4
temp_joint_data = -temp_joint_data;
end
plot_std_box(linspace(0,100,101), temp_joint_data',color_list(joint_idx,:));
end
end
target_task_name = {"Level Walking", "Stair Ascent", "Stair Descent","Slope Ascent", "Slope Descent", "Sit-to-Stand", "Stand-to-Sit"};
joint_name = {"Hip Flexion (Deg)", "Hip Adduction (Deg)", "Hip Int. Rot. (Deg)", "Knee Flexion (Deg)", "Ankle Dorsiflexion (Deg)"};
figure(1);
for i=1:7
subplot(5,7,i);
title(target_task_name{i}, 'FontSize',10, 'FontName','Times new roman');
end
for i=1:5
subplot(5,7,7*(i-1)+1);
ylabel(joint_name{i}, 'FontSize',10, 'FontName','Times new roman');
end
for i=1:7
subplot(5,7,4*7+i);
if (i<6)
xlabel('Gait cycle (%)', 'FontSize',10, 'FontName','Times new roman');
elseif (i==6)
xlabel('Sit-to-stand (%)', 'FontSize',10, 'FontName','Times new roman');
else
xlabel('Stand-to-sit (%)', 'FontSize',10, 'FontName','Times new roman');
end
end
f.Position = [422.500000000000 55.5000000000000 1147 835];
end
%%
function EMGSignalVisualizer(data_root)
target_task_name = {'LevelWalking', 'StairAscent', 'StairDescent',"SlopeAscent", 'SlopeDescent', 'SitToStand', 'StandToSit'};
task_name_num = numel(target_task_name);
total_data_cell = cell(1,task_name_num);
data_struct = RealDir(data_root);
for subject_idx = 1:numel(data_struct)
subject_trial = 0;
subject_name = data_struct(subject_idx).name;
subject_folder = fullfile(data_struct(subject_idx).folder, data_struct(subject_idx).name, 'EMG');
emg_processed_data = load(fullfile(subject_folder, 'ProcessedData.mat'));
for task_idx = 1:task_name_num
task_name = target_task_name{task_idx};
for trial_idx = emg_processed_data.(task_name).AvailableTrialIdx
trial_name = sprintf('Trial%02d', trial_idx);
for step_idx = 1:emg_processed_data.(task_name).(trial_name).nSteps
step_name = sprintf('Step%02d', step_idx);
emg_step_table = emg_processed_data.(task_name).(trial_name).(step_name).EMGs_interpolated;
if (subject_idx == 1 && task_idx == 1 && trial_idx == 1 && step_idx ==1)
muscle_names = emg_step_table.Properties.VariableNames;
end
emg_step_array = table2array(emg_step_table);
emg_step_array_reshaped = permute(emg_step_array, [1, 3, 2]);
total_data_cell{task_idx} = cat(2, total_data_cell{task_idx}, emg_step_array_reshaped);
if task_idx == 7
subject_trial = subject_trial + 1;
end
end
end
end
fprintf("Loaded %s subject EMG data.\n", subject_name);
end
f=figure();
task_name_num = 7;
plot_task_name = {'Level Walking', 'Stair Ascent', 'Stair Descent',"Slope Ascent", 'Slope Descent', 'Sit-to-Stand', 'Stand-to-Sit'};
muscle_names_small = {'VL', 'RF', 'VM', 'TA', 'BF', 'ST', 'GM','GL','SM','SL','PL','PB'};
muscle_num = numel(muscle_names);
ymax_list = zeros(1,muscle_num);
ymin_list = zeros(1,muscle_num);
for task_idx = 1:task_name_num
temp_task_data = total_data_cell{task_idx};
color_list = varycolor(muscle_num);
for muscle_idx = 1:muscle_num
plot_idx = task_name_num*(muscle_idx-1)+task_idx;
subplot(muscle_num,task_name_num,plot_idx);
temp_joint_data = squeeze(temp_task_data(:,:,muscle_idx));
plot_std_box(linspace(0,100,101), temp_joint_data',color_list(muscle_idx,:));
ax = gca;
yMax = ax.YLim(2);
yMin = ax.YLim(1);
if muscle_idx == 1
title(plot_task_name{task_idx}, 'FontSize',10, 'FontName','Times new roman');
end
if muscle_idx == muscle_num
if task_idx < 6
xlabel('Gait cycle (%)', 'FontSize',10, 'FontName','Times new roman');
elseif task_idx == 6
xlabel('Sit-to-stand (%)', 'FontSize',10, 'FontName','Times new roman');
else
xlabel('Stand-to-sit (%)', 'FontSize',10, 'FontName','Times new roman');
end
end
if task_idx == 1
ylabel(muscle_names_small{muscle_idx}, 'FontSize',10, 'FontName','Times new roman', 'Color','k');
end
if ymax_list(muscle_idx) < yMax
ymax_list(muscle_idx) = yMax;
end
if ymin_list(muscle_idx) > yMin
ymin_list(muscle_idx) = yMin;
end
end
end
f.Position = [376.500000000000 2 1088.50000000000 1002];
for muscle_idx =1:muscle_num
for task_idx = 1:task_name_num
plot_idx = task_name_num*(muscle_idx-1)+task_idx;
subplot(muscle_num,task_name_num,plot_idx);
ylim([ymin_list(muscle_idx), ymax_list(muscle_idx)]);
end
end
han = axes(figure(1), 'Visible', 'off');
han.YLabel.Visible = 'on';
ylabel(han, 'Muscle activation', 'FontSize',15, 'FontName','Times new roman');
han.YLabel.Position(1) = han.YLabel.Position(1) - 0.01;
end
%%
function dir_struct = RealDir(directory)
dir_struct = dir(directory);
dir_struct = dir_struct(~ismember({dir_struct.name}, {'.', '..'}));
end
%%
function splined_mot_data = SplineMotData(mot_filename, target_idx, target_spline_normal_num)
mot_data = readMOT(mot_filename);
target_mot_data = mot_data.data(:,target_idx);
x = linspace(0, 1, mot_data.nr);
y = target_mot_data';
xq = linspace(0,1,target_spline_normal_num);
splined_mot_data = spline(x, y, xq)';
end
%%
function q = readMOT(fname)
% q = readMOT(fname)
% Input: fname is the name of the ascii datafile to be read
% Output: q returns a structure with the following format:
% q.labels = array of column labels
% q.data = matrix of data
% q.nr = number of matrix rows
% q.nc = number of matrix columns
% Open ascii data file for reading.
fid = fopen(fname, 'r');
if fid == -1
error(['unable to open ', fname])
end
% Process the file header;
% store # data rows, # data columns.
q.nr = 0; % Added to ensure that the q structures from reading a motion file
q.nc = 0; % are always the same, even if nr and nc are different orders in file.
nextline = fgetl(fid);
while ~strncmpi(nextline, 'endheader', length('endheader'))
if strncmpi(nextline, 'datacolumns', length('datacolumns'))
q.nc = str2num(nextline(findstr(nextline, ' ')+1 : length(nextline)));
elseif strncmpi(nextline, 'datarows', length('datarows'))
q.nr = str2num(nextline(findstr(nextline, ' ')+1 : length(nextline)));
elseif strncmpi(nextline, 'nColumns', length('nColumns'))
q.nc = str2num(nextline(findstr(nextline, '=')+1 : length(nextline)));
elseif strncmpi(nextline, 'nRows', length('nRows'))
q.nr = str2num(nextline(findstr(nextline, '=')+1 : length(nextline)));
elseif strncmpi(nextline, 'inDegrees', length('inDegrees'))
q.inDeg = nextline(findstr(nextline, '=')+1 : length(nextline));
end
nextline = fgetl(fid);
end
% Process the column labels.
nextline = fgetl(fid);
if (all(isspace(nextline))) % Blank line, so the next one must be the one containing the column labels
nextline = fgetl(fid);
end
a=textscan(nextline,'%s','MultipleDelimsAsOne',1,'Delimiter',sprintf('\t'));
q.labels = [a{:}]';
% Process the data.
% Note: transpose is needed since fscanf fills columns before rows.
% Text = fscanf(fid, '%c');fclose(fid);
Data = fscanf(fid, '%f', [q.nc, q.nr])';fclose(fid);
if any(size(Data)~=[q.nr, q.nc]) || (q.nr==0 && q.nc==1)
fid = fopen(fname, 'r');
Text = fscanf(fid,'%c');fclose(fid);
if any(strfind(Text,'1.#QNAN0000000'))
Text=strrep(Text,'1.#QNAN0000000','0.000000000000');
end
if any(strfind(Text,'-1.#IND00000000'))
Text=strrep(Text,'-1.#IND00000000','0.000000000000');
end
if any(strfind(Text,'-1.#INF00000000'))
Text=strrep(Text,'-1.#INF00000000','0.000000000000');
end
if any(strfind(Text,'242373565591244840000000000000000000000000000000000000000000000000000000000000000.000000000000'))
Text=strrep(Text,'242373565591244840000000000000000000000000000000000000000000000000000000000000000.000000000000','0.000000000000');
end
fid = fopen(fname, 'w');
fprintf(fid,'%s',Text);fclose(fid);
DataStart = strfind(Text,q.labels{end});
DataStart=DataStart-1+min(strfind(Text(DataStart:end),char(13)))+1;
DataText = Text(DataStart:end);
LettersFound = intersect(regexp(DataText,'\w'),regexp(DataText,'\D'));
if any(LettersFound)
warning('letters found in data :')
DataText(LettersFound)
end
DataText = strrep(DataText,DataText(LettersFound),'');
Data = textscan(DataText, '%f');
Data = Data{1};
if length(Data) == q.nc*q.nr
Data = reshape(Data,q.nc,q.nr)';
else
warning('not correct size of data specified in header, using size of Data vector and length of column labels')
q.nc = length(q.labels);
q.nr = length(Data)./q.nc;
Data = reshape(Data,q.nc,q.nr)';
end
if any(size(Data)~=[q.nr q.nc])
warning('File read error still'),beep
end
end
q.data = Data;
end
%%
function ColorSet=varycolor(NumberOfPlots)
% VARYCOLOR Produces colors with maximum variation on plots with multiple
% lines.
%
% VARYCOLOR(X) returns a matrix of dimension X by 3. The matrix may be
% used in conjunction with the plot command option 'color' to vary the
% color of lines.
%
% Yellow and White colors were not used because of their poor
% translation to presentations.
%
% Example Usage:
% NumberOfPlots=50;
%
% ColorSet=varycolor(NumberOfPlots);
%
% figure
% hold on;
%
% for m=1:NumberOfPlots
% plot(ones(20,1)*m,'Color',ColorSet(m,:))
% end
%Created by Daniel Helmick 8/12/2008
% Original source: MATLAB Central File Exchange
% URL: https://kr.mathworks.com/matlabcentral/fileexchange/21050-varycolor
%
% The following BSD-style license applies to the 'varycolor' function:
%
% Copyright (c) 2008, Daniel Helmick
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in the
% documentation and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%
%------------------------------------------------------------------
error(nargchk(1,1,nargin))%correct number of input arguements??
error(nargoutchk(0, 1, nargout))%correct number of output arguements??
%Take care of the anomolies
if NumberOfPlots<1
ColorSet=[];
elseif NumberOfPlots==1
ColorSet=[0 1 0];
elseif NumberOfPlots==2
ColorSet=[0 1 0; 0 1 1];
elseif NumberOfPlots==3
ColorSet=[0 1 0; 0 1 1; 0 0 1];
elseif NumberOfPlots==4
ColorSet=[0 1 0; 0 1 1; 0 0 1; 1 0 1];
elseif NumberOfPlots==5
ColorSet=[0 1 0; 0 1 1; 0 0 1; 1 0 1; 1 0 0];
elseif NumberOfPlots==6
ColorSet=[0 1 0; 0 1 1; 0 0 1; 1 0 1; 1 0 0; 0 0 0];
else %default and where this function has an actual advantage
%we have 5 segments to distribute the plots
EachSec=floor(NumberOfPlots/5);
%how many extra lines are there?
ExtraPlots=mod(NumberOfPlots,5);
%initialize our vector
ColorSet=zeros(NumberOfPlots,3);
%This is to deal with the extra plots that don't fit nicely into the
%segments
Adjust=zeros(1,5);
for m=1:ExtraPlots
Adjust(m)=1;
end
SecOne =EachSec+Adjust(1);
SecTwo =EachSec+Adjust(2);
SecThree =EachSec+Adjust(3);
SecFour =EachSec+Adjust(4);
SecFive =EachSec;
for m=1:SecOne
ColorSet(m,:)=[0 1 (m-1)/(SecOne-1)];
end
for m=1:SecTwo
ColorSet(m+SecOne,:)=[0 (SecTwo-m)/(SecTwo) 1];
end
for m=1:SecThree
ColorSet(m+SecOne+SecTwo,:)=[(m)/(SecThree) 0 1];
end
for m=1:SecFour
ColorSet(m+SecOne+SecTwo+SecThree,:)=[1 0 (SecFour-m)/(SecFour)];
end
for m=1:SecFive
ColorSet(m+SecOne+SecTwo+SecThree+SecFour,:)=[(SecFive-m)/(SecFive) 0 0];
end
end
end
%%
function plot_std_box(x, data, color)
data_mean = mean(data);
data_std = std(data);
xconf = [x x(end:-1:1)] ;
yconf = [data_mean+data_std data_mean(end:-1:1)-data_std(end:-1:1)];
plot(x,data_mean,'Color', color);
hold on;
p = fill(xconf,yconf, 'k');
p.FaceAlpha = 0.1;
p.EdgeColor = 'none';
hold on;
plot(x,data_mean,'Color',color, "LineWidth", 2);
hold off;
end