
clear

# load data

#run /mnt/firma/kunden/HandyGames/daten/forwardbend/forwardbend_gl_3_subject_1_left.txt.m


#run /mnt/firma/kunden/HandyGames/daten/kneebend/kneebend_gl_0_subject_0_right.txt.m


function win = window(vec, pos)
  pos = pos / 10;
  win = vec(pos-25:pos+25-1,:);
end

function flat = flatten(win)
  flat = reshape(win, rows(win)*columns(win), 1);
end

function Q = getQ(gyro)
  
  R = zeros(150,150);
  m = zeros(150, 1);
  cnt = 0;

  for i = 1500:50:15000
    win = window(gyro, i);
    win = flatten(win);
    R += win*win';
    m += win;
    cnt = cnt + 1;
  end
  
  R = R / cnt;
  m = m / cnt;
  Q = R - m * m';
  
end

function C = getC(A, gyro)
  C = [];
  for i = 1500:100:6000
    win = window(gyro, i);
    win = flatten(win);
    c = A * win;
    C = [c C];
  end
end


run /mnt/firma/kunden/HandyGames/daten/forwardbend/forwardbend_gl_3_subject_1_left.txt.m
Qforward = getQ(Gyro);

run /mnt/firma/kunden/HandyGames/daten/kneebend/kneebend_gl_0_subject_0_right.txt.m
Qkneebend = getQ(Gyro);

run /mnt/firma/kunden/HandyGames/daten/pushups/pushups_gl_8_subject_4_right.txt.m
Qpushups = getQ(Gyro);

run /mnt/firma/kunden/HandyGames/daten/situps/situps_gl_12_subject_1_left.txt.m
Qsitups = getQ(Gyro);

run /mnt/firma/kunden/HandyGames/daten/jumpingjack/jumpingjack_gl_5_subject_3_left.txt.m
Qjumpingjack = getQ(Gyro);

Q = (Qforward + Qkneebend + Qpushups + Qsitups + Qjumpingjack) / 5;


[eVec, eVal] = eigs(Q,2,'sm');
A = eVec';

#function [P2,D2] = sortEig(P, D, mode) 
 # D2 = diag(sort(diag(D), mode));
 # [c,ind] = sort(diag(D), mode);
 # P2=P(:,ind);
#end

#[eVec, eVal] = eig(Q);
#[eVec, eVal] = sortEig(eVec, eVal, 'descend');
#A = eVec';







hold on;

run /mnt/firma/kunden/HandyGames/daten/forwardbend/forwardbend_gl_3_subject_1_left.txt.m
C = getC(A, Gyro);
plot(C(1,:)',C(2,:)', "+1")

run /mnt/firma/kunden/HandyGames/daten/kneebend/kneebend_gl_0_subject_0_right.txt.m
C = getC(A, Gyro);
plot(C(1,:)',C(2,:)', ".2")

run /mnt/firma/kunden/HandyGames/daten/pushups/pushups_gl_8_subject_4_right.txt.m;
C = getC(A, Gyro);
plot(C(1,:)',C(2,:)', ".3")

run /mnt/firma/kunden/HandyGames/daten/situps/situps_gl_12_subject_1_left.txt.m
C = getC(A, Gyro);
plot(C(1,:)',C(2,:)', ".4")

run /mnt/firma/kunden/HandyGames/daten/jumpingjack/jumpingjack_gl_5_subject_3_left.txt.m
C = getC(A, Gyro);
plot(C(1,:)',C(2,:)', ".5")

hold off;