Peak detection algorithm