c# - 即时绘制幅度和频率不断变化的信号类型

标签 c# draw

正如标题所说:

我的表格有 2 个轨迹栏。一个用于频率,一个用于幅度。我设置了即时更改的计时器。

private void timer1_Tick(object sender, EventArgs e)
    {
        float amplitude, frequency;

        amplitude = Convert.ToSingle(trackBar1.Value) / 100;
        label1.Text = amplitude.ToString() + " V";

        frequency = trackBar2.Value;
        label2.Text = frequency.ToString() + " Hz";
    }

我还有 4 个单选按钮来决定将显示哪种信号类型(正弦波、方波、三角波、锯齿波)

现在我用 ImageList 实现了这个(改变信号的图像)。

如何绘制信号类型并使用轨迹栏对其进行调节?所以它就像在示波器中一样。

感谢您的回答和代码。

最佳答案

让我们从创建不同的信号类型开始,这是一个创建振幅为 1 的波长的函数:

private PointF[] CreateBaseSignal(SignalType signalType)
{
  switch (signalType)
  {
    case SignalType.Sine:
      const int oversampling = 32;
      PointF[] signal = new PointF[oversampling];
      for (int i = 0; i < signal.Length; i++)
      {
        signal[i].X = (float) i / oversampling;
        signal[i].Y = Convert.ToSingle(Math.Sin((double) i / oversampling * 2 * Math.PI));
      }
      return signal;

    case SignalType.Square:
      return new PointF[]
      {
        new PointF(0.0f, -1.0f),
        new PointF(0.5f, -1.0f),
        new PointF(0.5f, 1.0f),
        new PointF(1.0f, 1.0f),
      };

    case SignalType.Triangle:
      return new PointF[]
      {
        new PointF(0.0f, -1.0f),
        new PointF(0.5f, 1.0f),
      };

    case SignalType.Sawtooth:
      return new PointF[]
      {
        new PointF(0.0f, -1.0f),
        new PointF(1.0f, 1.0f),
      };

    default:
      throw new ArgumentException("Invalid signal type", "signalType");
  }
}

然后我们创建具有选定幅度和频率的实际信号:

private PointF[] CreateSignal(PointF[] baseSignal, float frequency, float amplitude)
{
  PointF[] signal = new PointF[Convert.ToInt32(Math.Ceiling(baseSignal.Length * frequency))];
  for(int i = 0; i < signal.Length; i++)
  {
    signal[i].X = baseSignal[i % baseSignal.Length].X / frequency + (i / baseSignal.Length) / frequency;
    signal[i].Y = baseSignal[i % baseSignal.Length].Y * amplitude;
  }
  return signal;
}

在尝试将此信号绘制到 PictureBox 之前,我们缩放信号以适应宽度和高度:

private PointF[] ScaleSignal(PointF[] signal, int width, int height)
{
  const float maximumAmplitude = 10.0f;
  PointF[] scaledSignal = new PointF[signal.Length];
  for(int i = 0; i < signal.Length; i++)
  {
    scaledSignal[i].X = signal[i].X * width;
    scaledSignal[i].Y = signal[i].Y * height / 2 / maximumAmplitude;
  }
  return scaledSignal;
}

使用 Graphics.DrawLine 绘制信号比 Bitmap.SetPixel 好得多,因为即使在高频下数据点也会连接起来。 Bitmap.SetPixel 也很慢,你真的需要使用 Bitmap.LockBits 和不安全的代码来操作单个像素来实现任何像样的性能。使用 Graphics.DrawLine,您还可以控制线宽、消除锯齿等。

由于我们已将信号存储在 PointF 数组中,因此我们可以使用简单的 Graphics.DrawLines 方法绘制信号,而不是遍历数据点:

private void PlotSignal(PointF[] signal, PictureBox pictureBox)
{
  Bitmap bmp = new Bitmap(pictureBox.ClientSize.Width, pictureBox.ClientSize.Height);
  signal = ScaleSignal(signal, bmp.Width, bmp.Height); // Scale signal to fit image

  using(Graphics gfx = Graphics.FromImage(bmp))
  {
    gfx.SmoothingMode = SmoothingMode.HighQuality;
    gfx.TranslateTransform(0, bmp.Height / 2); // Move Y=0 to center of image
    gfx.ScaleTransform(1, -1); // Make positive Y axis point upward
    gfx.DrawLine(Pens.Black, 0, 0, bmp.Width, 0); // Draw zero axis
    gfx.DrawLines(Pens.Blue, signal); // Draw signal
  }

  // Make sure the bitmap is disposed the next time around
  Image old = pictureBox.Image;
  pictureBox.Image = bmp;
  if(old != null)
    old.Dispose();
}

如果您经常重绘信号,您可能希望重复使用 Bitmap 和 Graphics 对象,而不是每次都创建新对象。请记住在每次重绘之间调用 Graphics.Clear。

将所有内容放在一个大声明中:

PlotSignal(
  CreateSignal(
    CreateBaseSignal(signalType),
    frequency,
    amplitude),
  thePictureBox);

关于c# - 即时绘制幅度和频率不断变化的信号类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10141161/

相关文章:

java - 多次绘制位图

android - 调试绘制和引擎

java - 让我的 Canvas 无需大修即可滚动

c# - LPR 进程期间未处理的 C# 异常

c# - UWP App 中未链接到 UI 的计时器

c# - 使用 Windows 虚拟邮件服务器发送带有 header 返回路径的电子邮件

android - 谷歌地图 v2 获取方向 Android

c# 正则表达式问题 : only letters, 数字和一个点(2 到 20 个字符)允许

c# - VSTO 打印机 API

python - 在 Python 中使用 GLUT/OpenGL 绘制文本