CSHARP生成一个带噪声的实时正弦信号绘制折线图,并使用S-G滤波进行实时处理,处理后的数据绘制折线图
时间: 2024-05-01 22:19:13 浏览: 226
C# 绘制实时折线图,波形图
下面是一个C#代码示例,用于生成带噪声的实时正弦信号,并使用S-G滤波进行实时处理:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.DataVisualization.Charting;
namespace RealTimeSinWaveWithSGFilter
{
public partial class Form1 : Form
{
// 绘图相关变量
private Chart chart1;
private Series series1;
private Series series2;
// 实时信号相关变量
private double time = 0;
private double frequency = 2 * Math.PI * 0.2;
private double amplitude = 5;
private double noiseAmplitude = 1;
private Random rand = new Random();
// S-G滤波相关变量
private int windowSize = 5;
private int polynomialOrder = 2;
private SavitzkyGolayFilter sgFilter;
public Form1()
{
InitializeComponent();
// 初始化绘图
chart1 = new Chart();
chart1.Parent = this;
chart1.Dock = DockStyle.Fill;
series1 = new Series("Original Signal");
series2 = new Series("Filtered Signal");
chart1.Series.Add(series1);
chart1.Series.Add(series2);
// 初始化S-G滤波器
sgFilter = new SavitzkyGolayFilter(windowSize, polynomialOrder);
// 开始实时采集和绘制
timer1.Interval = 50; // 50ms采集一次
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
// 生成带噪声的正弦信号
double signal = amplitude * Math.Sin(frequency * time) + noiseAmplitude * rand.NextDouble();
// 绘制原始信号
series1.Points.AddXY(time, signal);
// S-G滤波处理
double filteredSignal = sgFilter.Filter(signal);
// 绘制滤波后的信号
series2.Points.AddXY(time, filteredSignal);
// 更新时间
time += 0.05;
}
}
public class SavitzkyGolayFilter
{
private int windowSize;
private int polynomialOrder;
private double[,] coefficients;
public SavitzkyGolayFilter(int windowSize, int polynomialOrder)
{
this.windowSize = windowSize;
this.polynomialOrder = polynomialOrder;
// 计算系数矩阵
int halfWindowSize = windowSize / 2;
double sum;
coefficients = new double[windowSize, polynomialOrder + 1];
for (int i = -halfWindowSize; i <= halfWindowSize; i++)
{
for (int j = 0; j <= polynomialOrder; j++)
{
sum = 0;
for (int k = -halfWindowSize; k <= halfWindowSize; k++)
{
sum += Math.Pow(k, j);
}
coefficients[i + halfWindowSize, j] = sum;
}
}
}
public double Filter(double[] data, int index)
{
double sum = 0;
int halfWindowSize = windowSize / 2;
for (int i = -halfWindowSize; i <= halfWindowSize; i++)
{
if (index + i < 0 || index + i >= data.Length)
{
continue;
}
sum += data[index + i] * coefficients[i + halfWindowSize, 0];
}
for (int j = 1; j <= polynomialOrder; j++)
{
double s = 0;
for (int i = -halfWindowSize; i <= halfWindowSize; i++)
{
if (index + i < 0 || index + i >= data.Length)
{
continue;
}
s += data[index + i] * coefficients[i + halfWindowSize, j];
}
sum += s * Math.Pow(0.05, j);
}
return sum;
}
public double Filter(double data)
{
throw new NotImplementedException();
}
}
}
```
这段代码实现了一个基于Windows Form的实时正弦信号绘制折线图,并使用S-G滤波进行实时处理的程序。其中,S-G滤波器的代码由Savitzky和Golay在1964年提出,通过多项式拟合的方式对信号进行平滑滤波。在本代码中,我们通过`SavitzkyGolayFilter`类实现了S-G滤波器,可以通过构造函数指定滑动窗口大小和多项式阶数,通过`Filter`方法对信号进行滤波处理。在`Form1`类中的`timer1_Tick`方法中,我们每次生成一个带噪声的正弦信号,并通过`SavitzkyGolayFilter`类对其进行滤波处理,然后将原始信号和滤波后的信号绘制到`Chart`控件上。这样就可以实现一个实时正弦信号绘制折线图,并使用S-G滤波进行实时处理的程序了。
阅读全文