// [TestMethod()] public void SavitzkyGolayTest() { // int winSize = 5; // TODO: 初始化为适当的值 var target = new SavitzkyGolay(31); // TODO: 初始化为适当的值 var matReader = new MatlabMatrixReader <double>(@"F:\3506\15chemometrics\RIPP_DEMO\src\RIPP\testdata\X.mat"); var input = matReader.ReadMatrix(); var reader2 = new MatlabMatrixReader <double>(@"F:\3506\15chemometrics\RIPP_DEMO\src\RIPP\testdata\filter\out_smooth_31.mat"); var expected = reader2.ReadMatrix(); var actual = target.Process((Matrix)input, RIPP.Lib.MathLib.VectorType.Row); var writer = new MatlabMatrixWriter(@"F:\3506\15chemometrics\RIPP_DEMO\src\RIPP\testdata\filter\out_smooth_31_my.mat"); writer.WriteMatrix <double>(actual, "dd"); writer.Close(); target = new SavitzkyGolay(21); actual = target.Process((Matrix)input, RIPP.Lib.MathLib.VectorType.Row); writer = new MatlabMatrixWriter(@"F:\3506\15chemometrics\RIPP_DEMO\src\RIPP\testdata\filter\out_smooth_21_my.mat"); writer.WriteMatrix <double>(actual, "dd"); writer.Close(); // Assert.Inconclusive("验证此测试方法的正确性。"); }
/// <summary>This sets up a Savitzky-Golay-Filtering for all spectra.</summary> /// <param name="numberOfPoints">Number of points. Must be an odd number, otherwise it is rounded up.</param> /// <param name="derivativeOrder">Order of derivative you want to obtain. Set 0 for smothing.</param> /// <param name="polynomialOrder">Order of the fitting polynomial. Usual values are 2 or 4.</param> public SavitzkyGolayCorrection(int numberOfPoints, int derivativeOrder, int polynomialOrder) { _numberOfPoints = numberOfPoints; _derivativeOrder = derivativeOrder; _polynomialOrder = polynomialOrder; _filter = new SavitzkyGolay(numberOfPoints,derivativeOrder,polynomialOrder); }
public static void SavitzkyGolayFiltering(WorksheetController ctrl) { if (ctrl.SelectedDataColumns.Count == 0) { return; } object paramobject = new SavitzkyGolayParameters(); if (!Current.Gui.ShowDialog(ref paramobject, "Savitzky-Golay parameters")) { return; } SavitzkyGolayParameters parameters = (SavitzkyGolayParameters)paramobject; Altaxo.Data.DataColumn yCol = ctrl.Doc.DataColumns[ctrl.SelectedDataColumns[0]]; Altaxo.Data.DataColumn xCol = ctrl.Doc.DataColumns.FindXColumnOf(yCol); double spacing = 1; if (xCol is Data.INumericColumn) { Calc.LinearAlgebra.VectorSpacingEvaluator calcspace = new Calc.LinearAlgebra.VectorSpacingEvaluator(Calc.LinearAlgebra.DataColumnWrapper.ToROVector(xCol)); if (!calcspace.HasValidSpaces || calcspace.HasInvalidSpaces) { Current.Gui.ErrorMessageBox(string.Format("The x-column {0} contains invalid spaces (is not equally spaced)", xCol.Name)); return; } if (calcspace.RelativeSpaceDeviation > 1E-2) { System.Windows.Forms.DialogResult dlgresult = System.Windows.Forms.MessageBox.Show(Current.MainWindow, string.Format("The x-column {0} is not equally spaced, the deviation is {1}, the mean spacing is {2}. Continue anyway?", xCol.Name, calcspace.RelativeSpaceDeviation, calcspace.SpaceMeanValue), "Continue?", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question, System.Windows.Forms.MessageBoxDefaultButton.Button1); if (dlgresult == System.Windows.Forms.DialogResult.No) { return; } } spacing = calcspace.SpaceMeanValue; } Calc.Regression.SavitzkyGolay filter = new SavitzkyGolay(parameters); yCol.Suspend(); filter.Apply(DataColumnWrapper.ToROVectorCopy(yCol), DataColumnWrapper.ToVector(yCol)); if (parameters.DerivativeOrder > 0) { double factor = Math.Pow(1 / spacing, parameters.DerivativeOrder) * Calc.GammaRelated.Fac(parameters.DerivativeOrder); yCol.Data = yCol * factor; } yCol.Resume(); }
public static double[] FilterBySavitzkyGolay(FiltersConfig filterConfig, double[] eyeCoords) { double[] smoothedResult = new double[eyeCoords.Length]; var filterSavitzkyGolay = new SavitzkyGolay(filterConfig.SavitzkyGolayNumberOfPoints, filterConfig.SavitzkyGolayDerivativeOrder, filterConfig.SavitzkyGolayPolynominalOrder); filterSavitzkyGolay.Apply(eyeCoords, smoothedResult); eyeCoords = smoothedResult; return(eyeCoords); }
public static void SavitzkyGolay(SavitzkyGolayParameters parameters, Altaxo.Data.DataColumn yCol, Altaxo.Data.DataColumn xCol) { double spacing = 1; if (xCol is Data.INumericColumn) { var calcspace = new Calc.LinearAlgebra.VectorSpacingEvaluator(Calc.LinearAlgebra.DataColumnWrapper.ToROVector(xCol)); if (!calcspace.HasValidSpaces || calcspace.HasInvalidSpaces) { Current.Gui.ErrorMessageBox(string.Format("The x-column {0} contains invalid spaces (is not equally spaced)", xCol.Name)); return; } if (calcspace.RelativeSpaceDeviation > 1E-2) { if (!Current.Gui.YesNoMessageBox( string.Format("The x-column {0} is not equally spaced, the deviation is {1}, the mean spacing is {2}. Continue anyway?", xCol.Name, calcspace.RelativeSpaceDeviation, calcspace.SpaceMeanValue), "Continue?", true)) { return; } } spacing = calcspace.SpaceMeanValue; } var filter = new SavitzkyGolay(parameters); using (var suspendToken = yCol.SuspendGetToken()) { filter.Apply(DataColumnWrapper.ToROVectorCopy(yCol), DataColumnWrapper.ToVector(yCol)); if (parameters.DerivativeOrder > 0) { double factor = Math.Pow(1 / spacing, parameters.DerivativeOrder) * Calc.GammaRelated.Fac(parameters.DerivativeOrder); yCol.Data = yCol * factor; } suspendToken.Dispose(); } }
// [TestMethod] public void SpectraFitTest() { var matReader = new MatlabMatrixReader <double>(@"F:\3506\15chemometrics\RIPP_DEMO\src\RIPP\testdata\cx.mat"); var cx = matReader.ReadMatrix(); matReader = new MatlabMatrixReader <double>(@"F:\3506\15chemometrics\RIPP_DEMO\src\RIPP\testdata\vx.mat"); var vx = matReader.ReadMatrix(); var f1 = new Sgdiff(); f1.P = 2; var cx1 = f1.Process((Matrix)cx); var writer = new MatlabMatrixWriter(@"F:\3506\15chemometrics\RIPP_DEMO\src\RIPP\testdata\fit\cx1.mat"); writer.WriteMatrix <double>(cx1, "cx1"); writer.Close(); var f2 = new SavitzkyGolay(5); var cx2 = f2.Process((Matrix)cx1); writer = new MatlabMatrixWriter(@"F:\3506\15chemometrics\RIPP_DEMO\src\RIPP\testdata\fit\cx2.mat"); writer.WriteMatrix <double>(cx2, "cx2"); writer.Close(); var f3 = new VarRegionManu(); f3.Xaxis = new DenseVector(cx.RowCount); for (int i = 1; i <= cx.RowCount; i++) { f3.Xaxis[i - 1] = i; } var a = f3.Argus; a["Xaxis"].Value = f3.Xaxis; f3.XaxisRegion = new System.Collections.Generic.List <RegionPoint>(); f3.XaxisRegion.Add(new RegionPoint(131, 314)); f3.XaxisRegion.Add(new RegionPoint(468, 677)); a["XaxisRegion"].Value = f3.XaxisRegion; f3.Argus = a; var lst = new System.Collections.Generic.List <Vector>(); for (int i = 0; i < cx2.ColumnCount; i++) { lst.Add(f3.VarProcess((Vector)cx2.Column(i))); } var cx3 = new DenseMatrix(lst[0].Count, cx2.ColumnCount); for (int i = 0; i < cx2.ColumnCount; i++) { cx3.SetColumn(i, lst[i]); } writer = new MatlabMatrixWriter(@"F:\3506\15chemometrics\RIPP_DEMO\src\RIPP\testdata\fit\cx3.mat"); writer.WriteMatrix <double>(cx3, "cx3"); writer.Close(); var f4 = new NormPathLength(); var cx4 = f4.Process(cx3); writer = new MatlabMatrixWriter(@"F:\3506\15chemometrics\RIPP_DEMO\src\RIPP\testdata\fit\cx4.mat"); writer.WriteMatrix <double>(cx4, "cx4"); writer.Close(); matReader = new MatlabMatrixReader <double>(@"F:\3506\15chemometrics\RIPP_DEMO\src\RIPP\testdata\fit\mcx4.mat"); cx4 = (Matrix)matReader.ReadMatrix(); var fitr = new DenseMatrix(cx4.ColumnCount - 1, cx4.ColumnCount); for (int i = 0; i < cx4.ColumnCount; i++) { var subm = new DenseMatrix(cx4.RowCount, cx4.ColumnCount - 1); int tag = 0; for (int k = 0; k < cx4.ColumnCount; k++) { if (k == i) { continue; } subm.SetColumn(tag, cx4.Column(k)); tag++; } var fr = SpectraFit.Fit((Matrix)subm, (Vector)cx4.Column(i)); fitr.SetColumn(i, fr); } writer = new MatlabMatrixWriter(@"F:\3506\15chemometrics\RIPP_DEMO\src\RIPP\testdata\fit\fitr.mat"); writer.WriteMatrix <double>(fitr, "fitr"); writer.Close(); }