def CalculateRollingMax(raw_scores, total_samples, window_samples):
  """Calculates a rolling maximum across the array, in windows of the same size.

  The scores returned from open-cv matchTemplate are calculated as if each
  value in the array matches with the first position in the window. Here,
  for each position in the original array, scores across the window are
  aggregated, to take into account the match with the first, second, third, ...
  position in the window. The aggegation method is max(), it performed better
  than avg().
  Args:
    raw_scores: array of scores calculated with open-cv matchTemplate function.
      It should have length total_samples - window_samples + 1.
    total_samples: total number of samples of the original data.
    window_samples: number of samples in the window.

  Returns:
    Array of scores calculated aggregating with the maximum.
  """
  scores = []

  left = 0
  current_max = 0
  for right in range(total_samples):
    if right < len(raw_scores):
      if raw_scores[right] > current_max:
        current_max = raw_scores[right]

    current_size = right - left + 1
    if current_size > window_samples:
      pop_value = raw_scores[left]
      left += 1

      if pop_value >= current_max:
        current_max = 0
        for c in range(left, min(right + 1, len(raw_scores))):
          if raw_scores[c] > current_max:
            current_max = raw_scores[c]
    score = current_max

    scores.append(score)

  return scores