PHP - Find turning points in array data (high and low) based on range

Viewed 439
$data = array(5,0,15,20,22,14,13,15,12,22,40,25);

enter image description here

Hi , i want to traverse the data points above and find the turning points based on a range.

The way i'm tackling it so far is simply taking the $array[$i] - $array[$i-1] , and if the absolute difference is greater than the range - i'm taking it as a turning point . however - the logic is flawed as if it moved slightly up and then back down - it breaks the cycle.

enter image description here

The 3 down values should have been enough to make X , a turning point downwards , but because they individually do not meet the range - they are discarded .

Any solutions ?

if($diff >= 0)
{
  $diff_up = $diff_up + $diff;
}
else
{
  $diff_down = $diff_down + abs($diff);
}


if((($diff_up-$diff_down) >=$range) && ($pivot_type != "UP"))
{
  echo "Pivot UP at : ".$current;
  break;
}
else if((($diff_down-$diff_up) >$range) && ($pivot_type != "DOWN"))
{
  echo "Pivot DOWN at : ".$current;
  break;
}
1 Answers
Related