For simple functions, such as peak/valley, you can find the algorithm in the manual