generateTrainingDataFromCSV.m 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. function [data, labels] = generateTrainingDataFromCSV(file, framesize, frameoffset, maxFrameShift)
  2. % generateTrainingDataFromCSV(file, 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. X_COORD = 10;
  9. Y_COORD = 11;
  10. % read the csv and start in the second row, first column
  11. sizeData = size(file);
  12. fprintf("read %d sensor samples\n", sizeData(1));
  13. % find the action down events
  14. indices = find(file(:,9) == 1);
  15. data = []; % zeros(framesize, 6, size(indices,1) * (2 * maxFrameShift + 1));
  16. labels = []; % zeros(2, size(indices,1) * (2 * maxFrameShift + 1));
  17. % create the frames and labels
  18. for i = -maxFrameShift:maxFrameShift
  19. data = cat(3, data, getSamplesFromData(file, indices + i, framesize, frameoffset));
  20. labels = cat(2, labels, file(indices, X_COORD:Y_COORD)');
  21. end
  22. fprintf("created %d frames out of %d touch events\n", size(data, 3), length(indices));
  23. end
  24. function samples = getSamplesFromData(file, indices, framesize, frameoffset)
  25. GYRO_X = 2;
  26. GYRO_Y = 3;
  27. GYRO_Z = 4;
  28. ACC_X = 6;
  29. ACC_Y = 7;
  30. ACC_Z = 8;
  31. samples = zeros(framesize, 6, size(indices,1));
  32. dataidx = 1;
  33. for i = indices'
  34. % creates a (framesize x 6 x batchsize) tensor
  35. if (i-frameoffset+framesize < size(file, 1) && i - frameoffset > 0)
  36. 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));
  37. dataidx = dataidx + 1;
  38. end
  39. end
  40. end