← All NMath Code Examples
Imports System
Imports CenterSpace.NMath.Core
Namespace CenterSpace.NMath.Examples.VisualBasic
A .NET example in Visual Basic
Module SavitzkyGolayFilteringExample
Sub Main()
Build a Savitzky-Golay filter with a window width of 7, and a 4th degree smoothing polynomial.
Dim SGF As New SavitzkyGolayFilter(3, 3, 4)
Make some random noise.
Dim RND As RandomNumberGenerator = New RandGenUniform()
Dim Data As New DoubleVector(100, RND)
Build a noisy sinusoidal signal to filter.
Dim step_size As Double = 0.1
Dim X As New DoubleVector(100, 0, step_size)
Dim sin_x As New DoubleVector(NMathFunctions.Sin(X) + Data.Scale(0.1))
Filter the signal function
Dim Z As DoubleVector = SGF.Filter(sin_x)
Build a vector of a sampled sinc() function and its derivative.
X = New DoubleVector(100, 0.01, step_size)
Dim Sinc As New DoubleVector(NMathFunctions.Sin(X) / X)
Dim derivative_sinc As New DoubleVector(NMathFunctions.Cos(X) / X - NMathFunctions.Sin(X) / (X * X))
Create a Savitzky-Golay filter for computing the first derivative using a 5th degree polynomial.
By default the boundaries are smoothed to the edges, if this is not necessary,
other faster boundary handling options are available.
SGF = New SavitzkyGolayFilter(3, 3, 5, 1)
Find the S-G derivatives.
Dim sg_d_sinc As DoubleVector = SGF.Filter(Sinc)
Scale the raw derivatives.
SGF.ScaleDerivative(step_size, sg_d_sinc)
Look at the mean squared error over the 100 samples, of the SG derivatives.
Dim mean_sqrd_error As Double = NMathFunctions.Mean(NMathFunctions.Pow(derivative_sinc - sg_d_sinc, 2.0))
Console.WriteLine()
Console.WriteLine("The mean squared error of the Savitzky-Golay derivative estimate of the sinc() function = {0}", mean_sqrd_error)
Console.WriteLine()
Console.WriteLine("Press Enter Key")
Console.Read()
End Sub
End Module
End Namespace
← All NMath Code Examples