C#实现局部峰值查找,功能对应Matlab中的findpeaks.m

2021-03-26 11:26

阅读:524

标签:作者   hat   for   c++   sign   数组   www.   查找   https   

原文:C#实现局部峰值查找,功能对应Matlab中的findpeaks.m

相关算法的原理参考Ronny,地址:图像分析:投影曲线的波峰查找,这里感谢下原作者。

参照C++的代码实现,我用C#翻译了下,其实原理也很简单的,下面放相关实现代码:

        private double[] oneDiff(double[] data) 
        {
            double[] result = new double[data.Length - 1];
            for (int i = 0; i )
            {
                result[i] = data[i + 1] - data[i];
            }
            return result;
        }
        private int[] trendSign(double[] data)
        {
            int[] sign = new int[data.Length];
            for (int i = 0; i )
            {
                if (data[i] > 0) sign[i] =1;
                else if (data[i] == 0) sign[i] = 0;
                else sign[i] = -1;
            }
            
            for (int i = sign.Length - 1; i >=0 ; i--)
            {
                if (sign[i] == 0 && i ==sign.Length - 1)
                {
                    sign[i] = 1;
                }
                else if (sign[i] == 0) 
                {
                    if (sign[i+1] >= 0)
                    {
                        sign[i] = 1;
                    }
                    else
                    {
                        sign[i] = -1;
                    }
                }
            }
            return sign;
        }
        private int[] getPeaksIndex(int[] diff) 
        {
            Listint> data = new Listint>();
            for (int i = 0; i != diff.Length - 1; i++)
            {
                if (diff[i+1] - diff[i] == -2)
                {
                    data.Add(i + 1);
                }
            }
            int[] result = new int[data.Count];
            for (int i = 0; i )
            {
                result[i] = data[i];
            }
            return result;//相当于原数组的下标
        }

调用方法:

double[] data = {25, 8, 15, 5, 6, 10, 10, 3, 1, 20, 7};
int[] index = getPeaksIndex(trendSign(oneDiff(Constant.data)));

返回的int[]数组内容是对应原数组中峰值对应的索引(从0开始)

 

C#实现局部峰值查找,功能对应Matlab中的findpeaks.m

标签:作者   hat   for   c++   sign   数组   www.   查找   https   

原文地址:https://www.cnblogs.com/lonelyxmas/p/9383992.html


评论


亲,登录后才可以留言!