Results 1 to 2 of 2

Thread: Live Plotting the data from 2D QVector of type QPointF

  1. #1
    Join Date
    Jan 2017
    Posts
    6
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Live Plotting the data from 2D QVector of type QPointF

    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.

    Qt Code:
    1. void Plot::plotSampleVector(QVector<QVector<QPointF> > sampleVector){
    2.  
    3. //*** length of the data
    4. const int sampleSize = sampleVector.length();
    5.  
    6. //***Append new values to "QVector<Plot *> d_plots"
    7. for (int ii=0; ii< sampleSize; i++){
    8.  
    9. d_plots[0] -> AppendPoint(sampleVector.at(ii).at(0));
    10. d_plots[1] -> AppendPoint(sampleVector.at(ii).at(1));
    11. d_plots[2] -> AppendPoint(sampleVector.at(ii).at(2));
    12. d_plots[3] -> AppendPoint(sampleVector.at(ii).at(3));
    13. // Problem Code:
    14. d_plots[4] -> AppendPoint(-0.5 * (sampleVector.at(ii).at(0) + sampleVector.at(ii).at(1)));
    15.  
    16. }
    17.  
    18. //***Draw Curves
    19. for (int ii=0; ii<5; ii++){
    20. d_plots[ii] ->DrawCurveSegment(sampleSize)
    21. }
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by VitaminG; 12th January 2017 at 15:00.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Live Plotting the data from 2D QVector of type QPointF

    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:

    Qt Code:
    1. 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() ) ) );
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by d_stranz; 12th January 2017 at 19:20.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 9
    Last Post: 27th March 2014, 10:45
  2. Replies: 2
    Last Post: 27th November 2013, 01:56
  3. QT 4.5 and Plotting data
    By Lezer in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 6th September 2012, 14:11
  4. Replies: 5
    Last Post: 3rd September 2011, 00:11
  5. Live Update data in table
    By abghosh in forum Qt Programming
    Replies: 8
    Last Post: 25th February 2010, 19:16

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.