%% Analyzing Motion Data - Electric Playground
%% Initialize
clear;
clc;
%% Data Import
% Raw Data
filename = 'C:\Temp\Bewegungstest.csv';
delimiter = ',';
startRow = 2;
formatSpec = '%{yyyy-MM-dd HH:mm:ss}D%f%f%f%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
dt = dataArray{:, 1};
motionvalue = dataArray{:, 2};
byte0 = dataArray{:, 3};
byte1 = dataArray{:, 4};
byte2 = dataArray{:, 5};
byte3 = dataArray{:, 6};
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
% Aggregated Data 1 min
filename = 'C:\Temp\Bewegungstest_agg1min.csv';
delimiter = ',';
startRow = 2;
formatSpec = '%{yyyy-MM-dd HH:mm:ss}D%f%f%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
dt1min = dataArray{:, 1};
avg_motion1min = dataArray{:, 2};
std_motion1min = dataArray{:, 3};
var_motion1min = dataArray{:, 4};
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
% Aggregated Data 5 min
filename = 'C:\Temp\Bewegungstest_agg5min.csv';
delimiter = ',';
startRow = 2;
formatSpec = '%{yyyy-MM-dd HH:mm:ss}D%f%f%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
dt5min = dataArray{:, 1};
avg_motion5min = dataArray{:, 2};
std_motion5min = dataArray{:, 3};
var_motion5min = dataArray{:, 4};
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
% Aggregated Data 10 min
filename = 'C:\Temp\Bewegungstest_agg10min.csv';
delimiter = ',';
startRow = 2;
formatSpec = '%{yyyy-MM-dd HH:mm:ss}D%f%f%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
dt10min = dataArray{:, 1};
avg_motion10min = dataArray{:, 2};
std_motion10min = dataArray{:, 3};
var_motion10min = dataArray{:, 4};
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
% Aggregated Data 20 min
filename = 'C:\Temp\Bewegungstest_agg20min.csv';
delimiter = ',';
startRow = 2;
formatSpec = '%{yyyy-MM-dd HH:mm:ss}D%f%f%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
dt20min = dataArray{:, 1};
avg_motion20min = dataArray{:, 2};
std_motion20min = dataArray{:, 3};
var_motion20min = dataArray{:, 4};
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
%% plot data
subplot(3,1,1);
plot(dt1min,avg_motion1min,dt5min,avg_motion5min,dt10min,avg_motion10min,dt20min,avg_motion20min);
title('Average');
ylim([5.5E5, 7E5]);
subplot(3,1,2);
plot(dt1min,std_motion1min,dt5min,std_motion5min,dt10min,std_motion10min,dt20min,std_motion20min);
title('Standard Deviation');
ylim([0, 1E5]);
subplot(3,1,3);
plot(dt1min,var_motion1min,dt5min,var_motion5min,dt10min,var_motion10min,dt20min,var_motion20min);
title ('Variance');
ylim([0, 1E10]);