核心尋峰算法的原理參考Ronny,鏈接:投影曲線的波峰查找,
C#翻譯原理代碼參考sowhat4999,鏈接:C#翻譯Matlab中findpeaks方法
前人種樹,后人乘涼。感謝原作者詳細的解釋說明。
這里先把翻譯代碼貼一下(略微的修改了sowhat4999代碼中的幾個參數)
//調用方法 List<double> data = new List<double>{25, 8, 15, 5, 6, 10, 10, 3, 1, 20, 7}; List<int> index = getPeaksIndex(trendSign(oneDiff(data)));
//第一次尋峰(基本峰距為1)算法 private double[] oneDiff(List<double> data) { double[] result = new double[data.Count - 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 List<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); } } return data;//相當於原數組的下標 }
以上方法並沒有將峰距、邊鋒、峰值情況考慮在內,但已經給與我們后人一個完整的思路。
峰距情況分析:
我們可以將上述方法理解為峰距1的尋峰算法,當我們需要完成峰距為2的尋峰情況時我們需要判斷
data[i]是否大於data[i+1],data[i+2],data[i-1],data[i-2]
同理按照此方法完成點數為100000,峰距為1000的尋峰,則需要進行100000的1000次方次運算,這顯然需要花費大量的時間進行運算。
優化過程中,我們並不能改變峰距(即冪指數1000),但我們可以改變點數(即底數100000)的大小。從而實現運算量的降低。
以上峰距為1的尋峰方法此時已經完成判斷
data[i]是否大於data[i+1],data[i-1]
並返還峰值對應的索引列
峰距為2時,我們只需要再次對索引列中內容進行判斷即可(只有在峰距為1的判斷中勝出的點,才有可能在峰距為2的判斷中勝出)
data[i]是否大於data[i+2],data[i-2]
此時你會發現我們需要遍歷的底數已經並不是原點數100000,而是上次返還的尋峰序列個數
// 調用方法 List<double> Xaxis = new List<double> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; List<double> Yaxis = new List<double> { 25, 8, 15, 5, 6, 10, 10, 3, 1, 20, 7 }; // 峰距 int DisPeak = 3; // 峰距為3時得到的腳標 List<int> index = getPeaksIndex(trendSign(oneDiff(Yaxis))); // 已進行的判斷 int level = 1; // 擴大峰距范圍范圍算法 while (DisPeak > level) { level++; List<int> result = DoPeakInstance(Yaxis, index, level); index = null; index = result; } // 獲取兩側滿足條件的邊峰序列 index = GetBothSidePeakIndex(Xaxis, Yaxis, 1, index); double minFZ = 10.0; // 根據最小峰值序列進行篩選 index = FindMinPeakValue(minFZ, Yaxis, index);
//擴大尋峰范圍算法 private List<int> DoPeakInstance(List<double> data, List<int> index, int level) { //相當於原數組的下標 List<int> result = new List<int>(); for (int i = 0; i < index.Count; i++) { //判斷是否超出下界和上界 if (index[i] - level >= 0 && index[i] + level < data.Count) { if (data[index[i] + level] <= data[index[i]] && data[index[i] - level] <= data[index[i]]) { result.Add(index[i]); } } } return result; }
邊鋒情況分析:
仔細閱讀上述兩算法,你會發現該算法存在一個無法避免的問題 如:
峰距是3,此時峰首部點序(點0,點1,點2)因無法向前比較,導致並沒有參與到峰值計算中。 尾部點則因無法向后比較沒有參與到峰值計算中。
此情況我們首先要清楚,因上述情況未參與比較的點序中,首部最多僅有一個峰值,尾部最多僅有一個峰值。
那我們把它加上就好了,美滋滋。
//獲取兩側滿足條件的邊峰序列 private static List<int> GetBothSidePeakIndex(List<double> Xaxis, List<double> Yaxis, int FJ, List<int> index) { //獲取數據首尾兩側最大峰值(0,FJ)點序和(Date.CountFJ-FJ,Data.Count)點序 int TopIndex = 0; int BottomIndex = Yaxis.Count - 1; for (int i = 0; i < FJ; i++) { if (Yaxis[i] >= Yaxis[TopIndex]) { TopIndex = i; } if (Yaxis[Yaxis.Count - 1 - i] >= Yaxis[BottomIndex]) { BottomIndex = Yaxis.Count - 1 - i; } } //判斷是否滿足條件檢索條件 int newTopIndex = TopIndex; int newBottomIndex = BottomIndex; for (int i = 0; i <= FJ; i++) { if (Yaxis[TopIndex + i] >= Yaxis[TopIndex]) { newTopIndex = TopIndex + i; } if (Yaxis[BottomIndex - i] >= Yaxis[BottomIndex]) { newBottomIndex = BottomIndex - i; } } TopIndex = newTopIndex; BottomIndex = newBottomIndex; //添加到結果序列 if (TopIndex <= FJ && TopIndex != 0) { index.Insert(0, TopIndex); } if (BottomIndex >= BottomIndex - FJ && BottomIndex != Xaxis.Count - 1) { index.Add(BottomIndex); } return index; }
最后,也就是最簡單的峰值判斷了。比一下就好了。
//根據最小峰值序列進行篩選 private static List<int> FindMinPeakValue(double minFZ, List<double> Yaxis, List<int> index) { List<int> finalresult = new List<int>(); for (int i = 0; i < index.Count; i++) { if (Yaxis[index[i]] >= minFZ) { finalresult.Add(index[i]); } } index = null; index = finalresult; return index; }