6

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.

Blast
  • 955
  • 1
  • 17
  • 40

2 Answers2

0

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

Community
  • 1
  • 1
petchirajan
  • 4,152
  • 1
  • 18
  • 20
  • This did not work. There are still intersections. Also I think the thread that you linked explains same think with what my code does. – Blast Mar 06 '14 at 10:42
  • did you tried http://stackoverflow.com/questions/12503746/zedgraph-curve-with-multiple-segments – petchirajan Mar 06 '14 at 11:30
  • I modified the above code. Previously I didn't take the center point for plot the empty point for breaking the line. Now, I calculated the center X-Axis point and added the empty point in the curve. Try the above code now.. – petchirajan Mar 06 '14 at 11:34
  • I tried both the link that you have shared and your code. There are still instersections because I have to merge 6 or more data according to my file length. This code only prevent intersection of first two part of data. – Blast Mar 06 '14 at 11:42
0

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;
Blast
  • 955
  • 1
  • 17
  • 40