<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>nmath peak finding Archives - CenterSpace</title>
	<atom:link href="https://www.centerspace.net/tag/nmath-peak-finding/feed" rel="self" type="application/rss+xml" />
	<link>https://www.centerspace.net/tag/nmath-peak-finding</link>
	<description>.NET numerical class libraries</description>
	<lastBuildDate>Tue, 01 Mar 2016 21:38:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>
<site xmlns="com-wordpress:feed-additions:1">104092929</site>	<item>
		<title>Finding Peaks in Data with NMath</title>
		<link>https://www.centerspace.net/finding-peaks-in-data-with-nmath</link>
					<comments>https://www.centerspace.net/finding-peaks-in-data-with-nmath#respond</comments>
		
		<dc:creator><![CDATA[Paul Shirkey]]></dc:creator>
		<pubDate>Wed, 13 Oct 2010 17:06:58 +0000</pubDate>
				<category><![CDATA[NMath]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[nmath peak finding]]></category>
		<category><![CDATA[peak finding]]></category>
		<category><![CDATA[peak finding c#]]></category>
		<category><![CDATA[savitzy-golay]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=2682</guid>

					<description><![CDATA[<p><img src="https://www.centerspace.net/blog/wp-content/uploads/2010/10/peaksexample.png" alt="Example peaks" title="Peaks Example"  class="excerpt" /><br />
Finding peaks in experimental data is a very common computing activity, and because of its intuitive nature there are many established techniques and literally dozens of heuristics built on top of those.  CenterSpace Software has jumped into this algorithmic fray with a new peak finding class based on smooth Savitzy-Golay polynomials.  If  you are not familiar with Savitzy-Golay polynomial smoothing, take a look at our previous <a href="https://www.centerspace.net/blog/statistics/savtizky-golay-smoothing/">blog article</a>. </p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/finding-peaks-in-data-with-nmath">Finding Peaks in Data with NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Finding peaks in experimental data is a very common computing activity, and because of its intuitive nature there are many established techniques and literally dozens of heuristics built on top of those.  CenterSpace Software has jumped into this algorithmic fray with a new peak finding class based on smooth Savitzy-Golay polynomials.  If  you are not familiar with Savitzy-Golay polynomial smoothing, take a look at our previous <a href="/savitzky-golay-smoothing/">blog article</a>.  When used for peak finding, we simply report the zero crossing derivatives of the smoothing, locally-fit, Savitzy-Golay polynomials.  This is a very fast peak finder because the Savitzy-Golay smoothing algorithm can be slightly altered to directly report the first derivatives, which remarkably, can be done with a convolve operation.  Because this peak finder is based on Savitzy-Golay polynomials, it requires that the data be sampled at regular intervals.  </p>
<h3> An Introductory Example </h3>
<p>Suppose we have the following sampled data.</p>
<figure id="attachment_2697" aria-describedby="caption-attachment-2697" style="width: 450px" class="wp-caption aligncenter"><a href="https://www.centerspace.net/blog/wp-content/uploads/2010/09/peakfinder-example-1.png"><img decoding="async" src="https://www.centerspace.net/blog/wp-content/uploads/2010/09/peakfinder-example-1.png" alt="graph of example data" title="simple graph" width="450"  class="size-full wp-image-2697" srcset="https://www.centerspace.net/wp-content/uploads/2010/09/peakfinder-example-1.png 557w, https://www.centerspace.net/wp-content/uploads/2010/09/peakfinder-example-1-300x131.png 300w" sizes="(max-width: 557px) 100vw, 557px" /></a><figcaption id="caption-attachment-2697" class="wp-caption-text">Simple data with a single peak.</figcaption></figure>
<p>The following C# code builds the test data and locates the single peak in this simple data set.</p>
<pre lang="csharp">
Using CenterSpace.NMath.Core;

DoubleVector d = new DoubleVector(0,-1, 1.5, 2, 3, 4, 4.5, 4, 3, 2.2, 1.0, -3.0, 0);
PeakFinderSavitzkyGolay pfa = new PeakFinderSavitzkyGolay(d, 5, 4);
pfa.LocatePeaks();
</pre>
<p>The peak data reported back by the Savitzky-Golay peak finder can be either the <code>(x,y)</code> location of the peak, or the index lying on or preceding the found peak.</p>
<pre class="code">
Peak found at x:6.00, y:4.50
Peak found at index: 6
</pre>
<p>The peak finding <code>PeakFinderSavitzkyGolay</code> class requires three parameters: a data vector, the filter window width, and the degree of the smoothing polynomial.</p>
<h3> A More Complex Example </h3>
<p>To build a peak finder on top on this class for some domain specific data, we need to understand the basic parameters that control which peaks are reported.  For this second example, let&#8217;s use the complex signal show below, which contains a mixture of isolated peaks, adjacent peaks, and narrow and broad peaks.</p>
<figure id="attachment_2713" aria-describedby="caption-attachment-2713" style="width: 450px" class="wp-caption aligncenter"><a href="https://www.centerspace.net/blog/wp-content/uploads/2010/10/peaksexample.png"><img decoding="async" src="https://www.centerspace.net/blog/wp-content/uploads/2010/10/peaksexample.png" alt="Example peaks" title="Peaks Example" width="450"  class="size-full wp-image-2713" srcset="https://www.centerspace.net/wp-content/uploads/2010/10/peaksexample.png 572w, https://www.centerspace.net/wp-content/uploads/2010/10/peaksexample-300x192.png 300w" sizes="(max-width: 572px) 100vw, 572px" /></a><figcaption id="caption-attachment-2713" class="wp-caption-text">Peaks of various widths and heights</figcaption></figure>
<p>This signal also includes some very subtle peaks near <code>x = 23.5</code> and <code>x = 29</code>.  Applying the <code> PeakFinderSavitzkyGolay </code> class as shown below, all 15 peaks are found (the top of the first peak is off the scale in our image).</p>
<pre lang="csharp">
PeakFinderSavitzkyGolay pfa = new PeakFinderSavitzkyGolay(signal, 10, 5);
pfa.AbscissaInterval = 0.1;
pfa.LocatePeaks();
Console.WriteLine("Number of peaks found: " + pfa.NumberPeaks.ToString());
Console.WriteLine(String.Format("Peak found at x:{0:0.00}, y:{1:0.00}", pfa[4].X, pfa[4].Y));
</pre>
<p>The position of the fifth peak is reported to the console, using indexing notation, <code>pfa[4].X, pfa[4].Y</code>, on the peak finder object.</p>
<pre class="code">
Number of peaks found: 15
Peak found at x:9.03, y:0.35
</pre>
<p>By setting the <code> AbsciassaInterval </code> to the signal sample rate (in this example 0.1 seconds) the class can scale the x-axis according to your units, and supply the <code>(x, y)</code> positions of all found peaks.  If you only need the peak location down to the resolution of the sample interval, the peak finder will just report the index that either precedes or lies on the peak abscissa location.  This avoids the an extra interpolation step to locate the inter-sample peak abcissa, and increases performance.</p>
<p>Now supposing that we want to eliminate all broad peaks and can increase the peak finders selectivity.</p>
<pre lang="csharp">
PeakFinderSavitzkyGolay pfa = new PeakFinderSavitzkyGolay(signal, 10, 5);
pfa.AbscissaInterval = 0.1;
pfa.SlopeSelectivity = 0.003;
pfa.LocatePeaks();
</pre>
<p>The property <code> SlopeSelectivity </code> defaults to zero, causing the peak finder to report all found peaks.  By increasing the selectivity a hair to <code>0.003</code>, the peak finder no longer reports the last three peaks.  Both the two subtle peaks are eliminated along with the final broad peak near <code>26</code>.  The slope selectivity is simply the slope of the smoothed first derivative of the Savitzy-Golay polynomial at each zero crossing &#8211; so as it&#8217;s value is increased only the more pronounced peaks (with steeply diving smoothed first derivatives) are reported.</p>
<p>If we want to heavily filter the peaks and only see the peaks of the general trend line, we could increase the filter width dramatically from 10 to 80.</p>
<pre lang="csharp">
PeakFinderSavitzkyGolay pfa = new PeakFinderSavitzkyGolay(signal, 80, 5);
pfa.AbscissaInterval = 0.1;
pfa.SlopeSelectivity = 0.0;
pfa.LocatePeaks();
</pre>
<p>Using these parameters, we find only the four peaks that capture the low frequency variation of the signal above.</p>
<pre class="code">
Peak found at x:7.53, y:0.34
Peak found at x:14.27, y:0.26
Peak found at x:20.28, y:0.25
Peak found at x:26.76, y:0.24
</pre>
<p>Note that the y-values here correspond to the <em> smoothed, fitted polynomial </em> not the actual data at the <code>x</code> value.  This demonstrates the ability of the this peak finder to act as a low pass filter, which can be use to sort out the peaks of interest from higher frequency noise.  This is why Savitzy-Golay data smoothing is often used for baseline subtraction (for example in pre-processing mass spectrometry data before peak finding).</p>
<h3> Summary &#038; Performance </h3>
<p>The first example used a smoothing polynomial of degree 4, and the second example a polynomial degree of 5.  Experience shows that typically a polynomial degree between 3-7 will be best suited to smooth measurement data and correctly locate peaks.   However, there is no hard and fast rule, so feel free to experiment.  Having said that, the polynomial degree must always be strictly less than the window width or an exception will be thrown.  </p>
<pre class="code">
polynomial degree < window width
</pre>
<p>Because, after object construction, this peak finder boils down to a convolution, it's performance is far better that many peak finders.  On my 2.8Ghz Intel i7 Quad Core, I can find peaks in 3 million data points in about 80 ms, and in 30 million data that requires about 700ms.  That would give us the ability to do peak finding in a real time system running with a sample rate of  ~43 MHz.   In a real system there would likely be other peak filtering overhead, but we would still be able to process data at a very high rate - suitable for most real-time data sampling applications. </p>
<p>Happy Computing<br />
<em><br />
-Paul Shirkey<br />
</em></p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/finding-peaks-in-data-with-nmath">Finding Peaks in Data with NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/finding-peaks-in-data-with-nmath/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2682</post-id>	</item>
	</channel>
</rss>
