PageRenderTime 5ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/details/smooth.php

http://showslow.googlecode.com/
PHP | 53 lines | 35 code | 7 blank | 11 comment | 10 complexity | 6e69b2f86e7096b121ff08a53ceb04af MD5 | raw file
  1. <?php
  2. #
  3. # Smooths a series of data rows
  4. #
  5. # @rows - reference to array of rows each of which is an associative array of different values to smooth
  6. # @to_smooth - array of metrics to smooth
  7. # @size = distance between the value and the boundary - full range will be from x - size to x + size
  8. #
  9. # Return: rows are updated inplace - nothing is returned
  10. #
  11. function smooth(&$rows, $to_smooth) {
  12. global $smoothDistance;
  13. $total = count($rows);
  14. for ($i = 0; $i <= $total - 1; $i++) {
  15. # from and to define windown boudaries
  16. if ($smoothDistance * 2 + 1 > $total) { # window is bigger then the array
  17. $from = 0;
  18. $to = $total - 1;
  19. } else if ($i < $smoothDistance) {
  20. $from = 0; # window starts at the beginnning of array
  21. $to = $smoothDistance * 2;
  22. } else if ($i > $total - 1 - $smoothDistance) {
  23. $to = $total - 1; # widnow ends at the end of array
  24. $from = $total - $smoothDistance * 2 - 1;
  25. } else {
  26. $from = $i - $smoothDistance;
  27. $to = $i + $smoothDistance;
  28. }
  29. # j iterates through the window
  30. for ($j = $from; $j <= $to; $j++) {
  31. foreach ($to_smooth as $metric) {
  32. if (!array_key_exists($metric.'_avg', $rows[$i])) {
  33. $rows[$i][$metric.'_avg'] = 0;
  34. }
  35. $rows[$i][$metric.'_avg'] += $rows[$j][$metric];
  36. }
  37. }
  38. }
  39. $windowsize = ($smoothDistance * 2 + 1 > $total) ? $total : ($smoothDistance * 2 + 1);
  40. for ($i = 0; $i <= $total - 1; $i++) {
  41. foreach ($to_smooth as $metric) {
  42. $rows[$i][$metric] = sprintf('%.2f', $rows[$i][$metric.'_avg'] / $windowsize);
  43. unset($rows[$i][$metric.'_avg']);
  44. }
  45. }
  46. }