generateTrainingDataFromCSV.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. function [data, labels] = generateTrainingDataFromCSV(file, offset, len, framesize, frameoffset, maxFrameShift)
  2. % generateTrainingDataFromCSV(file, offset, length, framesize, frameoffset, maxFrameShift)
  3. % generating frames with fixed number of samples, parametrized by framesize
  4. % and frameoffset. frameoffset determines the number of sample to include
  5. % before the actual touch event
  6. % maxFrameShift determines the jitter in both directions when determining
  7. % the origin of the frame based on the postion of the touch event
  8. % finally offset and lenght defined in percent (in 100 percent) of the dataset to be used
  9. % offset + lenght cannot be larger than 100
  10. %
  11. % Returns two matrices
  12. % data:
  13. % framesize x 6 x N (Where N denotes the total number of samples)
  14. %
  15. % labels:
  16. % 2 x N
  17. X_COORD = 10;
  18. Y_COORD = 11;
  19. % find the action down events
  20. indices = find(file(:,9) == 1);
  21. events = length(indices);
  22. offset_index = floor(length(indices) * offset / 100) + 1;
  23. len_index = floor(length(indices) * len / 100) + offset_index - 1;
  24. indices = indices(offset_index:len_index);
  25. fprintf("%d touch events in %d sensor samples, using %d:%d\n", events, size(file, 1),offset_index, len_index);
  26. data = []; % zeros(framesize, 6, size(indices,1) * (2 * maxFrameShift + 1));
  27. labels = []; % zeros(2, size(indices,1) * (2 * maxFrameShift + 1));
  28. % create the frames and labels
  29. for i = -maxFrameShift:maxFrameShift
  30. data = cat(3, data, getSamplesFromData(file, indices + i, framesize, frameoffset));
  31. labels = cat(2, labels, file(indices, X_COORD:Y_COORD)');
  32. end
  33. fprintf("created %d frames out of %d touch events\n", size(data, 3), length(indices));
  34. labels = bsxfun(@minus, labels, [0; 699])
  35. end
  36. function samples = getSamplesFromData(file, indices, framesize, frameoffset)
  37. GYRO_X = 2;
  38. GYRO_Y = 3;
  39. GYRO_Z = 4;
  40. ACC_X = 6;
  41. ACC_Y = 7;
  42. ACC_Z = 8;
  43. samples = zeros(framesize, 6, size(indices,1));
  44. dataidx = 1;
  45. for i = indices'
  46. % creates a (framesize x 6 x batchsize) tensor
  47. if (i-frameoffset+framesize < size(file, 1) && i - frameoffset > 0)
  48. samples(:, :, dataidx) = cat(2, file(i-frameoffset:i-frameoffset+framesize-1,GYRO_X:GYRO_Z), file(i-frameoffset:i-frameoffset+framesize-1,ACC_X:ACC_Z));
  49. dataidx = dataidx + 1;
  50. end
  51. end
  52. end