酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

请用C#写一份完整的Savitzky-Golay filter算法 语言方向:C#

Savitzky-Golay滤波器是一种常用于数据平滑处理的数字滤波器,它通过局部多项式回归来平滑数据。以下是一个用C#实现的Savitzky-Golay滤波器的示例代码: ```csharp using System; using System.Linq; public class SavitzkyGolayFilter { private int windowSize; private int polynomialOrder; private double[] coefficients; public SavitzkyGolayFilter(int windowSize, int polynomialOrder) { if (windowSize % 2 == 0) throw new ArgumentException("Window size must be odd."); if (polynomialOrder >= windowSize) throw new ArgumentException("Polynomial order must be less than window size."); this.windowSize = windowSize; this.polynomialOrder = polynomialOrder; this.coefficients = CalculateCoefficients(); } private double[] CalculateCoefficients() { int halfWindow = windowSize / 2; var A = new double[windowSize, polynomialOrder + 1]; for (int i = -halfWindow; i <= halfWindow; i++) { for (int j = 0; j <= polynomialOrder; j++) { A[i + halfWindow, j] = Math.Pow(i, j); } } var AT = TransposeMatrix(A); var ATA = MultiplyMatrices(AT, A); var ATAInverse = InvertMatrix(ATA); var ATAInverseAT = MultiplyMatrices(ATAInverse, AT); return ATAInverseAT.GetRow(halfWindow); } public double[] ApplyFilter(double[] data) { int halfWindow = windowSize / 2; var smoothedData = new double[data.Length]; for (int i = 0; i < data.Length; i++) { double sum = 0; for (int j = -halfWindow; j <= halfWindow; j++) { int index = i + j; if (index < 0) index = 0; if (index >= data.Length) index = data.Length - 1; sum += coefficients[j + halfWindow] * data[index]; } smoothedData[i] = sum; } return smoothedData; } private double[,] TransposeMatrix(double[,] matrix) { int rows = matrix.GetLength(0); int cols = matrix.GetLength(1); var result = new double[cols, rows]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[j, i] = matrix[i, j]; } } return result; } private double[,] MultiplyMatrices(double[,] matrix1, double[,] matrix2) { int rows1 = matrix1.GetLength(0); int cols1 = matrix1.GetLength(1); int rows2 = matrix2.GetLength(0); int cols2 = matrix2.GetLength(1); if (cols1 != rows2) throw new ArgumentException("Matrix dimensions do not match for multiplication."); var result = new double[rows1, cols2]; for (int i = 0; i < rows1; i++) { for (int j = 0; j < cols2; j++) { for (int k = 0; k < cols1; k++) { result[i, j] += matrix1[i, k] * matrix2[k, j]; } } } return result; } private double[,] InvertMatrix(double[,] matrix) { int n = matrix.GetLength(0); var result = new double[n, n]; var identity = new double[n, n]; for (int i = 0; i < n; i++) { identity[i, i] = 1; } for (int i = 0; i < n; i++) { double pivot = matrix[i, i]; for (int j = 0; j < n; j++) { matrix[i, j] /= pivot; identity[i, j] /= pivot; } for (int k = 0; k < n; k++) { if (k != i) { double factor = matrix[k, i]; for (int j = 0; j < n; j++) { matrix[k, j] -= factor * matrix[i, j]; identity[k, j] -= factor * identity[i, j]; } } } } return identity; } } public static class MatrixExtensions { public static double[] GetRow(this double[,] matrix, int row) { int cols = matrix.GetLength(1); var result = new double[cols]; for (int i = 0; i < cols; i++) { result[i] = matrix[row, i]; } return result; } } class Program { static void Main() { var data = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var filter = new SavitzkyGolayFilter(5, 2); var smoothedData = filter.ApplyFilter(data); Console.WriteLine("Smoothed Data:"); foreach (var value in smoothedData) { Console.WriteLine(value); } } } ``` 代码说明: 1. SavitzkyGolayFilter类:这是Savitzky-Golay滤波器的主要实现类。它包含计算滤波器系数和应用滤波器的方法。 2. CalculateCoefficients方法:计算Savitzky-Golay滤波器的系数矩阵。 3. ApplyFilter方法:应用滤波器到输入数据上,返回平滑后的数据。 4. 矩阵操作:包括矩阵转置、矩阵乘法和矩阵求逆等辅助方法。 注意事项: 1. 窗口大小:窗口大小必须是奇数,以确保滤波器对称。 2. 多项式阶数:多项式阶数必须小于窗口大小,以避免过拟合。 3. 边界处理:在应用滤波器时,边界点的处理需要特别注意,通常通过镜像或截断来处理边界数据。 [2025-03-01 14:38:11 | AI写代码神器 | 1378点数解答]

相关提问