Results 1 to 10 of 10

Thread: Qwt spline fitter - it doesn't work as expected

  1. #1
    Join Date
    Nov 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Qwt spline fitter - it doesn't work as expected

    Hello,

    I need to use spline interpolation for data display in my program. There is QwtSplineCurveFitter class - documentation says, thar it uses spline to fit curve, but the results are not acceptable (or I have missed something?):

    Code to fit curve between 5 points:

    Qt Code:
    1. QwtPlotCurve *curve1 = new QwtPlotCurve("Curve 1");
    2.  
    3. QPolygonF polygon;
    4.  
    5. polygon << QPointF(-1, -3);
    6. polygon << QPointF(1, 5);
    7. polygon << QPointF(2, 16);
    8. polygon << QPointF(3, 8);
    9. polygon << QPointF(4, 13);
    10.  
    11. fitter.setFitMode(fitter.ParametricSpline);
    12. curve1->setData(fitter.fitCurve(polygon));
    13. curve1->attach(ui->plot);
    14.  
    15. ui->plot->replot();
    To copy to clipboard, switch view to plain text mode 

    And this is the result:



    Without using fitter class:



    OS: Debian Sid
    Qwt: 5.2.0-1 (libqwt5-qt4)

    What's wrong there? Interpolation seems to be not working at all or spline resolution is very low but I can't see any method to increase it?

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qwt spline fitter - it doesn't work as expected

    Guess you want to interpolate the points after they have been translated to widget coordinates. QwtPlotCurve does this for you, when:

    Qt Code:
    1. curve->setStyle(QwtPlotCurve::Lines);
    2. curve->setCurveAttribute(QwtPlotCurve::Fitted, true);
    To copy to clipboard, switch view to plain text mode 

    If you want to pass an individual fitter:

    Qt Code:
    1. fitter->setFitMode(fitter.ParametricSpline);
    2. fitter->setSplineSize(...);
    3. curve->setCurveFitter(fitter);
    To copy to clipboard, switch view to plain text mode 

    Uwe

  3. #3
    Join Date
    Nov 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qwt spline fitter - it doesn't work as expected

    It works, but there is no interpolation when using custom fitter...

  4. #4
    Join Date
    Nov 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qwt spline fitter - it doesn't work as expected

    Ok, I'm going to write interpolation method from scratch, because built-in fitter is not enough for me (it's quite poor i think), but anyway, thanks for response

  5. #5
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qwt spline fitter - it doesn't work as expected

    Quote Originally Posted by giaur View Post
    Ok, I'm going to write interpolation method from scratch, because built-in fitter is not enough for me (it's quite poor i think), but anyway, thanks for response
    As far as I can see you didn't succeed to use the QwtCurveFitter API ( guess you missed to enable the QwtPlotCurve::Fitted attribute, or didn't adjust the spline size.) . If frustration about this is the reason for your "poor", well ...

    If your statement is because you have checked the implementation, please elaborate, why spline interpolation ( this is a very common way of interpolation ) isn't a solution for you situation.

    QwtCurveFitter is an abstract API and you can implement any other fitting algorithm as well ( f.e. in 5.3 you find an implementation of the Douglas/Peucker algorithm ). So if you know other/better strategies for curve fitting I'm interested to add them to the Qwt lib.

    Uwe

  6. #6
    Join Date
    Nov 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qwt spline fitter - it doesn't work as expected

    What about Cardinal/Cubic Spline? The cubic spline is used in OpenOffice and it works quite good. I tested cardinal spline also, and results was much better than default qwt fitter

    Quote Originally Posted by Uwe
    QwtCurveFitter is an abstract API and you can implement any other fitting algorithm as well
    It sounds good - can you provide simple example how to do this?
    Last edited by giaur; 1st December 2009 at 09:50.

  7. #7
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qwt spline fitter - it doesn't work as expected

    What about Cardinal/Cubic Spline? The cubic spline is used in OpenOffice and it works quite good.
    QwtSplineCurveFitter is an implementation of a cubic spline - are you sure, that you successfully enabled spline interpolation for your curve ?

    You didn't explain why you want to have curve interpolation. If your motivation is to paint smoother curves it's obvious, that interpolation needs to be done for the translated points ( in widget coordinates ) - not for the points in application coordinates.

    It sounds good - can you provide simple example how to do this?
    If you want to interpolate the translated points (see above) you must derive from QwtCurveFitter and implement YourFitter::fitCurve(). Please send me the code of your cardinal spline class or something else if you think it is valuable for others.

    But initializing your curve with your individual fitter requires the same steps you have to do for QwtSplineCurveFitter. So I recommend to check if the bad results of the interpolation are really because of the implemented interpolation algorithm. If true I would be interested in your curve points for further investigations.

    Maybe it helps to check the curvdemo2 example, that shows how to enable interpolation ( don't forget the QwtPlotCurve::Fitted attribute).

    Uwe
    Last edited by Uwe; 2nd December 2009 at 08:42. Reason: spelling error

  8. #8
    Join Date
    Nov 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qwt spline fitter - it doesn't work as expected

    Quote Originally Posted by Uwe
    So I recommend to check if the bad results of the interpolation are really because of the implemented interpolation algorithm.
    I'm sure they are.

    I've made simple test. My data points are:
    0, 17
    1, 16.5
    2, 8
    4, 3
    5, 12
    8, 14.5

    And look at the plot screenshot:


    Legend:
    - blue: without spline
    - green: parametric spline (built in)
    - red: cardinal spline, tangentStrength=0.35

    Built-in cubic spline (entirely unacceptable for me):


    And, finally - cubic spline from open office for the same points:



    I'm not really sure what alghoritm exactly is used in Open Office, but this is much better, than your cubic spline implementation, and it seems to be better, than cardinal spline I used.
    Last edited by giaur; 2nd December 2009 at 17:27.

  9. #9
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qwt spline fitter - it doesn't work as expected

    I tried your points with the following code:

    Qt Code:
    1. #include <qapplication.h>
    2. #include <qwt_plot.h>
    3. #include <qwt_plot_curve.h>
    4. #include <qwt_curve_fitter.h>
    5. #include <qwt_symbol.h>
    6.  
    7. int main(int argc, char **argv)
    8. {
    9. const QColor color(35, 70, 106);
    10.  
    11. QApplication a(argc, argv);
    12.  
    13. QwtPlot plot;
    14. plot.setCanvasBackground(Qt::white);
    15. plot.resize(600,400);
    16. plot.show();
    17.  
    18. const double x[] = { 0, 1, 2, 4, 5, 8 };
    19. const double y[] = { 17, 16.5, 8, 3, 12, 14.5 };
    20.  
    21. // Insert new curves
    22. QwtPlotCurve *curve = new QwtPlotCurve();
    23. curve->setStyle(QwtPlotCurve::Lines);
    24. curve->setCurveAttribute(QwtPlotCurve::Fitted, true);
    25.  
    26. QwtSymbol symbol;
    27. symbol.setStyle(QwtSymbol::Rect);
    28. symbol.setSize(10, 10);
    29. symbol.setBrush(color);
    30.  
    31. curve->setSymbol(symbol);
    32.  
    33. fitter->setFitMode(QwtSplineCurveFitter::ParametricSpline);
    34. fitter->setSplineSize(100);
    35. curve->setCurveFitter(fitter);
    36.  
    37. curve->setRenderHint(QwtPlotItem::RenderAntialiased);
    38. curve->setPen(QPen(color, 3));
    39.  
    40. curve->setData(x, y, 6);
    41. curve->attach(&plot);
    42.  
    43. return a.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 

    The result is different compared to the OpenOffice curve, but what exactly makes you so unhappy with it ?

    Uwe

    PS: You can play with the spline size parameter. IMHO the curve looks better with a value of 50.
    Attached Images Attached Images

  10. #10
    Join Date
    Nov 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qwt spline fitter - it doesn't work as expected

    Ok, parametric spline is quite good - it looks the same as on my screenshot. But there is still some effect which I neew to minimalize - I need only edges round, and minimal curve peeks. For cardinal spline, there is parametr called tangentStrength and I can play with it to get bes results.

    I simply need the situation, when maximum/minimum data point is curve maximum/minimum (ideally) and cardinal spline in my screen looks better here
    Last edited by giaur; 2nd December 2009 at 19:12.

Similar Threads

  1. Can qwt work on Windows 7 x64?
    By RavenS in forum Qwt
    Replies: 0
    Last Post: 28th March 2009, 09:37
  2. QHTTP::GET doesn't work as expected !
    By tuthmosis in forum Qt Programming
    Replies: 1
    Last Post: 27th July 2008, 15:45

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.