Spectrogram coming out black



  • I'm trying to plot a spectrogram. The issue in a nutshell is that my spectrogram image is coming out black. Its probably a sign that something is generating an error somewhere along the way. (I have not yet attempted to narrow it down). The text below describes the steps I currently take. Thanks for reading this and of course any suggestions as to what I may be doing wrong would be appreciated.

    The spectrogram data arrives via Ethernet, and its always the same size (256x4095) (float) and all I have to do is paint it using a 64 row colormap; say a JET colormap (like Matlab). To setup the image I want to paint I create a QPixmap sized to fit the rectangle within the control that will eventually be the spectrogram.

    @
    m_plot =new QPixmap(m_plotR);
    @

    when new data arrives, I scale the data to the size of the colormap, and refresh the display:

    @
    for(irow=0;irow<2564095;irow++)
    m_vSpectrogram[irow]=(unsigned char)((vSpectrogram[irow]-m_fdBMin)/(m_fdBMax-m_fdBMin)
    (COLORMAP_SIZE-1));
    refresh();
    @

    The refresh routine re-draws the axes, and the plot then updates the screen:

    @
    void Spectrogram::refresh()
    {
    drawAxes();
    drawPlot();
    update();
    }
    @

    Since the data size (256x4095) and the size of the QPixmap control (~512x512) are usually different (since the GUI is resizeable), to draw the data I create a QImage, set the colortable, scale the image to the actual plot size, and then convert the Pixmap from the image before Updating the plot:

    @
    void Spectrogram::drawPlot()
    {
    QImage AnImage(m_vSpectrogram,4095,256,QImage::Format_Indexed8);
    AnImage.setColorTable(m_vColormap);
    AnImage.scaled(m_plotR.width(),m_plotR.height());
    m_plot->convertFromImage(AnImage,Qt::AutoColor);
    }
    @

    [edit: added missing coding tags @ SGaist]


  • Lifetime Qt Champion

    Hi,

    one thing that you are not doing correctly: scaled returns a copy of the image so currently you are not converting the image you think you are.



  • SGaist,
    As you recommended, I have altered drawPlot() to use the scaled image as follows:

    @
    void Spectrogram::drawPlot()
    {
    QImage ScaledImage;

    QImage AnImage(m_vSpectrogram,4095,256,QImage::Format_Indexed8);
    AnImage.setColorTable(m_vColormap);
    ScaledImage=AnImage.scaled(m_plotR.width(),m_plotR.height());
    m_plot->convertFromImage(ScaledImage,Qt::AutoColor);

    }
    @
    but the image is still black. I have verified that this code is executed when the data arrives.

    The painEvent routine that is called by update() is:
    @
    void Spectrogram::paintEvent(QPaintEvent * /* event*/)
    {
    QStylePainter painter(this);
    int wtx,wty,hty;

    wtx= m_xAxisR.width(); /* X Axis width /
    wty= m_yAxisR.width(); /
    Y Axis width /
    hty= m_yAxisR.height(); /
    Y Axis height /
    ...
    painter.drawPixmap(wty, 0,wtx,hty,m_plot);/ This draws the spectrogram
    /
    }.
    @
    I draw the plot onto the control (positioning it within the control so that the XY axes show up to the left and bottom of the spectrogram itself. The axes are plotting OK. (I have omitted that code).

    Thanks so much for your help, but there's obviously still something amiss...

    Mike

    [edit: Added missing coding tags @ SGaist]


  • Lifetime Qt Champion

    m_plot doesn't need to be allocated on the the heap however it doesn't explain your current problem.
    Did you check that m_plot is valid ?



  • STOP !!!!
    ^^
    If you want plot graphics I suggest you download the beautyfull lib here ->
    http://www.qcustomplot.com/

    I use it to plot some spectrogram in Polar it's very usefull !

    !http://www.qcustomplot.com/images/examples/colormapdemo.png(Spectrogram)!



  • SGaist,
    Thanks again for your advice. I will remove the allocation of m_plot, and check its validity.



  • Franknos,
    Thankyou for the suggestion. Were it possible for me to select a 3rd party solution, I'd be tempted, but its not. The plot you sent is beautiful. I'll keep this solution in mind for other projects...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.