如何在javascript中检测给定1D数组中的峰值?

如何在javascript中检测给定1D数组中的峰值?,javascript,arrays,node.js,search,graph,Javascript,Arrays,Node.js,Search,Graph,我有一个(y)值数组。这些值的范围和生成的图形如下 我想取这个数组,得到p,Q,R,S和T属性的索引 如果你想知道这些财产是什么意思。检查此图像以解释数组的哪个部分引用属性: 假设我们有一个y值数组,如下所示: const数据=[ 0, 0, 0, 0, 0.0000050048828125, 0.0000137939453125, 0.000049560546875, 0.00008740234375, 0.00015966796875, 0.000262451171875, 0.00039

我有一个(y)值数组。这些值的范围和生成的图形如下

我想取这个数组,得到p,Q,R,S和T属性的索引

如果你想知道这些财产是什么意思。检查此图像以解释数组的哪个部分引用属性:

假设我们有一个y值数组,如下所示:

const数据=[
0, 0, 0, 0, 0.0000050048828125, 0.0000137939453125, 0.000049560546875,
0.00008740234375, 0.00015966796875, 0.000262451171875, 0.0003975830078125, 0.0005687255859375,
0.0007802734375, 0.001037353515625, 0.0013468017578125, 0.00172119140625, 0.0021756591796875,
0.0027232666015625, 0.0033880615234375, 0.004206787109375, 0.0052380371093750005,
0.006586181640625, 0.008400146484375001, 0.010904296875, 0.0144892578125, 0.0196798095703125,
0.049684204101562504, 0.0886883544921875, 0.11185363769531251, 0.134164306640625,
0.137352294921875, 0.1160369873046875, 0.08516308593750001, 0.0539765625,
0.014997436523437501, -0.015882568359375, -0.0387554931640625, -0.06125732421875, -0.0745780029296875, -0.07479357910156251, -0.0725338134765625, -0.0418538818359375,
0.08582861328125001, 0.397717529296875, 0.8136408691406251, 1.2295617980957032,
0.9944150390625001, 0.2824605712890625, -0.38949267578125, -0.597251220703125, -0.425675537109375, -0.1537947998046875, -0.0500914306640625, -0.0111041259765625,
0.0027451171875, 0.0071739501953125, 0.008443359375, 0.0094327392578125, 0.012530517578125,
0.0176046142578125, 0.0300162353515625, 0.0433489990234375, 0.056962646484375004,
0.0704832763671875, 0.0770511474609375, 0.0898175048828125, 0.10311853027343751,
0.117046142578125, 0.1312630615234375, 0.1529300537109375, 0.167607177734375,
0.1899068603515625, 0.2124422607421875, 0.235044677734375, 0.2575535888671875,
0.2724073486328125, 0.286978271484375, 0.3007579345703125, 0.3067425537109375,
0.3106370849609375, 0.303756103515625, 0.2897236328125, 0.25916931152343753,
0.2200599365234375, 0.1728209228515625, 0.133416259765625, 0.086224853515625,
0.05493408203125, 0.02409423828125, 0.00922607421875, -0.0043409423828125, -0.0097349853515625, -0.013127685546875, -0.01423095703125, -0.013834716796875, -0.012556030273437501, -0.010675048828125, -0.00835888671875, -0.0057305908203125, -0.0000562744140625

]
我的意见:

  • 计算出所有波(向上或向下),指数穿过y轴=0

  • 根据第一步的结果,我们可以计算出p,Q,R,S

  • 例如:假设步骤1的结果是一个数组=found

    如果
    found[index]
    是上波,
    found[index-1]
    found[index+1]
    是下波,那么我们就知道
    found[index]
    是一个Q

    代码如下:

    const测试=[
    0, 0, 0, 0, 0.0000050048828125, 0.0000137939453125, 0.000049560546875,
    0.00008740234375, 0.00015966796875, 0.000262451171875, 0.0003975830078125, 0.0005687255859375,
    0.0007802734375, 0.001037353515625, 0.0013468017578125, 0.00172119140625, 0.0021756591796875,
    0.0027232666015625, 0.0033880615234375, 0.004206787109375, 0.0052380371093750005,
    0.006586181640625, 0.008400146484375001, 0.010904296875, 0.0144892578125, 0.0196798095703125,
    0.049684204101562504, 0.0886883544921875, 0.11185363769531251, 0.134164306640625,
    0.137352294921875, 0.1160369873046875, 0.08516308593750001, 0.0539765625,
    0.014997436523437501, -0.015882568359375, -0.0387554931640625, -0.06125732421875, -0.0745780029296875, -0.07479357910156251, -0.0725338134765625, -0.0418538818359375,
    0.08582861328125001, 0.397717529296875, 0.8136408691406251, 1.2295617980957032,
    0.9944150390625001, 0.2824605712890625, -0.38949267578125, -0.597251220703125, -0.425675537109375, -0.1537947998046875, -0.0500914306640625, -0.0111041259765625,
    0.0027451171875, 0.0071739501953125, 0.008443359375, 0.0094327392578125, 0.012530517578125,
    0.0176046142578125, 0.0300162353515625, 0.0433489990234375, 0.056962646484375004,
    0.0704832763671875, 0.0770511474609375, 0.0898175048828125, 0.10311853027343751,
    0.117046142578125, 0.1312630615234375, 0.1529300537109375, 0.167607177734375,
    0.1899068603515625, 0.2124422607421875, 0.235044677734375, 0.2575535888671875,
    0.2724073486328125, 0.286978271484375, 0.3007579345703125, 0.3067425537109375,
    0.3106370849609375, 0.303756103515625, 0.2897236328125, 0.25916931152343753,
    0.2200599365234375, 0.1728209228515625, 0.133416259765625, 0.086224853515625,
    0.05493408203125, 0.02409423828125, 0.00922607421875, -0.0043409423828125, -0.0097349853515625, -0.013127685546875, -0.01423095703125, -0.013834716796875, -0.012556030273437501, -0.010675048828125, -0.00835888671875, -0.0057305908203125, -0.0000562744140625
    ];
    //计算步骤1的结果
    step1=test.reduce(函数(pre、cur、index、orgArray){
    if(cur==0){pre['found'].push({'type':'no-signal','index':index,'value':0});pre['min']={'value':0,'index':0};pre['max']={'value':0,'index':0};返回pre;}
    如果(在['max']['value']orgArray[index+1]){pre['found'].push({'type':'upwave','index':index,'value':cur});
    }
    如果(在['min']['value']>cur之前){
    pre['min']={'value':cur,'index':index};
    如果(pre['min']['value']0&&cur<0){pre['found'].push({'type':'down-cross','index':index});pre['min']={'value':0,'index':0};pre['max']={'value':0,'index':0};}
    如果(orgArray[index-1]<0&&cur>0){pre['found'].push({'type':'up-cross','index':index});pre['max']={'value':0,'index':0};pre['min']={'value':0,'index';}
    返回前
    },{'max':{'value':0,'index':0},'min':{'value':0,'index':0},'found':[]})
    //console.log(步骤1['found']);
    //计算最终结果
    final=step1['found'].reduce(函数(pre、cur、index、orgArray){
    如果(当前类型==='downwave'
    &&orgArray[index-1]。类型=='down-cross'
    &&orgArray[in