
clear

source("training.m");

# 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 C = getC(A, sensor)
  C = [];
  for i = 1:numel(sensor)
    c = A * (sensor{i});
    C = [c C];
  end
end

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

function cls = getClass(name)
  cls = {};
  cls.name = name;
  cls.samples = {};
end

function m = getM(samples)
  size = length(samples{1});  # length of the first entry(all have the same size)
  m = zeros(size,1);          # allocate memory
  for i = 1 : length(samples)
    m += samples{i};
  end
  m /= length(samples);
end

function R = getR(samples)
  size = length(samples{1});  # length of the first entry(all have the same size)
  R = zeros(size,size);       # allocate memory
  for i = 1 : length(samples)
    R += samples{i} * samples{i}';
  end
  R /= length(samples);
end

function Q = getQ1(classes)
  samples = {}
  for i = 1 : length(classes)
    samples = [samples classes{i}.samples];
  end
  m = getM(samples);
  R = getR(samples);
  Q = R - m*m';
end

function Q = getQ3(classes)
  size = length(classes{1}.samples{1});
  Q = zeros(size,size);
  for i = 1 : length(classes)
    m = getM(classes{i}.samples);
    R = getR(classes{i}.samples);
    Q += ( R - m*m' );
  end
  Q /= length(classes);
end

function Q = getQ2(classes)
  
  numClasses = length(classes);
  size = length(classes{1}.samples{1});
  
  sumR = zeros(size, size);
  sumM = zeros(size, size);
  
  for k = 1 : numClasses
    R = getR(classes{k}.samples);
    sumR += R;
  end
  
  for k = 2 : numClasses
    for l = 1 : k-1
      mk = getM(classes{k}.samples);
      ml = getM(classes{l}.samples);
      sumM += (mk*ml') + (ml*mk');
    end
  end
  
  sumM / (numClasses*(numClasses-1))
  
  Q = (sumR / numClasses) - (sumM / (numClasses*(numClasses-1)));

end

function plotC(C, args)
  plot3(C(1,:)', C(2,:)', C(3,:)', args);
end

function show(A)

  hold on;
  samples = 6;
    
  data = getSamplesForClass("forwardbend", samples, 1050, 0.8);
  C = getC(A, data);
  plotC(C, "-1");
  
  data = getSamplesForClass("kneebend", samples, 1050, 0.8);
  C = getC(A, data);
  plotC(C, "-2");

  data = getSamplesForClass("pushups", samples, 1050, 0.8);
  C = getC(A, data);
  plotC(C, "-3");

  data = getSamplesForClass("situps", samples, 1050, 0.8);
  C = getC(A, data);
  plotC(C, "-4");

  data = getSamplesForClass("jumpingjack", samples, 1050, 0.8);
  C = getC(A, data);
  plotC(C, "-5");
  
  hold off;
  
end

function avg = getAvg(classes)
  
  size = length(classes{1}.samples{1});
  avg = zeros(size,1);
  cnt = 0;
  
  for i = 1:numel(classes)
    for j = 1:numel(classes{i}.samples)
      avg += classes{i}.samples{j};
      ++cnt;
    end
  end
  
  avg /= cnt;

end

function res = removeAvg(classes)
  
  size = length(classes{1}.samples{1});
  avg = getAvg(classes);

  for i = 1:numel(classes)
    for j = 1:numel(classes{i}.samples)
      classes{i}.samples{j} = classes{i}.samples{j} - avg;
    end
  end
  
  res = classes;

end

classes = {};
classes{1}.samples = {};
classes{2}.samples = {};
classes{3}.samples = {};

classes{1}.samples = [classes{1}.samples [1;2;3;4]];
classes{1}.samples = [classes{1}.samples [1;2;4;5]];

classes{2}.samples = [classes{2}.samples [3;8;9;0]];
classes{2}.samples = [classes{2}.samples [3;7;5;0]];

classes{3}.samples = [classes{3}.samples [9;1;3;2]];
classes{3}.samples = [classes{3}.samples [7;3;3;2]];

Q1 = getQ1(classes);
Q2 = getQ2(classes);
Q3 = getQ3(classes);

Q1
Q2
Q3

# get training dataset
#classes = getTrainData();
#classes = removeAvg(classes);

#Q1 = getQ1(classes);
#[eVec, eVal] = eigs(Q1, 3, 'lm');

#Q2 = getQ2(classes);
#[eVec, eVal] = eigs(Q2, 3, 'lm');

#Q3 = getQ1(classes);
#[eVec, eVal] = eigs(Q3, 3, 'sm');

#avg = getAvg(classes);

#A = eVec';
#show(A, avg);

