<?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>Savitzky-Golay in VB Archives - CenterSpace</title>
	<atom:link href="https://www.centerspace.net/tag/savitzky-golay-in-vb/feed" rel="self" type="application/rss+xml" />
	<link>https://www.centerspace.net/tag/savitzky-golay-in-vb</link>
	<description>.NET numerical class libraries</description>
	<lastBuildDate>Wed, 23 Apr 2014 16:23:38 +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>Savitzky-Golay Smoothing in C#</title>
		<link>https://www.centerspace.net/savitzky-golay-smoothing</link>
					<comments>https://www.centerspace.net/savitzky-golay-smoothing#comments</comments>
		
		<dc:creator><![CDATA[Paul Shirkey]]></dc:creator>
		<pubDate>Mon, 09 Nov 2009 19:45:58 +0000</pubDate>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[data smoothing]]></category>
		<category><![CDATA[data smoothing in c#]]></category>
		<category><![CDATA[experimental data smoothing]]></category>
		<category><![CDATA[Savitzky-Golay]]></category>
		<category><![CDATA[Savitzky-Golay .NET]]></category>
		<category><![CDATA[Savitzky-Golay coefficients]]></category>
		<category><![CDATA[Savitzky-Golay in C#]]></category>
		<category><![CDATA[Savitzky-Golay in VB]]></category>
		<category><![CDATA[Savitzky-Golay smoothing]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=547</guid>

					<description><![CDATA[<p>Savitzky-Golay smoothing effectively removes local signal noise while preserving the shape of the signal. Commonly, it&#8217;s used as a preprocessing step with experimental data, especially spectrometry data because of it&#8217;s effectiveness at removing random variation while minimally degrading the signal&#8217;s information content. Savitzky-Golay boils down to a fast (multi-core scaling) correlation operation, and therefore can [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/savitzky-golay-smoothing">Savitzky-Golay Smoothing in C#</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Savitzky-Golay smoothing effectively removes local signal noise while preserving the shape of the signal.  Commonly, it&#8217;s used as a preprocessing step with experimental data, especially spectrometry data because of it&#8217;s effectiveness at removing random variation while minimally degrading the signal&#8217;s information content.  Savitzky-Golay boils down to a fast (multi-core scaling) correlation operation, and therefore can be used in a real-time environment or on large data sets efficiently.  If higher order information is needed from the signal, Savitzky-Golay can also provide high quality smoothed derivatives of a noisy signal.</p>
<h3>Algorithm</h3>
<p>Savitzky-Golay locally smooths a signal by fitting a polynomial, in a least squares sense, to a sliding window of data.  The degree of the polynomial and the length of the sliding window are the filter&#8217;s two tuning parameters.  If <code>n</code> is the degree of the polynomial that we are fitting, and <code>k</code> is the width of the sliding window, then<br />
<center><br />
<img decoding="async" title="n &lt; k - 1" src="http://latex.codecogs.com/gif.latex?n &lt; k - 1" alt="" /><br />
</center><br />
is needed for smoothing behavior (we must avoid an over-determined system).  Typically <code>n</code> is 3 or 4, and <code>k</code> depends on the size in samples of the noisy features to be suppressed in your data set.  </p>
<p>For the case of <code>n=0</code> the Savitzy-Golay filter degenerates to a moving average filter &#8211; which is good for removing white noise, but is poor for preserving peak shape (higher order moments).  For <code>n=1,</code> the filter does a linear least-squares fit of the windowed data to a line.  If <code>n=k-1,</code> the polynomial exactly fits data point in the window, and so no filtering takes place.</p>
<p>Once the polynomial is fit, then (typically) the center data-point in this window is replaced by value of the polynomial at that location.  The window then slides to the right one sample and the process is repeated.</p>
<p>Savizky-Golay delivers the unexpected surprise that <em> the polynomial fitting coefficients are constant </em> for a given <code>n</code> and <code>k.</code>  This means that once we fix <code>n</code> and <code>k</code> for our filter, the Savizky-Golay polynomial fitting coefficients are computed once during setup and then used across the entire data set.  This is why Savizky-Golay is a high performance correlation filter.</p>
<h3>Comparison Example</h3>
<p>The following three images show some real experimental data and a comparison of two filtering algorithms.  The first image shows the raw data, the second image shows the effect of an averaging filter, and the last image demonstrates a Savitzky-Golay smoothing filter of length five.</p>
<figure id="attachment_604" aria-describedby="caption-attachment-604" style="width: 400px" class="wp-caption aligncenter"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-604" title="current_raw_data" src="https://www.centerspace.net/blog/wp-content/uploads/2009/11/current_raw_data.jpg" alt="Unsmoothed Data" width="400" height="316" /><figcaption id="caption-attachment-604" class="wp-caption-text">Raw Data</figcaption></figure>
<figure id="attachment_603" aria-describedby="caption-attachment-603" style="width: 400px" class="wp-caption aligncenter"><img decoding="async" loading="lazy" class="size-full wp-image-603" title="current_averaging_filter" src="https://www.centerspace.net/blog/wp-content/uploads/2009/11/current_averaging_filter.jpg" alt="Averaging Filter, Length 5" width="400" height="316" /><figcaption id="caption-attachment-603" class="wp-caption-text">Averaging Filter, Length 5</figcaption></figure>
<figure id="attachment_605" aria-describedby="caption-attachment-605" style="width: 400px" class="wp-caption aligncenter"><img decoding="async" loading="lazy" class="size-full wp-image-605" title="current_savitzky-golay5" src="https://www.centerspace.net/blog/wp-content/uploads/2009/11/current_savitzky-golay5.jpg" alt="Savitzky-Golay Smoothing, Length = 5" width="400" height="316" /><figcaption id="caption-attachment-605" class="wp-caption-text">Savitzky-Golay Smoothing, Length 5</figcaption></figure>
<p>The averaging filter introduces a large error into the location of the orange peak whereas Savitzky-Golay removes the noise while maintaining the peak location.  Computationally, they require identical effort.</p>
<h3>Savitzky-Golay Smoothing in C#</h3>
<p>The Savitzky-Golay smoothing filter is implemented in the NMath-Stats package as a generalized correlation filter.  Any filter coefficients can be used with this moving window filter, Savitzky-Golay coefficients are just one possibility.  The moving window filter also does not require the filtering to take place in the center of the sliding window; so when specifying the window, two parameters are required: number to the left, and number to the right of the filtered data point.</p>
<p>Here are the key software components.</p>
<p><code></p>
<ul>
<li> MovingWindowFilter.<br />
SavitzkyGolayCoefficients(nLeft, nRight, PolyDeg)</li>
<li> MovingWindowFilter(nLeft, nRight, Coefficients[])</li>
<li> MovingWindowFilter.Filter(data, boundary options)</li>
</ul>
<p></code><br />
The first is a static function for generating the Savizky-Golay coefficients, the second is the filtering class that takes the generated coefficients in the constructor.  The third is the method that does the Savitzky-Golay filtering by running a cross-correlation between the data the the saved coefficients.</p>
<p>Below is a complete code example to copy and experiment with your own data sets.  Only three lines of code are needed to build the filter and actually do the filtering.  The remaining code builds a synthetic noisy signal to filter and displays the results.</p>
<pre lang="csharp">
int nLeft = 2;
int nRight = 2;
int n = 3;

// Generate the coefficients.
DoubleVector c =
MovingWindowFilter.SavitzkyGolayCoefficients( nLeft, nRight, n );

// Build the filter of width: nLeft + nRight + 1
MovingWindowFilter filter =
  new MovingWindowFilter( nLeft, nRight, c );
Console.WriteLine( "Filter coeffs = " + filter.Coefficients );

// Generate a signal
DoubleVector x = new DoubleVector( 100, -5, .1 );
DoubleVector y = new DoubleVector( x.Length );
for ( int i = 0; i &lt; x.Length; i++ )
{
  double a = x[i];
  y[i] = 0.03*Math.Pow(a, 3) + 0.2*Math.Pow(a, 2) -.22*a + 0.5;
}
Console.WriteLine( "Smoothed signal = " + y );

RandGenUniform rng = new RandGenUniform(-1, 1, 0x124 );
for ( int i = 0; i &lt; y.Length; i++ )
{
  y[i] += rng.NextDouble();
}
Console.WriteLine( "x = " + x );
Console.WriteLine( "Noisy signal = " + y );

// Do the filtering.
DoubleVector z =
filter.Filter(y, MovingWindowFilter.BoundaryOption.PadWithZeros);
Console.WriteLine( "Signal filtered = " + z );

</pre>
<p><em>-Paul </em></p>
<h3>Addendum &#8211; Savitzky-Golay Coefficients</h3>
<p>If you want to quickly try out Savitzky-Golay smoothing without computing the coefficients, or just compare coefficients, here are some coefficients for a sliding window of length five.  They also provide some insight into the relationship between the coefficients and the behavior of the filter.</p>
<pre class="code">
Filter length = 5, nLeft = 2, nRight = 2.
Polynomial of order 0,
[ 0.2 0.2 0.2 0.2 0.2 ]  (averaging filter)
Polynomial of order 1,
[ 0.2 0.2 0.2 0.2 0.2 ]
Polynomial of order 2,
[ -0.0857142 0.3428571 0.4857142 0.3428571 -0.0857142 ]
Polynomial of order 3,
[ -0.0857142 0.3428571 0.4857142 0.3428571 -0.0857142 ]
Polynomial of order 4 and higher,
[ 0 0 1 0 0 ]  (as expected, no filtering here!)
</pre>
<table border="0">
<tbody></tbody>
</table>
<h3>Another Smoothing Example on Real Data</h3>
<p>This is another example of Savitzky-Golay smoothing on some experimental data.  If more smoothing was desired, a longer filtering window could have been used.</p>
<figure id="attachment_602" aria-describedby="caption-attachment-602" style="width: 367px" class="wp-caption alignnone"><img decoding="async" loading="lazy" class="size-full wp-image-602" title="blue_rawdata_savitzky_golay5" src="https://www.centerspace.net/blog/wp-content/uploads/2009/11/blue_rawdata_savitzky_golay5.jpg" alt="Savitzky-Golay Smoothing Example, Length = 5." width="367" height="778" /><figcaption id="caption-attachment-602" class="wp-caption-text">Savitzky-Golay Smoothing, Length = 5.</figcaption></figure>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/savitzky-golay-smoothing">Savitzky-Golay Smoothing in C#</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/savitzky-golay-smoothing/feed</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">547</post-id>	</item>
	</channel>
</rss>
