If I assume that it is the 3000 period wave you are looking for extrema in, I can see you keeping track of 2 quantities. One is the long term average, something which takes in more than 2 of the long term waves (hopefully much more than 2). The other is a moving average, of about 1/3 the period (so about 1000 points). Occasionally a person has to calculate the average of that 1000 points, but for many iterations it is probably easier to just use a circular buffer of all the points, and to calculate the new average you subtract out the value to be replaced and add in the new value. If the 1000 term MA is below the long term average, you are looking for minima. If the 1000 term MA is above the long term average, you are looking for a maxima.