clc; clear all; close all;
names = {'lurch', 'antalgic', 'normal', 'steppage', 'trendelenburg', 'stiff_legged'};
num_names = length(names);
basePath = 'C:\Users\HAMSHAHRI\Documents\MATLAB\movementdisorderopen';
data_cells = cell(1, num_names);
filename = fullfile(basePath, sprintf('human%d_%s%d_SkeletonData%d.csv', h, names{n}, s, s));
data = readmatrix(filename);
if isempty(data_cells{n})
data_cells{n} = [data_cells{n}; data];
assignin('base', names{n}, data_cells{n});
eval([group_name '_X = [];']);
eval([group_name '_Y = [];']);
eval([group_name '_Z = [];']);
[nRows, nCols] = size(x);
x(:, i) = x(:, i) - x(:, 7);
eval([group_name '_X = [', group_name '_X; x(:,i).'';];']);
x(:, j) = x(:, j) - x(:, 8);
eval([group_name '_Y = [', group_name '_Y; x(:,j).'';];']);
x(:, k) = x(:, k) - x(:, 9);
eval([group_name '_Z = [', group_name '_Z; x(:,k).'';];']);
ClassMatrices = cell(1, length(names));
for classIdx = 1:length(names)
currentClass = names{classIdx};
eval(['LURCH_X_T = ' currentClass '_X'';']);
eval(['LURCH_Y_T = ' currentClass '_Y'';']);
eval(['LURCH_Z_T = ' currentClass '_Z'';']);
NewMatrix = [];numCols = size(LURCH_X_T, 2);
NewMatrix = [NewMatrix, col_X, col_Y, col_Z];
ClassMatrices{classIdx} = NewMatrix;
lurchData = ClassMatrices{1};antalgicData = ClassMatrices{2};normalData = ClassMatrices{3};
steppageData = ClassMatrices{4};trendelenburgData = ClassMatrices{5};stiff_leggedData = ClassMatrices{6};
lurchData(:, [4, 5, 6]) = [];antalgicData(:, [4, 5, 6]) = [];normalData(:, [4, 5, 6]) = [];
steppageData(:, [4, 5, 6]) = [];trendelenburgData(:, [4, 5, 6]) = [];stiff_leggedData(:, [4, 5, 6]) = [];
V_X1=[];Velocity_lurch=[];A_X1=[];ACC_lurch=[];time = lurch(:, 1);
velocity_X = diff(lurchData(:, i)) ./ diff(time);
velocity_X(end+1) = NaN;V_X1=[V_X1;velocity_X'];Velocity_lurch=V_X1';
acceleration_X = diff(velocity_X) ./ diff(time);
acceleration_X(end+1) = NaN;A_X1=[A_X1;acceleration_X'];ACC_lurch=A_X1';
V_X2=[];Velocity_antalgic=[];A_X2=[];ACC_antalgic=[];time = antalgic(:, 1);
velocity_X = diff(antalgicData(:, i)) ./ diff(time);
velocity_X(end+1) = NaN;V_X2=[V_X2;velocity_X'];Velocity_antalgic=V_X2';
acceleration_X = diff(velocity_X) ./ diff(time);
acceleration_X(end+1) = NaN;A_X2=[A_X2;acceleration_X'];ACC_antalgic=A_X2';
V_X3=[];Velocity_normal=[];A_X3=[];ACC_normal=[];time = normal(:, 1);
velocity_X = diff(normalData(:, i)) ./ diff(time);
velocity_X(end+1) = NaN;V_X3=[V_X3;velocity_X'];Velocity_normal=V_X3';
acceleration_X = diff(velocity_X) ./ diff(time);
acceleration_X(end+1) = NaN;A_X3=[A_X3;acceleration_X'];ACC_normal=A_X3';
V_X4=[];Velocity_steppage=[];A_X4=[];ACC_steppage=[];time = steppage(:, 1);
velocity_X = diff(steppageData(:, i)) ./ diff(time);
velocity_X(end+1) = NaN;V_X4=[V_X4;velocity_X'];Velocity_steppage=V_X4';
acceleration_X = diff(velocity_X) ./ diff(time);
acceleration_X(end+1) = NaN;A_X4=[A_X4;acceleration_X'];ACC_steppage=A_X4';
V_X5=[];Velocity_trendelenburg=[];A_X5=[];ACC_trendelenburg=[];time = trendelenburg(:, 1);
velocity_X = diff(trendelenburgData(:, i)) ./ diff(time);
velocity_X(end+1) = NaN;V_X5=[V_X5;velocity_X'];Velocity_trendelenburg=V_X5';
acceleration_X = diff(velocity_X) ./ diff(time);
acceleration_X(end+1) = NaN;A_X5=[A_X5;acceleration_X'];ACC_trendelenburg=A_X5';
V_X6=[];Velocity_stiff_legged=[];A_X6=[];ACC_stiff_legged=[];time = stiff_legged(:, 1);
velocity_X = diff(stiff_leggedData(:, i)) ./ diff(time);
velocity_X(end+1) = NaN;V_X6=[V_X6;velocity_X'];Velocity_stiff_legged=V_X6';
acceleration_X = diff(velocity_X) ./ diff(time);
acceleration_X(end+1) = NaN;A_X6=[A_X6;acceleration_X'];ACC_stiff_legged=A_X6';
positionData = [lurchData; antalgicData; normalData; steppageData; trendelenburgData; stiff_leggedData];
velocityData = [Velocity_lurch; Velocity_antalgic; Velocity_normal; Velocity_steppage; Velocity_trendelenburg; Velocity_stiff_legged];
accelerationData = [ACC_lurch; ACC_antalgic; ACC_normal; ACC_steppage; ACC_trendelenburg; ACC_stiff_legged];
X = cat(3, positionData, velocityData, accelerationData);
1*ones(size(lurchData, 1), 1);
2*ones(size(antalgicData, 1), 1);
3*ones(size(normalData, 1), 1);
4*ones(size(steppageData, 1), 1);
5*ones(size(trendelenburgData, 1), 1);
6*ones(size(stiff_leggedData, 1), 1)
randomIndices = randperm(numSamples);
numTrain = floor(0.7 * numSamples);
numTest = numSamples - numTrain;
XTrain = X(randomIndices(1:numTrain), :, :, :);
YTrain = Y(randomIndices(1:numTrain));
XTest = X(randomIndices(numTrain+1:end), :, :, :);
YTest = Y(randomIndices(numTrain+1:end));
XTrain = cat(4, XTrain, zeros(size(XTrain, 1), size(XTrain, 2), size(XTrain, 3), 1));
XTest = cat(4, XTest, zeros(size(XTest, 1), size(XTest, 2), size(XTest, 3), 1));
YTrain = cat(4, YTrain, zeros(size(YTrain, 1), 1, 1, 1));
YTest = cat(4, YTest, zeros(size(YTest, 1), 1, 1, 1));
imageInputLayer([72, 3, 3], 'Normalization', 'none', 'Name', 'Input');
imageInputLayer([72, 3, 3], 'Normalization', 'none', 'Name', 'Input')
convolution2dLayer(3, 32, 'Padding', 'same', 'Name', 'Conv1')
batchNormalizationLayer('Name', 'BatchNorm1')
reluLayer('Name', 'ReLU1')
averagePooling2dLayer(2, 'Stride', 1, 'Name', 'AvgPool1')
convolution2dLayer(3, 64, 'Padding', 'same', 'Name', 'Conv2')
batchNormalizationLayer('Name', 'BatchNorm2')
reluLayer('Name', 'ReLU2')
averagePooling2dLayer(2, 'Stride', 1, 'Name', 'AvgPool2')
convolution2dLayer(3, 128, 'Padding', 'same', 'Name', 'Conv3')
batchNormalizationLayer('Name', 'BatchNorm3')
reluLayer('Name', 'ReLU3')
fullyConnectedLayer(6, 'Name', 'FC')
softmaxLayer('Name', 'Softmax')
classificationLayer('Name', 'Output')
options = trainingOptions('sgdm', ...
'InitialLearnRate', 0.01, ...
'Plots', 'training-progress');
net = trainNetwork(XTrain, YTrain, layers, options);
YPred = classify(net, XTest);
accuracy = sum(YPred == YTest) / numel(YTest);
disp(['Accuracy: ', num2str(accuracy)]);
edges = [3 4; 3 21; 21 5; 5 6; 6 7; 7 8; 8 22; 7 23;
21 9; 9 10; 10 11; 11 12;11 25; 12 24; 21 2; 2 1;
1 13; 13 14; 14 15; 15 16; 1 17;17 18; 18 19; 19 20];
G = graph(edges(:,1), edges(:,2));
position=[lurchData;antalgicData;normalData;steppageData;...
trendelenburgData;stiff_leggedData];
velocityData = [ Velocity_lurch;Velocity_antalgic;Velocity_normal;Velocity_steppage;...
Velocity_trendelenburg;Velocity_stiff_legged];
accelerationData = [ACC_lurch;ACC_antalgic;ACC_normal;ACC_steppage;...
ACC_trendelenburg;ACC_stiff_legged];
positionInput = imageInputLayer([72, 1, 1], 'Normalization', 'none', 'Name', 'PositionyInput');
velocityInput = imageInputLayer([72, 1, 1], 'Normalization', 'none', 'Name', 'VelocityInput');
accelerationInput = imageInputLayer([72, 1, 1], 'Normalization', 'none', 'Name', 'AccelerationInput');
positionNorm = batchNormalizationLayer('Name', 'PositionBatchNorm');
velocityNorm = batchNormalizationLayer('Name', 'VelocityBatchNorm');
accelerationNorm = batchNormalizationLayer('Name', 'AccelerationBatchNorm');
stgcnLayer1_position = stgcnLayer(6, 64, 'Name', 'ST-GCNLayer1-Position');
stgcnLayer2_position = stgcnLayer(64, 48, 'Name', 'ST-GCNLayer2-Position');
attentionLayer1_position = attentionLayer('Name', 'AttentionLayer1-Position');
positionWithAttention = custom_attention_layer1(positionInput, 3);
stgcnLayer3_position = stgcnLayer(64, 16, 'Name', 'ST-GCNLayer3-Position');
attentionLayer2_position = attentionLayer('Name', 'AttentionLayer2-Position');
stgcnLayer1_velocity = stgcnLayer(6, 64, 'Name', 'ST-GCNLayer1-Velocity');
stgcnLayer2_velocity = stgcnLayer(64, 48, 'Name', 'ST-GCNLayer2-Velocity');
attentionLayer1_velocity = attentionLayer('Name', 'AttentionLayer1-Velocity');
velocityWithAttention = custom_attention_layer(velocityInput, 3);
stgcnLayer3_velocity = stgcnLayer(64, 16, 'Name', 'ST-GCNLayer3-Velocity');
attentionLayer2_velocity = attentionLayer('Name', 'AttentionLayer2-Velocity');
velocityWithAttention = custom_attention_layer(velocityInput, 3);
stgcnLayer1_acceleration = stgcnLayer(6, 64, 'Name', 'ST-GCNLayer1-ACC');
stgcnLayer2_acceleration = stgcnLayer(64, 48, 'Name', 'ST-GCNLayer2-ACC');
attentionLayer1_acceleration = attentionLayer('Name', 'AttentionLayer1-ACC');
accelerationWithAttention = custom_attention_layer(accelerationInput, 3);
stgcnLayer3_acceleration = stgcnLayer(64, 16, 'Name', 'ST-GCNLayer3-ACC');
attentionLayer2_acceleration = attentionLayer('Name', 'AttentionLayer2-ACC');
accelerationWithAttention = custom_attention_layer(accelerationInput, 3);
concatLayer = concatenationLayer(1, 2, 3, 'Name', 'Concatenate');
stgcnLayer4 = stgcnLayer(48, 64, 'Name', 'ST-GCNLayer4');
attentionLayer = attentionLayer('Name', 'AttentionLayer');
stgcnLayer5 = stgcnLayer(64, 128, 'Name', 'ST-GCNLayer5');
attentionLayer2 = attentionLayer('Name', 'AttentionLayer2');
gapLayer = globalAveragePooling2dLayer('Name', 'GAP');
fcLayer = fullyConnectedLayer(6, 'Name', 'FC');
attentionLayer1_acceleration
attentionLayer2_acceleration
lgraph_position = layerGraph(layers_position);
lgraph_velocity = layerGraph(layers_velocity);
lgraph_acceleration = layerGraph(layers_acceleration);
options = trainingOptions('sgdm', ...
'InitialLearnRate', 0.01, ...
'Plots', 'training-progress');
net = trainNetwork(X_train, Y_train, lgraph, options);
YPred = classify(net, XTest);
accuracy = sum(YPred == YTest) / numel(YTest);
disp(['Accuracy: ', num2str(accuracy)]);