preliminary_segment.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. function [ prelIntArray ] = preliminary_segment(data, metadata, attributes, labelVec, typeOfMotion, t_wind, t_overlap, eta_p)
  2. %This funcation performs Larsson's preliminary segmentation
  3. % @data to use
  4. % @metadata of the ARFF data
  5. % @attributes describing the data
  6. % @labelVec label vector to consider for intervals with value 0 (unassigned)
  7. % @typeOfMotion gets the values 1 -> eye FOV, 2 -> eye+head, 3 -> head
  8. %
  9. % @t_wind is window size in us, default is 22000
  10. % @t_overlap is overlp size in us, default is 6000
  11. % @eta_p - threshold for average p-value of Reyleigh test for each sample,
  12. % default value (from paper) is 0.01
  13. %
  14. % @prelIntArray preliminary segmentation of labelVec intervals
  15. if nargin < 3
  16. error('At least 3 arguments are needed');
  17. end
  18. if nargin < 4
  19. t_wind = 22000; % us
  20. end
  21. if nargin < 5
  22. t_overlap = 6000; % us
  23. end
  24. if nargin < 6
  25. eta_p = 0.01;
  26. end
  27. % get position of attributes in data
  28. timeIndex = GetAttPositionArff(attributes, 'time');
  29. [eyeFovVec, eyeHeadVec, headVec] = GetCartVectors(data, metadata, attributes);
  30. if (typeOfMotion == 1)
  31. vecList = eyeFovVec;
  32. elseif (typeOfMotion == 2)
  33. vecList = eyeHeadVec;
  34. elseif (typeOfMotion == 3)
  35. vecList = headVec;
  36. else
  37. error('Uknown motion');
  38. end
  39. moveId = 0; % unassigned
  40. intArray = GetIntervalsIndex(labelVec, moveId);
  41. % calculate direction of motion as difference between vector positions in 3D
  42. dirList = zeros(size(vecList));
  43. for i=1:size(intArray,1)
  44. startIndex = intArray(i,1);
  45. endIndex = intArray(i,2);
  46. for j=startIndex:endIndex-1
  47. dirList(j,:) = vecList(j+1,:) - vecList(j,:);
  48. if (sum(dirList(j,:)) == 0)
  49. if (j > startIndex)
  50. dirList(j,:) = dirList(j-1,:);
  51. end
  52. else
  53. % nomalize directions to unit vectors
  54. dirList(j,:) = dirList(j,:)/norm(dirList(j,:));
  55. end;
  56. end
  57. % assing same value to the last entry as the penultimate
  58. if (endIndex-startIndex > 1)
  59. dirList(endIndex,:) = dirList(endIndex-1,:);
  60. end
  61. end
  62. % calculate P mean
  63. Pmean = zeros(1, size(data,1));
  64. N = zeros(1, size(data,1));
  65. for i=1:size(intArray,1)
  66. startIndex = intArray(i,1);
  67. endIndex = intArray(i,2);
  68. j=startIndex;
  69. startInterval = startIndex;
  70. newStartInterval = -1;
  71. while (j<=endIndex)
  72. if (data(j,timeIndex) > data(startInterval,timeIndex) + t_overlap && newStartInterval < 0)
  73. newStartInterval = j;
  74. end
  75. if (data(j,timeIndex) < data(startInterval,timeIndex) + t_wind)
  76. j = j + 1; % just move to next
  77. else
  78. % get r for all values
  79. p = Rtest(dirList(startInterval:j-1,:));
  80. Pmean(startInterval:j-1) = Pmean(startInterval:j-1) + p;
  81. N(startInterval:j-1) = N(startInterval:j-1) + 1;
  82. startInterval = newStartInterval;
  83. j = newStartInterval;
  84. newStartInterval = -1;
  85. end
  86. end
  87. % get last entries of intersaccadic interval
  88. p = Rtest(dirList(startInterval:endIndex,:));
  89. Pmean(startInterval:j-1) = Pmean(startInterval:endIndex) + p;
  90. N(startInterval:j-1) = N(startInterval:endIndex) + 1;
  91. end
  92. Pmean = Pmean./N;
  93. % convert Pmean to 0 1 array
  94. Pmean(Pmean(:) < eta_p) = 0;
  95. Pmean(Pmean(:) ~= 0) = 1;
  96. prelIntArray = zeros(0,2);
  97. for i=1:size(intArray,1)
  98. startIndex = intArray(i,1);
  99. endIndex = intArray(i,2);
  100. intStart = startIndex;
  101. for j=startIndex+1:endIndex
  102. if (Pmean(j) ~= Pmean(j-1))
  103. prelIntArray = [prelIntArray; intStart j-1];
  104. intStart = j;
  105. end
  106. end
  107. % add last part of interval
  108. if (intStart ~= endIndex)
  109. prelIntArray = [prelIntArray; intStart endIndex];
  110. end
  111. end
  112. % Function that returns mean Reyleigh-R
  113. %
  114. % @l_velList nomalized vector list for 3D points in 360 deg videos
  115. function R = meanR(l_vecList)
  116. R = sum(l_vecList,1);
  117. R = norm(R) / size(l_vecList,1);
  118. end
  119. % This function returns the p-value calculated from the Reyleigh test
  120. function pval = Rtest(l_vecList)
  121. r = meanR(l_vecList);
  122. n = size(l_vecList,1);
  123. R = n*r;
  124. pval = exp(sqrt(1 + 4*n + 4*(n^2 - R^2)) - (1 + 2*n));
  125. end
  126. end