I am having an issue that has come up with a specific set of points using the code provided. I have a JS fiddle that can demonstrate the issue. There are two sets of points provided in the example – one that works as expected, and one that is not calculating the peak correctly. Any direction would be helpful.
https://jsfiddle.net/3tL60wzy/
Let me know if you need any more information in order to review, etc.