PDA

View Full Version : Real Time Plot Getting Slower and Slower



Tong14
30th October 2015, 11:14
Hi everyone,

Im a newbie to QT and QWT and required some help. I want to draw a real time plot for sine and cosine wave but my plot get slower and slower when time passes. Any kind soul can help me out? Thank You very much.

MainWindow Code


if(plottingStatus == "play")
{
if(angle1 == 360)
{
timer->stop();
timer->start();
}
else if(angle1 <= 720)
{
if (timer_count%3 == 0)
{
angle1++;
}
timer_count++;

//calling the calculation function
if(angle1 == 0 && rotationCount > 0)
{
emit toCalculate(0, timer_count, graphType, passType);
}
else
{
emit toCalculate(harmonics, timer_count, graphType, passType);
}

}
else
{
plottingStatus = "stopped1";
rotationCount ++;
timer->stop();
angle1 = 0;
timer_count = 0;
}
}


Sine Wave Code


theta1 = ((2*n)+1) * 2 * M_PI * timer_count*0.000925925;
amplitude = (1.00/((2*n)+1)*1.00);

real = prev_real + (amplitude *(qCos(theta1)));
img = prev_img + (amplitude*(qSin(theta1)));

Y = (timer_count*0.000925925);
Y = Y;

yData.push_back(img);
xData.push_back(Y);

emit graphdata(yData, xData);


Graphing code


cSin = new QwtPlotCurve( "y = sin(x)" );
cSin->setRenderHint( QwtPlotItem::RenderAntialiased, true ); // smoothen the line of graph

cSin->setPen( QColor(Qt::green) );
_yData = yData;
_xData = xData;

cSin->setSamples(_xData, _yData);
replot();
cSin->attach( this );

ars
31st October 2015, 16:04
Hello,

in your sine wave code you use xData/yData.push_back() to add new values. Have you preallocated (vector::reserve()) a sufficiently big amount of memory to these vectors? If not, the vector will reallocate a bigger chunk of memory when there is not enough memory availabe for storing the new data. When I remember right, std::vector() will duplicate the reserved memory every time a reallocation is necessary. Also note that reallocation requires copying of the already available data from the previous memory location to the new location before freeing the old memory.

You use emit graphdata(yData, xData) to notify availability of new data. If graphdata() uses value semantics for its parameters, depending on the type of xData and yData a copy of the vectors is created, which again takes time.

I do not know which class is the client of signal graphdata. Assuming that you have connected some qwt related classes to this signal check if that code walks through the whole data set for plotting the data. Also, do you really need to replot for each new point added to the trace?

Best regards
ars