# C# 实现寻峰算法的简单优化（包含边峰，最小峰值，峰距）

2018-02-01 12:08:29来源:cnblogs.com作者:柠檬五个半人点击

C#翻译原理代码参考sowhat4999，链接：C#翻译Matlab中findpeaks方法

//调用方法List<double> data = new List<double>{25, 8, 15, 5, 6, 10, 10, 3, 1, 20, 7};List<int> index = getPeaksIndex(trendSign(oneDiff(Constant.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;//相当于原数组的下标}

data[i]是否大于data[i+1],data[i+2],data[i-1],data[i-2]

以上峰距为1的寻峰方法此时已经完成判断

data[i]是否大于data[i+1],data[i-1]

data[i]是否大于data[i+2],data[i-2]

//调用方法            List<double> data = new List<double>{25, 8, 15, 5, 6, 10, 10, 3, 1, 20, 7};            //峰距            int DisPeak = 3；            // 峰距为1时得到的脚标            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;            }

//扩大寻峰范围算法
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)
{
double aa = data[index[i] + level];
double bb = data[index[i]];
double cc = data[index[i] - level];
if (data[index[i] + level] <= data[index[i]] && data[index[i] - level] <= data[index[i]])
{
}
}
}
return result;
}

边锋情况分析：

//获取数据首尾两侧最大峰值（0，DisPeak）点序和（Date.CountFJ-DisPeak,Data.Count）点序            int TopIndex = 0;            int BottomIndex = Yaxis.Count-1;            for (int i = 0; i < DisPeak; i++)            {                if (Yaxis[i] >= Yaxis[TopIndex])                {                    TopIndex = i;                }                if (Yaxis[Yaxis.Count-1 - i] >= Yaxis[BottomIndex])                {                    BottomIndex = Yaxis.Count - 1 - i;                }            }            //判断是否满足条件检索条件（首部向后点进行比较，尾部向前点进行比较，比较DisPeak个点）            int newTopIndex = TopIndex;            int newBottomIndex = BottomIndex;            for (int i = 0; i <= DisPeak; 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 <= DisPeak)            {                index.Insert(0, TopIndex);            }            if (BottomIndex >= Xaxis.Count - DisPeak)            {                index.Add(BottomIndex);            }

//最小峰值            int minPeakValue = 10;            List<int> finalresult = new List<int>();            for (int i = 0; i < index.Count; i++)            {                                if (Yaxis[index[i]] >= minPeakValue)                {                    finalresult.Add(index[i]);                }            }            index = null;            index = finalresult;