简体   繁体   English

在一维数组中查找时间序列数据中的波峰和波谷-Javascript

[英]Finding peaks and troughs in time series data in a 1D array - Javascript

So basically i have a 1d array with over 500 values floating between the ranges of 100- 130 or about there arranged in no particular order(its random); 因此,基本上我有一个一维数组,其中有500多个值在100-130范围之间浮动,或者没有按特定顺序排列(随机); and I wanna know how can I find the peaks and troughs of this data. 我想知道如何找到这些数据的波峰和波谷。 Is there a way to also change sensitivity of the peak detection? 有没有办法改变峰值检测的灵敏度? What is a comprehensive algorithm for this? 什么是综合算法? If there is a JS library for it, that would be awesome. 如果有一个JS库,那就太好了。

An algorithm for finding peaks and troughs would be essentially the same thing you would do with your finger when looking at a graph. 查找峰和谷的算法本质上与查看图形时用手指执行的操作相同。 You go from the start and follow the line and take note when you see a peak and a trough. 您从头开始,沿直线行驶,并在看到高峰和低谷时记录下来。

Programmically we can define that as this: 我们可以通过编程将其定义为:

For some array of length n ( with n-1 being the index of the last element and 0 the index of the first ), iterate thorough from 1 to n-2 . 对于某些长度为n数组( n-1是最后一个元素的索引, 0是第一个元素的索引 ),从1迭代到n-2 Then peaks and troughs would be defined as: 然后将波峰和波谷定义为:

  • For element i , if i-1 > i and i+1 > i . 对于元素i ,如果i-1 > ii+1 > i Then i is a trough. i是低谷。
  • For element i , if i-1 < i and i+1 < i . 对于元素i ,如果i-1 < ii+1 < i Then i is a peak. i是一个高峰。

This would be an O(n) algorithm and be enough information to be able to program it. 这将是一个O(n)算法,并且有足够的信息可以对其进行编程。


Below is an example program implementing the algothim above: 以下是实现上述algothim的示例程序:

 var array = [102,112,115,120,119,102,101,100,103,105,110,109,105,100]; function findPeaksAndTroughs(array) { var start = 1; // Starting index to search var end = array.length - 2; // Last index to search var obj = { peaks: [], troughs: [] };// Object to store the indexs of peaks/thoughs for(var i = start; i<=end; i++) { var current = array[i]; var last = array[i-1]; var next = array[i+1]; if(current > next && current > last) obj.peaks.push(i); else if(current < next && current < last) obj.troughs.push(i); } return obj; } console.log(findPeaksAndTroughs(array)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM