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); }
/// <summary> /// Processes the spectra in matrix xMatrix for prediction. /// </summary> /// <param name="xMatrix">The matrix of spectra. Each spectrum is a row of the matrix.</param> /// <param name="xMean">Not used.</param> /// <param name="xScale">Not used.</param> /// <param name="regionstart">Starting index of the region to process.</param> /// <param name="regionend">End index of the region to process.</param> public void ProcessForPrediction(IMatrix xMatrix, IROVector xMean, IROVector xScale, int regionstart, int regionend) { int regionlength = regionend - regionstart; IVector helpervector = VectorMath.ToVector(new double[regionlength]); for(int n=0;n<xMatrix.Rows;n++) { IVector vector = MatrixMath.RowToVector(xMatrix,n,regionstart,regionlength); _filter.Apply(vector,helpervector); VectorMath.Copy(helpervector,vector); } }
/// <summary> /// Processes the spectra in matrix xMatrix for prediction. /// </summary> /// <param name="xMatrix">The matrix of spectra. Each spectrum is a row of the matrix.</param> /// <param name="xMean">Not used.</param> /// <param name="xScale">Not used.</param> /// <param name="regionstart">Starting index of the region to process.</param> /// <param name="regionend">End index of the region to process.</param> public void ProcessForPrediction(IMatrix <double> xMatrix, IReadOnlyList <double> xMean, IReadOnlyList <double> xScale, int regionstart, int regionend) { int regionlength = regionend - regionstart; var helpervector = VectorMath.ToVector(new double[regionlength]); for (int n = 0; n < xMatrix.RowCount; n++) { var vector = MatrixMath.RowToVector(xMatrix, n, regionstart, regionlength); _filter.Apply(vector, helpervector); VectorMath.Copy(helpervector, vector); } }
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(); } }