PDA

View Full Version : Live Plotting the data from 2D QVector of type QPointF



VitaminG
12th January 2017, 13:51
Using sources online I have managed to live plot 4 graphs of sensor values over time, however I want to use the values from the sensors to plot a live plot a 5th plot using the values from the previous plots. The variable sampleVector is a 2D array containing sensory values (Y-variable) over time (X-variable)

The formula for the 5th plot is: Plot5= -0.5 * (Plot0 + Plot1) ... over all the samples

Below is my attempt at achieving this, however it did not work. The 5th plot seemed to plot twice as fast like it was appending twice as many x-values. It also did not seem to like me multiplying a value such as "-0.5". I am new to Qt and I haven't found a way to manipulate and use the Y-values of "sampleVector". I don't quite understand the format of the variable either if that can be explained as well please.

Thank you in advance, please Help I am desperate.



void Plot::plotSampleVector(QVector<QVector<QPointF> > sampleVector){

//*** length of the data
const int sampleSize = sampleVector.length();

//***Append new values to "QVector<Plot *> d_plots"
for (int ii=0; ii< sampleSize; i++){

d_plots[0] -> AppendPoint(sampleVector.at(ii).at(0));
d_plots[1] -> AppendPoint(sampleVector.at(ii).at(1));
d_plots[2] -> AppendPoint(sampleVector.at(ii).at(2));
d_plots[3] -> AppendPoint(sampleVector.at(ii).at(3));
// Problem Code:
d_plots[4] -> AppendPoint(-0.5 * (sampleVector.at(ii).at(0) + sampleVector.at(ii).at(1)));

}

//***Draw Curves
for (int ii=0; ii<5; ii++){
d_plots[ii] ->DrawCurveSegment(sampleSize)
}

}

d_stranz
12th January 2017, 17:54
I don't see anywhere in your code where you are clearing the old points from your "d_plots" instances. This code continues to append new points. But then you tell each of these plots only to draw the last "sampleSize" points you have appended.

I don't know what your formula for the 5th plot is intended to do. It looks like you are trying to compute the average of the corresponding pairs of points in plots 1 and 2, but this formula doesn't do it. It is first adding together the two x coordinate values and the two y coordinate values, then dividing each of those new values by -2. This give you a new point that is in the quadrant on the opposite side of the origin from the sum of your original points, but only half as far away from the origin.

If what you -do- want is the average, then presumably it is the average of the y values only. I would guess that the x-values (sample time) for all of the plots should be the same if you are sampling sensors periodically. In this case, you want to leave the x-values unchanged and average only the y-values:



d_plots[4] -> AppendPoint( QPointF( sampleVector.at(ii).at(0).rx(), 0.5 * ( sampleVector.at(ii).at(0).ry() + sampleVector.at(ii).at(1).ry() ) ) );


If you do want the new line to be drawn on the opposite side of the x-axis, then change 0.5 to -0.5.

I would also change the signature of your method to take "const QVector< QVector < QPointF > > & sampleVector" as the argument (i.e. pass by reference), because your current signature "passes by value" meaning it makes an unnecessary copy of the vector on the way in, only to discard it when the method exits. Since you aren't modifying the contents of sampleVector in the method, using a const reference is also preferred since it may allow the compiler to optimize a bit better.