Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Arithmetic mean of selected data region

Solved!
Go to solution

Hello there i need help visual .net c# :

 

I want to know arithmetic mean of ploty values, between two user selected points.

 

I think selection of points will be easy with cursor, when user click on data point, cursor will snap pointy.

 

QShot_0000.jpg 

 

But how i can select whole data points in these user selected region and get arithmetic mean of these y values? 

 

 

QShot_0001.jpg 

 

QShot_0002.jpg

 

Thanks for your kindly help. 

 

0 Kudos
Message 1 of 8
(5,576 Views)

Hello there,

 

I quickly worked on what you had asked and got it working for me. Here is the code sample.

 

Things to note:

  1. This is for a waveform graph.
  2. The xyCursors have SnapMode set to ToPlot.

 

        private void xyCursor1_AfterMove(object sender, NationalInstruments.UI.AfterMoveXYCursorEventArgs e)
        {
            double value = GetAverage();
            label1.Text = value.ToString(); //Use the average value to display
        }

        private void xyCursor2_AfterMove(object sender, NationalInstruments.UI.AfterMoveXYCursorEventArgs e)
        {
            double value = GetAverage();
            label1.Text = value.ToString(); //Use the average value to display
        }

        // Method that calculates avg (or something that you want)
        private double GetAverage()
        {
            // Note that the SnapMode of both cursors are set to ToPlot.
            
            int startIndex = (int)(xyCursor1.XPosition * waveformPlot1.DefaultIncrement);
            int endIndex = (int)(xyCursor2.XPosition * waveformPlot1.DefaultIncrement);

            if (startIndex > endIndex)
            {
                int temp = endIndex;
                endIndex = startIndex;
                startIndex = temp;
            }

            double[] data = waveformPlot1.GetYData();
            data = GetMyDataInterval(data, startIndex, endIndex);

            double value = CalculateAverage(data);
            return value;
        }

        // Method to do some arithmetic on the array values
        private double CalculateAverage(double[] data)
        {
            // Here you performe the calculation of Average or Mean or what ever you want
            double avg = 0;

            for (int i = 0; i < data.Length; i++ )
            {
                avg += data[i];
            }

            avg = avg / data.Length;

            return avg;
        }

        // Method that returns valid data between two indices
        private double[] GetMyDataInterval(double[] originalData, int startIndex, int endIndex)
        {
            double[] data = new double[endIndex - startIndex + 1];

            for (int i = startIndex; i <= endIndex; i++)
            {
                data[i - startIndex] = originalData[i];
            }

            return data;
        }

 

I hope this helps for you. Let us know if you wanted something else.

 

Vijet Patankar

National Instruments

Message 2 of 8
(5,570 Views)

Thanks a lot Vijet it works fine. But now i want to give number to regions. At first, user select two points of regions. After selecting regions program gives averages like reg1average, reg2average... In addition, user must see regions after selection. So i want to change regions visual styles.  Also there can be labels above regions like area 1, area 2...

 

For example  if there are 8 region, there will be 16 xycursor. It means mess. And how can know region count before user state them? I think after selecting 2 points with xycursor, program must change color and size of points. And finally, i think it will be useful if keep reg1average values in array.

 

Thanks  

 

 

0 Kudos
Message 3 of 8
(5,557 Views)

Hello there,

 

I saw your requirement requirement and you are right for the fact that using curors is going to be a lot of mess. Becuase when you are dealing with many cursors, it is difficult to pair the curosrs even though the cursors pairs have different colors. It would look bad.

 

This is the part where you could use the XYRangeAnnotations. The only issue with the annotations that you can only drag the caption but, you cannot resize or move the annotations. However, if you can achieve the resize and move for the annotations, you can make the application look a lot better when you have many annotations in the graph.

 

Here is the screenshot that I implemented for with the annotations (replacing the cursors).

 

AnnotationDrag.png

 


Misc:

  • Here I used the XYRangeAnnotation instead of XYCursor.
  • The Annotations in the graph does not allow dragging the annotation itself. I implemented them manually. (I have not handled all the boundary conditions).
  • Notice the number in White at the top of the annotations are the average that you are interested in dispalying. I can display if anymore information where I want. 
  • I would say this is much neater compared to the XYCursor.
  • You can randomize color genertion as you want easily.
  • You can add as many number of annotations you want.
  • You could also remove any annotations if I desire.

 

I have attached the code using the annotations.

 

Hope this helps.

 

Vijet Patankar,

National Instruments

 

Message 4 of 8
(5,541 Views)

i will try it.

how i can add  user clicked data points to double[] ? 

0 Kudos
Message 5 of 8
(5,526 Views)
Solution
Accepted by topic author isobio
  • Use WaveformPlot.MapDataPoint(...) to convert x and y data values to Point (i.e. in pixels)
  • Use WaveformPlot.InverseMapDataPoint(...) to Point to x and y data values.
  • Use the WaveformPlot.GetYData() to get the data that you plotted.
  • Use the WaveformPlot.GetXData() to get the data coordinates of the each point you plotted on the graph. By default the value in the XData array [If you used the PlotY method(s)] are basically calculated from the WaveformPlot.DefaultIncrement and WaveformPlot.DefaultStart. So using these you can calculate index of the double[].

Use above methods to do all the tricks you want to perform.

For example, To get the x and y data where the mouse clicked, you just use the WaveformPlot.InverseMapDataPoint() method passing the location of the mouse click.

0 Kudos
Message 6 of 8
(5,512 Views)

I have one question, how i can find maximum and minimum Y points between x1-x2 ?

0 Kudos
Message 7 of 8
(5,499 Views)

i find it thanks. 

0 Kudos
Message 8 of 8
(5,496 Views)