Question

I have a .wav file and I am plotting waveform using ZedGraph. I am calculating the energies of .wav file of each second and if energy is less then 4 I want to draw the sample in different color. I have created two PointPairLlist and LineItem to do this but there is a problem while merging these two list. Here is my code and how my graph appears.

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);

enter image description here The lines of myCurveAudio and myCurveAudio2 intersect like in the picture.

How can I merge these two list preventing those intersections?

I have also tried to add `double.NaN to end of the lists but it did not work.

Was it helpful?

Solution 2

Gradient Fills to Color Line Segments

This tutorial helped me to solve my problem. There is no need two or more PointPairList Here is my code:

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;

OTHER TIPS

Try the code below, I Modified your code based on "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);

If the above code is not worked, follow the steps given in this thread

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top