gpt4 book ai didi

c# - NAudio 和 ZedGraph 波形绘图

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:14 25 4
gpt4 key购买 nike

    int window;
LineItem myCurve1;
LineItem myCurve2;
public void DrawWave(ZedGraphControl zgc)
{

NAudio.Wave.WaveChannel32 wave = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(open.FileName));
WaveFileReader wavFile = new WaveFileReader(open.FileName);
byte[] mainBuffer = new byte[wave.Length];

float fileSize = (float)wavFile.Length / 1048576;
if (fileSize < 2)
window = 8;
else if (fileSize > 2 && fileSize < 4)
window = 16;
else if (fileSize > 4 && fileSize < 8)
window = 32;
else if (fileSize > 8 && fileSize < 12)
window = 128;
else if (fileSize > 12 && fileSize < 20)
window = 256;
else if (fileSize > 20 && fileSize < 30)
window = 512;
else
window = 2048;

float[] fbuffer = new float[mainBuffer.Length / window];
wave.Read(mainBuffer, 0, mainBuffer.Length);

for (int i = 0; i < fbuffer.Length; i++)
{
fbuffer[i] = (BitConverter.ToSingle(mainBuffer, i * window));
}

double time = wave.TotalTime.TotalSeconds;
GraphPane myPane = zgc.GraphPane;
PointPairList list1 = new PointPairList();
PointPairList list2 = new PointPairList();
for (int i = 0; i < fbuffer.Length; i++)
{
list1.Add(i, fbuffer[i]);
}
list2.Add(0, 0);
list2.Add(time, 0);
if (myCurve1 != null && myCurve2 != null)
{
myCurve1.Clear();
myCurve2.Clear();
}

myCurve1 = myPane.AddCurve(null, list1, Color.Red, SymbolType.None);
myCurve1.IsX2Axis = true;
myCurve2 = myPane.AddCurve(null, list2, Color.Black, SymbolType.None);
myPane.XAxis.Scale.MaxAuto = true;
myPane.XAxis.Scale.MinAuto = true;
zgc.AxisChange();
zgc.Invalidate();
}

我有这段代码可以在 ZedGraph 中绘制波形。除了一件事,它工作正常。绘制波形后,我选择了一个新文件来绘制,但我必须清除旧文件。我尝试在全局中定义 LineItem,这样我就可以编写 if(myCruver != null) myCurve.Clear(); 这个条件。但是它不会清除旧绘图。我应该怎么做才能解决这个问题?

最佳答案

    int window;
LineItem myCurve1;
LineItem myCurve2;
public void DrawWave(ZedGraphControl zgc)
{

NAudio.Wave.WaveChannel32 wave = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(open.FileName));
WaveFileReader wavFile = new WaveFileReader(open.FileName);
byte[] mainBuffer = new byte[wave.Length];

float fileSize = (float)wavFile.Length / 1048576;
if (fileSize < 2)
window = 8;
else if (fileSize > 2 && fileSize < 4)
window = 16;
else if (fileSize > 4 && fileSize < 8)
window = 32;
else if (fileSize > 8 && fileSize < 12)
window = 128;
else if (fileSize > 12 && fileSize < 20)
window = 256;
else if (fileSize > 20 && fileSize < 30)
window = 512;
else
window = 2048;

float[] fbuffer = new float[mainBuffer.Length / window];
wave.Read(mainBuffer, 0, mainBuffer.Length);

for (int i = 0; i < fbuffer.Length; i++)
{
fbuffer[i] = (BitConverter.ToSingle(mainBuffer, i * window));
}

double time = wave.TotalTime.TotalSeconds;
GraphPane myPane = zgc.GraphPane;
PointPairList list1 = new PointPairList();
PointPairList list2 = new PointPairList();
for (int i = 0; i < fbuffer.Length; i++)
{
list1.Add(i, fbuffer[i]);
}
list2.Add(0, 0);
list2.Add(time, 0);
if (myCurve1 != null && myCurve2 != null)
{
myCurve1.Clear();
myCurve2.Clear();
}

myCurve1 = myPane.AddCurve(null, list1, Color.Red, SymbolType.None);
myCurve1.IsX2Axis = true;
myCurve2 = myPane.AddCurve(null, list2, Color.Black, SymbolType.None);
myPane.XAxis.Scale.MaxAuto = true;
myPane.XAxis.Scale.MinAuto = true;
zgc.AxisChange();
zgc.Invalidate();
}

我只是将 list1 和 list2 的 LineItems 分开了。现在它工作正常。

关于c# - NAudio 和 ZedGraph 波形绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18871547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com