# NMath User's Guide

14.4 Rule-Based Peak Finding (.NET, C#, CSharp, VB, Visual Basic, F#)

```Class PeakFinderRuleBased finds peaks subject to rules about peak height and
peak separation. A peak is defined as a point which is higher that both neighbors
or infinity. Non-infinite end points are excluded as a peak. This class is analogous
to MATLAB's findpeaks() function.
```

Creating Rule-Based Peak Finders

A PeakFinderRuleBased instance is constructed from a vector of data.

Code Example – C# rule-based peak finding

```var x = new DoubleVector(5000, 0.01, 0.1);
```
```DoubleVector data = NMathFunctions.Sin(x) / x;
```
```var pf = new PeakFinderRuleBased( data );
```

Code Example – VB rule-based peak finding

```Dim X As New DoubleVector(5000, 0.01, 0.1)
```
```Dim Data As DoubleVector = NMathFunctions.Sin(X) / X
```
```Dim PF As New PeakFinderRuleBased(Data)
```

Adding Rules

Peak finding rule types are specified with the PeakFinderRuleBased.Rules enumeration.

Rules.MinHeight

Removes peaks that have an amplitude less than a specified amount.

Rules.Threshold

Find peaks that are at least a specified amount higher than their neighbor­ing samples.

Rules are added using the AddRule() method, and removed using RemoveRule(). Only one rule of each type is allowed. After updating the rule list, call either LocatePeaks() or LocatePeakIndices() to update the peak inventory.

For example, this rule finds all peaks with an amplitude greater than 1.5.

Code Example – C# rule-based peak finding

```pf.AddRule( PeakFinderRuleBased.Rules.MinHeight, 1.5 );
```
```pf.LocatePeaks();
```

Code Example – VB rule-based peak finding

```PF.AddRule( PeakFinderRuleBased.Rules.MinHeight, 1.5 );
```
```PF.LocatePeaks();
```

If a Rules.MinHeight rule was already specified, it is removed before adding the new rule.

Rule-Based Peak Finder Results

The provided indexer on PeakFinderRuleBased gets each peak as an instance of struct Extrema. Property NumberPeaks gets the total number of peaks found. For example, this code dump all peaks to the console:

Code Example – C# peak finding

```for (int i = 0; i < pf.NumberPeaks; i++)
```
```{
```
```  Extrema peak = pf[i];
```
```  Console.WriteLine("Found peak at = ({0},{1})", peak.X, peak.Y);
```
```}
```

Code Example – VB peak finding

```For I As Integer = 0 To PF.NumberPeaks - 1
```
```  Dim Peak As Extrema = PF(I)
```
```  Console.WriteLine("Found peak at = ({0},{1})", Peak.X, Peak.Y)
```

Next

Top

Top