% This function is part of the NMSM Pipeline, see file for full license.
%
% Plot muscle activations for one trial resulting from Neural Control
% Personalization from synergy weights and synergy commands output files.
%
% (string, string, string, double, double) -> (None)
% Plot NCP muscle activations from weights and commands files.
function plotNeuralControlPersonalizationActivations(weightsFile, ...
commandsFile, mtpActivationsFile, figureWidth, figureHeight)
% Define number of
if nargin < 4
figureWidth = 8;
end
if nargin < 5
figureHeight = 8;
end
figureSize = figureWidth * figureHeight;
import org.opensim.modeling.Storage
weightsStorage = Storage(weightsFile);
muscleNames = getStorageColumnNames(weightsStorage);
synergyWeights = storageToDoubleMatrix(weightsStorage);
commandsStorage = Storage(commandsFile);
time = findTimeColumn(commandsStorage);
synergyCommands = storageToDoubleMatrix(commandsStorage);
muscleActivations = synergyWeights * synergyCommands;
if isstring(mtpActivationsFile) || ischar(mtpActivationsFile)
mtpStorage = Storage(mtpActivationsFile);
mtpMuscleNames = getStorageColumnNames(mtpStorage);
mtpActivations = storageToDoubleMatrix(mtpStorage);
else
mtpMuscleNames = "";
end
figureNumber = 1;
subplotNumber = 1;
hasLegend = false;
figure(1)
for i = 1:size(muscleActivations, 1)
if i > figureSize * figureNumber
figureNumber = figureNumber + 1;
figure(figureNumber)
subplotNumber = 1;
hasLegend = false;
end
subplot(figureHeight, figureWidth, subplotNumber)
plot(time, muscleActivations(i, :), 'LineWidth', 2)
mtpIndex = find(muscleNames(i) == mtpMuscleNames);
if ~isempty(mtpIndex)
hold on
plot(time, mtpActivations(mtpIndex, :), 'LineWidth', 2);
if ~hasLegend
legend("NCP Results", "Previous Activations")
hasLegend = true;
end
hold off
end
title(strrep(muscleNames(i), "_", " "))
ylim([0 1])
subplotNumber = subplotNumber + 1;
end
end