使用 Javascript 查找数组的峰值

Jos*_*lor 2 javascript graph

我有一个包含 640 个值的数组,其中包含绘制线条所需的 y 值。

线

我需要确定每三个峰顶部的阵列位置。

我在这里查看了与局部最大值/局部最小值相关的类似问题,但是运行我发现的代码会出现小峰值。我只需要三个非常不同的峰。

我正在尝试用 JavaScript 来做到这一点。来自数组的数据如下:

126,126,126,126,126,126,126,126,126,126,126,126,126,126,124,123,122,122,120,119,119,118,118,118,119,119,119,120,121,121,122,124,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,124,125,124,125,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,123,124,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,122,123,122,123,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,119,119,119,119,116,115,114,113,113,113,113,113,115,117,118,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,108,107,106,118,108,108,103,103,103,103,103,103,104,105,107,108,109,109,109,109,109,109,109,109,109,109,109,109,109,109,109
Run Code Online (Sandbox Code Playgroud)

我在数组上运行了这段代码:

    function pickPeaks(arr){
  return arr.reduce( (res, val, i, self) => {
    if(
      // a peak when the value is greater than the previous and greater than the next
      val > self[i - 1] && val > self[i + 1] 
      || 
      // a plateau when the value is greater than the previuos and equal to the next and from there the next different value is less
      val > self[i - 1] && val === self[i + 1] && self.slice(i).find( item =>  item !== val ) < val 
    ){
      res.pos.push(i);
      res.peaks.push(val);
    }
    return res;
  }, { pos:[],peaks:[] } );
}

console.log(pickPeaks(first_black_array))
Run Code Online (Sandbox Code Playgroud)

它输出以下内容:

peaks: (9) [362, 356, 356, 357, 358, 358, 367, 374, 377]
pos: (9) [21, 67, 69, 112, 179, 181, 313, 608, 612]
Run Code Online (Sandbox Code Playgroud)

我希望结果是这样的:

pos: [24, 316, 616]
Run Code Online (Sandbox Code Playgroud)

(每个山峰的顶部)

非常感谢

AKX*_*AKX 5

为了扩展我的评论,比如

function detectPeaks(data, windowWidth, threshold) {
  const peaks = [];
  for (let i = 0; i < data.length; i++) {
    const start = Math.max(0, i - windowWidth);
    const end = Math.min(data.length, i + windowWidth);
    let deltaAcc = 0;
    for (let a = start; a < end; a++) {
      deltaAcc += Math.abs(data[a - 1] - data[a]);
    }
    if (deltaAcc > threshold) {
      peaks.push(i);
    }
  }
  return peaks;
}
Run Code Online (Sandbox Code Playgroud)

可以很好地找到“峰值”(或者更确切地说,值变化比其他地方更多的区域)。

您可以在https://codepen.io/akx/pen/QowEQq找到一个交互式示例,您还可以在其中调整窗口宽度和阈值。