C#實現局部峰值尋找,功能對應Matlab中的findpeaks.m

來源:互聯網
上載者:User

標籤:作者   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.Length; 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 < sign.Length; 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)         {            List<int> data = new List<int>();            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.Length; 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.