我有一个.wav文件,我正在使用ZedGraph绘制波形。我正在计算每秒的.wav文件的能量,如果能量小于4,我想用不同的颜色绘制样本。我创建了两个PointPairLlist
和LineItem
来执行此操作,但在合并这两个列表时出现问题。这是我的代码以及我的图表的显示方式。
LineItem myCurveAudio;
LineItem myCurveAudio2;
PointPairList list1 = new PointPairList();
PointPairList list2 = new PointPairList();
while(true)
{
for (int i = 0; i < fmainBuffer.Length; i++)
{
float segmentSquare = fmainBuffer[i] * fmainBuffer[i];
listOfSquaredSegment.Add(segmentSquare);
}
float energy = (float)Math.Sqrt(listOfSquaredSegment.Sum());
if (energy < 4)
{
for (int i = 0; i < read; i += (int)window)
{
list1.Add((float)(count / ((float)read / (float)window)), fmainBuffer[i]);
count++;
}
}
else
{
for (int i = 0; i < read; i += (int)window)
{
list4.Add((float)(count / ((float)read / (float)window)), fmainBuffer[i]);
count++;
}
}
}
zgc.MasterPane.PaneList[1].XAxis.Scale.MaxAuto = true;
zgc.MasterPane.PaneList[1].XAxis.Scale.MinAuto = true;
zgc.MasterPane.PaneList[1].XAxis.Type = AxisType.Linear;
zgc.MasterPane.PaneList[1].XAxis.Scale.Format = "";
zgc.MasterPane.PaneList[1].XAxis.Scale.Min = 0;
myCurveAudio = zgc.MasterPane.PaneList[1].AddCurve(null, list1, Color.Lime, SymbolType.None);
myCurveAudio2 = zgc.MasterPane.PaneList[1].AddCurve(null, list4, Color.Red, SymbolType.None);
myCurveAudio和myCurveAudio2的线条如图所示相交。
如何合并这两个列表以防止这些交叉点?
我还尝试将`double.NaN添加到列表的末尾,但它不起作用。
答案 0 :(得分:0)
尝试以下代码,我根据“this thread”
修改了代码LineItem myCurveAudio;
LineItem myCurveAudio2;
PointPairList list1 = new PointPairList();
PointPairList list2 = new PointPairList();
double lastAddedHighEnergy = double.NaN;
double lastAddedLowEnergy = double.NaN;
while (true)
{
for (int i = 0; i < fmainBuffer.Length; i++)
{
float segmentSquare = fmainBuffer[i] * fmainBuffer[i];
listOfSquaredSegment.Add(segmentSquare);
}
float energy = (float)Math.Sqrt(listOfSquaredSegment.Sum());
if (energy < 4)
{
for (int i = 0; i < read; i += (int)window)
{
lastAddedLowEnergy = (double)(count / ((double)read / (double)window));
if (lastAddedHighEnergy != double.NaN)
{
list1.Add(lastAddedHighEnergy + ((lastAddedLowEnergy - lastAddedHighEnergy) / 2.0), double.NaN);
lastAddedHighEnergy = double.NaN;
}
list1.Add(lastAddedLowEnergy, fmainBuffer[i]);
count++;
}
}
else
{
for (int i = 0; i < read; i += (int)window)
{
lastAddedHighEnergy = (double)(count / ((double)read / (double)window));
if (lastAddedLowEnergy != double.NaN)
{
list2.Add(lastAddedLowEnergy + ((lastAddedHighEnergy - lastAddedLowEnergy) / 2.0), double.NaN);
lastAddedLowEnergy = double.NaN;
}
list2.Add(lastAddedHighEnergy, fmainBuffer[i]);
count++;
}
}
}
zgc.MasterPane.PaneList[1].XAxis.Scale.MaxAuto = true;
zgc.MasterPane.PaneList[1].XAxis.Scale.MinAuto = true;
zgc.MasterPane.PaneList[1].XAxis.Type = AxisType.Linear;
zgc.MasterPane.PaneList[1].XAxis.Scale.Format = "";
zgc.MasterPane.PaneList[1].XAxis.Scale.Min = 0;
myCurveAudio = zgc.MasterPane.PaneList[1].AddCurve(null, list1, Color.Lime, SymbolType.None);
myCurveAudio2 = zgc.MasterPane.PaneList[1].AddCurve(null, list2, Color.Red, SymbolType.None);
如果上述代码无效,请按照this thread
中给出的步骤操作答案 1 :(得分:0)
Gradient Fills to Color Line Segments
本教程帮助我解决了我的问题。不需要两个或更多PointPairList
这是我的代码:
float energy = (float)Math.Sqrt(listOfSquaredSegment.Sum());
for (int i = 0; i < fmainBuffer.Length; i += (int)window)
{
listaudio.Add((float)(count / ((float)8000 / (float)window)) / 5, fmainBuffer[i], energy > 4 ? 2.0 : 1.0);
count++;
}
myCurveAudio = zgc.MasterPane.PaneList[1].AddCurve(null, listaudio, Color.Lime, SymbolType.None);
Fill fill = new Fill(Color.Lime, Color.Red);
fill.RangeMin = 1;
fill.RangeMax = 2;
fill.Type = FillType.GradientByZ;
myCurveAudio.Line.GradientFill = fill;