C#实现消抖滤波算法 输入List<double>返回List<double>
时间: 2024-06-09 13:01:23 浏览: 134
消抖滤波算法可以用于去除输入信号中的噪声。具体实现如下:
```csharp
using System;
using System.Collections.Generic;
public class DebounceFilter
{
private List<double> inputSignal;
private double threshold;
public DebounceFilter(List<double> inputSignal, double threshold)
{
this.inputSignal = inputSignal;
this.threshold = threshold;
}
public List<double> ApplyFilter()
{
List<double> outputSignal = new List<double>();
for (int i = 0; i < inputSignal.Count; i++)
{
double currentValue = inputSignal[i];
if (i == 0 || Math.Abs(currentValue - outputSignal[outputSignal.Count - 1]) > threshold)
{
outputSignal.Add(currentValue);
}
}
return outputSignal;
}
}
```
使用示例:
```csharp
List<double> inputSignal = new List<double> { 1.2, 1.3, 1.5, 1.9, 2.1, 1.8, 0.9 };
double threshold = 0.5;
DebounceFilter filter = new DebounceFilter(inputSignal, threshold);
List<double> outputSignal = filter.ApplyFilter();
foreach (double value in outputSignal)
{
Console.WriteLine(value);
}
```
阅读全文