% ----------------------------------------------------------------------------------
%
% Copyright (c) 2024, Bastian Latsch, Technische Universität Darmstadt.
% Some Rights Reserved.
%
% Except where otherwise noted, this work is licensed under Creative Commons Attribution 4.0 International.
% To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/
% 
% You are free to:
%    * Share — copy and redistribute the material in any medium or format for any purpose.
%    * Adapt — remix, transform, and build upon the material for any purpose, even commercially.
% 
% Under the following terms::
%    * Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made.
%    
% For any reuse or distribution, you must make clear to others the license terms of this work.
% The best way to give credit is with a reference to the corresponding publication.
% Any of the above conditions can be waived if you get permission from the copyright holder.
% No warranties are given.
%
% ----------------------------------------------------------------------------------

%% Read the data from HDF5 formatted file

% File containing data
h5file = 'data.hdf5';

% Display file description
disp(h5readatt(h5file, '/', 'description'))

% Use the provided data structure
fe_sensors = {'heel', 'lateral', 'medial', 'toe'}; % Insole
grf_components = {'vertical_right'}; % Treadmill
speeds = 50:25:150;

% Initialize cell array to hold data: row = speed, col = sensor
data = cell([size(speeds, 2), size(fe_sensors, 2) + size(grf_components, 2)]);

% Get ferroelectret insole data for all sensors and all speeds
for j = 1:size(speeds, 2)
    for i = 1:size(fe_sensors, 2)

        % Create path for this speed and this sensor
        thisPath = sprintf('/%s/%s/v%03i', 'insole', fe_sensors{i}, speeds(j)); % e.g. '/insole/heel/v050'

        % Get data from this path
        data{j, i} = h5read(h5file, thisPath);
    end
end

% Get ground reaction force (GRF) treadmill data for all speeds
for j = 1:size(speeds, 2)
    for i = 1:size(grf_components, 2)

        % Create path for this speed and this sensor
        thisPath = sprintf('/%s/%s/v%03i', 'treadmill', grf_components{i}, speeds(j)); % e.g. '/treadmill/vertical/v050'

        % Get data from this path
        data{j, i + size(fe_sensors, 2)} = h5read(h5file, thisPath);
    end
end

%% Calculate mean gait cycles

data_mean = cellfun(@(c) mean(c), data, "UniformOutput",false);
data_std = cellfun(@(c) std(c), data, "UniformOutput",false);

%% Sample plot with the means

colors = [ ...
    0.00 0.51 0.80; % Blue
    0.45 0.06 0.56; % Purple
    0.90 0.00 0.08; % Red
    0.98 0.73 0.08; % Yellow
    0.41 0.56 0.23  % Green
];

% Figure with tiles for each sensor
f = figure;
t = tiledlayout(size(data,2), 1, "TileSpacing","tight", "Padding","tight");

% Ferroelectret insole
for i = 1:size(fe_sensors, 2)
    nexttile;
    hold on;

    for j = 1:size(speeds, 2)
        x = linspace(0,100,size(data{j,i},2));
        plot(x, data_mean{j,i}, 'Color',colors(j,:))
        ylabel(sprintf('%s (V)', fe_sensors{i}));
    end
end

% GRF treadmill
i = size(data, 2);

nexttile;
hold on;

for j = 1:size(speeds, 2)
    x = linspace(0,100,size(data{j,i},2));
    plot(x, data_mean{j,i}, 'Color',colors(j,:))
    ylabel(sprintf('%s (N)', 'vertical GRF'));
end

% Legend, x label
leg = legend(compose('v%03i', speeds));
xlabel("Time (% stride)");
