Skip to content

Instantly share code, notes, and snippets.

@mvacha
Created July 17, 2017 02:00
Show Gist options
  • Save mvacha/410e25ded1f66d9bcff33325b80cfca9 to your computer and use it in GitHub Desktop.
Save mvacha/410e25ded1f66d9bcff33325b80cfca9 to your computer and use it in GitHub Desktop.
C# implementation of PeakDet algorithm from: http://www.billauer.co.il/peakdet.html
public static IList<(int pos, double val)> PeakDet(this IList<double> vector, double triggerDelta)
{
double mn = Double.PositiveInfinity;
double mx = Double.NegativeInfinity;
int mnpos = 0;
int mxpos = 0;
bool lookformax = true;
var maxtab_tmp = new List<(int pos, double val)>();
//var mintab_tmp = new List<(int pos, double val)>();
for (int i = 0; i < vector.Count; i++)
{
double a = vector[i];
if (a > mx)
{
mx = a;
mxpos = i;
}
if (a < mn)
{
mn = a;
mnpos = i;
}
if (lookformax)
{
if (a < mx - triggerDelta)
{
maxtab_tmp.Add((mxpos, mx));
mn = a;
mnpos = i;
lookformax = false;
}
}
else
{
if (a > mn + triggerDelta)
{
//mintab_tmp.Add((mnpos, ns));
mx = a;
mxpos = i;
lookformax = true;
}
}
}
return maxtab_tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment