在C++中,查找数组中的重复峰值并计算脉冲

In C++, finding repeated peak values in array and calculating pulse

首先,我提前致歉,我是编程新手,非常感谢任何帮助。我正在使用 mbed 对微控制器进行编程: 我的目标是检查数组的峰值:

void peakvals(int array[],const int count);{     //count = size array = 10, meant to find peak vals in array
    int peakcount=0;                             //meant to record the number of peak vals occurring
    for (int i=0;i<peakcount;i++){                
    for (int j=0;j<i;j++){                   
        if ( array[i]==array[j] ){  
            peakcount=1;                         
            peakcount++;                         
        
        }
    }

我的总体目标是,然后我想使用此代码并输出脉率估计值,首先等待几秒钟以累积值并给出准确的读数,然后每 1 秒更新一次。 我希望我的问题足够清楚,感谢您的宝贵时间

正如我在评论中建议的那样,一个简单的函数可能如下所示:

int count_peaks(const int array[], const int arrsize)
{     
    int peakcount=0;                            

    // NOTE: C++ index start at 0, so (arrsize-1) is the last element you can access in the array.
    for (int i= 1; i < arrsize - 2; ++i)
    {    
        // Temporary variables are just to clarify the code, you can do it one-liner below instead.
        const int previous  = array[i-1];
        const int current   = array[i];
        const int next      = array[i+1];
        
        // if( array[i] > array[i-1] && array[i] > array[i+1])
        if( current > previous && current > next)
            peakcount++;
    }
    return peakcount;
}

查看数组的上一个和下一个元素的位置。