核心寻峰算法的原理参考Ronny,链接:投影曲线的波峰查找,
C#翻译原理代码参考sowhat4999,链接:C#翻译Matlab中findpeaks方法
前人种树,后人乘凉。感谢原作者详细的解释说明。
这里先把翻译代码贴一下(略微的修改了sowhat4999代码中的几个参数类型)
//调用方法
List<double> data = {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;//相当于原数组的下标
}