% Gait Variability Analysis 
% Script created for BAP students 2020
% Iris Hagoort
% April 2020

% Input: needs mat file which contains all raw accelerometer data
% Input: needs excel file containing the participant information including
% leg length. 

%% Clear and close;
clear;
close all;

%% Load data
load('Phyphoxdata.mat'); % loads accelerometer data, is stored in struct with name AccData
load('ExcelInfo.mat');
Participants = fields(AccData);

%% Settings
FS = 100; % sample frequency
LegLengths = excel.data.GeneralInformation(:,5); % leglength info is in 5th column
LegLengthsM = LegLengths/100; % convert to m

%% Calculate parameters;
for i = 1:  length(Participants);
tic;   
    LegLength = LegLengthsM(i);
    WalkingConditions = fields(AccData.([char(Participants(i))]));
    
    for j = 1: length(WalkingConditions);
        
        if strcmp(char(WalkingConditions(j)),'Treadmill')
            
            SubConditions = fieldnames(AccData.([char(Participants(i))]).([char(WalkingConditions(j))]));
            
            for k = 1: length(SubConditions);
               inputData = AccData.([char(Participants(i))]).([char(WalkingConditions(j))]).([char(SubConditions(k))]);
               WindowLength = FS*10;
               ApplyRealignment = true;
               ApplyRemoveSteps = true;
               [ResultStruct] = GaitOutcomesTrunkAccFuncIH(inputData,FS,LegLength,WindowLen,ApplyRealignment,ApplyRemoveSteps);
                OutcomesAcc.([char(Participants(i))]).([char(WalkingConditions(j))]).([char(SubConditions(k))]) = ResultStruct;
            end
            
        elseif strcmp(char(WalkingConditions(j)),'Balance') || strcmp(char(WalkingConditions(j)),'TwoMWT') 
              disp('Files are not used for current analysis');
            
        elseif strcmp(char(WalkingConditions(j)),'InsideStraight')
            inputData = AccData.([char(Participants(i))]).([char(WalkingConditions(j))]);
            ApplyRealignment = true;
            ApplyRemoveSteps = false; % don't remove steps for the straight conditions
            % function specific for the walking conditions containing a lot
            % of turns
            [ResultStruct] = GaitVariabilityAnalysisIH_WithoutTurns(inputData,FS,LegLength,ApplyRealignment,ApplyRemoveSteps);
            OutcomesAcc.([char(Participants(i))]).([char(WalkingConditions(j))]) = ResultStruct;
            
        else
            inputData = AccData.([char(Participants(i))]).([char(WalkingConditions(j))]);
            ApplyRealignment = true;
            ApplyRemoveSteps = true;
            WindowLen = FS*10;
            [ResultStruct] = GaitOutcomesTrunkAccFuncIH(inputData,FS,LegLength,WindowLen,ApplyRealignment,ApplyRemoveSteps)
            OutcomesAcc.([char(Participants(i))]).([char(WalkingConditions(j))]) = ResultStruct;
        end
    end
    
 toc;  
end

% Save struct as .mat file
save('GaitVarOutcomes30pril.mat', 'OutcomesAcc');



