Re: [ MathGL ] Plot image data

523 views
Skip to first unread message

mathgl....@gmail.com

unread,
Sep 30, 2012, 12:51:53 AM9/30/12
to mat...@googlegroups.com
Dear Fredrik,

2012/9/28 Fredrik Lingvall <fredrik....@gmail.com>:
> Hi All,
>
> I'm new to MathGL and I'm trying to use it to plot image data (eg., matrix
> as a 2D image).

All this functions
(http://mathgl.sourceforge.net/doc_en/doc_en_141.html#g_t2D-plotting)
work with 2D data (matrices). In particular Dens() function draw and
"image" like plot
(http://mathgl.sourceforge.net/doc_en/doc_en_62.html#Dens-sample).

> What is the best function to do this? As an example, I
> want to plot a spectrogram image where the x-axis is time, y-axis is
> frequency (0 to Nyquist), and the amplitudes is in dB where I want a
> colorbar to show (in dB) what the colors in the image means.

I'm not familiar with name of Matlab functions, but I think you are
looking for something like this
http://mathgl.sourceforge.net/doc_en/doc_en_143.html#stfa
or for sample picture
http://mathgl.sourceforge.net/doc_en/doc_en_95.html#STFA-sample

For colorbar see http://mathgl.sourceforge.net/doc_en/doc_en_138.html#colorbar

--
All the best,
Alexey Balakin

Fredrik Lingvall

unread,
Oct 1, 2012, 7:57:12 AM10/1/12
to mat...@googlegroups.com
Hi Alexey,

Thanks for the info. I tried the Dens function but I can't get the scaling right - everything outside 0 and 1.0 is disregarded. What I need is to set the colomap to an arbitrary range. For example, Octave's

imagesc(x,y,A,[-120 -20])

will "autoscle" all colors between -120 and -20 in this example.

If I do something like:

    mglData mgl_x;
    mglData mgl_y;   
    mglData mgl_d;   

    mgl_x.Link(xd,M,1);
    mgl_y.Link(yd,N,1);
    mgl_d.Link(dd,M,N);

    mglGraph gr;

    gr.SetFontSize(3.0);

    gr.Alpha(true);

    gr.Title(title);
    gr.Label('x',xlabel,0);
    gr.Label('y',ylabel,0);

    gr.SetRange('x',xd[0], xd[M-1]);
    gr.SetRange('y',yd[0], yd[N-1]);
    gr.SetRange('z',0.0, 100.0);

    gr.Axis();
    gr.Box();
    gr.Grid();

    gr.Dens(mgl_x,mgl_y,mgl_d);

    gr.Colorbar(">");
 
   gr.WriteJPEG(filename);

There is nothing outside the 0 to 1 range so I can't make the color scale map to dB for example.

The

gr.SetRange('z',0.0, 100.0);

above doesn't seem to do anything.

Is there a way to control the range of the color map using MathGL?

/Fredrik

mathgl....@gmail.com

unread,
Oct 1, 2012, 8:06:08 AM10/1/12
to mat...@googlegroups.com
Dear Fredrik

2012/10/1 Fredrik Lingvall <fredrik....@gmail.com>:
Yes, it do mostly nothing for Dens() function.

> Is there a way to control the range of the color map using MathGL?

You need to set color range, i.e. use
gr.SetRange('c',0.0, 100.0);
instead of
gr.SetRange('z',0.0, 100.0);

Fredrik Lingvall

unread,
Oct 3, 2012, 4:10:31 AM10/3/12
to mat...@googlegroups.com
Hi Alexey,


The

gr.SetRange('z',0.0, 100.0);

worked perfectly. I have, however an out-of-memory problem. Does the code


    mglData mgl_x;
    mglData mgl_y;   
    mglData mgl_d;   

    mgl_x.Link(xd,M ,1);
    mgl_y.Link(fd,N,1);
    mgl_d.Link(spec_image,M,N);


    mglGraph gr;

    gr.SetFontSize(3.0);

    gr.Alpha(true);

    gr.Title(title);
    gr.Label('x',xlabel,0);
    gr.Label('y',ylabel,0);

    gr.SetRange('x',xd[0], xd[M-1]);
    gr.SetRange('y',fd[0],  fd[N-1]);
    gr.SetRange('c',scale_min, scale_max);

    gr.Axis();
    gr.Box();
    gr.Grid();

    gr.Dens(mgl_x,mgl_y,mgl_d);

allocate/copy any memory? In my case M and N is rather large and I hoped that the Link functions above would use the data in-place and not copy the data. If there is a memory copy then is there some way to avoid this?

/Fredrik


On Monday, October 1, 2012 2:06:08 PM UTC+2, Alexey Balakin wrote:

>
> The
>
> gr.SetRange('z',0.0, 100.0);
>
> above doesn't seem to do anything.

Yes, it do mostly nothing for Dens() function.

> Is there a way to control the range of the color map using MathGL?

You need to set color range, i.e. use
gr.SetRange('c',0.0, 100.0);
instead of
gr.SetRange('z',0.0, 100.0);




Fredrik Lingvall

unread,
Oct 4, 2012, 2:54:37 AM10/4/12
to mat...@googlegroups.com

Alexey,

This was a copy-and-paste error:


On Wednesday, October 3, 2012 10:10:31 AM UTC+2, Fredrik Lingvall wrote:
Hi Alexey,

The

gr.SetRange('z',0.0, 100.0);

worked perfectly.

It should have been:

   gr.SetRange('c',scale_min, scale_max);

worked perfectly.

In the code in my previous post it's the Dens call that consumes most of the memory/time. that is

   gr.Dens(mgl_x,mgl_y,mgl_d);

Some background info: I'm computing spectrograms using (50% overlapping windows and) 4096 point FFTs which takes about 1-2 s for each input vector using FFTW resulting in an 2000x2048 matrix (after some downsampling) . The  Dens call takes about 18 s for the 2000x2048 input image.  We need to do at least 20 000 such computations so I need a very fast way to plot.

/Fredrik

mathgl....@gmail.com

unread,
Oct 4, 2012, 3:35:48 AM10/4/12
to mat...@googlegroups.com
Dear Fredrik,

> allocate/copy any memory? In my case M and N is rather large and I hoped
> that the Link functions above would use the data in-place and not copy the
> data. If there is a memory copy then is there some way to avoid this?

Yes, MathGL allocate the memory for primitives (about 120 bytes per
point) by default. You can avoid it (i.e. use direct drawing to
bitmap) by setting gr->SetQuality(6); before any drawing.

kb...@mst.edu

unread,
Apr 27, 2014, 4:52:31 AM4/27/14
to mat...@googlegroups.com
Hi,

I am trying to do something similar. I want to call in the mathgl from c++ code in linux. As I can follow the example here, My question is what does each of the array x, y and  d contains specifically? I have a 1 d array of the spectrogram values computed which I need to plot. My time of each signal is 200 ms. Could you help me with a code snippet that will allow me to plot the 1d data into a spectrogram plot with colormap visualizations? Thanks!

kb...@mst.edu

unread,
Apr 29, 2014, 12:09:00 AM4/29/14
to mat...@googlegroups.com
Hi Fedrick,

I am trying to do the same thing I have a 2d array created of rows of frequency values vs coloums of time data. I want to plot using mathgl from C++ running on ubuntu. I am using Mathgl 2.2 version but im getting linking errors. Can you please share you entire code so that It can be a good reference for me? I am also generating lots of jpg images for each audio spectogram data. I would like to see how you used the last option about efficient memory allocation that you used. Kindly help!


On Friday, September 28, 2012 7:50:31 AM UTC-4, Fredrik Lingvall wrote:
Hi All,

I'm new to MathGL and I'm trying to use it to plot image data (eg., matrix as a 2D image).  What is the best function to do this?   As an example, I want to plot a spectrogram image where the x-axis is time, y-axis is frequency (0 to Nyquist), and the amplitudes is in dB where I want a colorbar to show (in dB) what the colors in the image means.

Similar code in Octave/Matlab would be:

fft_len = 4096;
Fs = 96e3;

s = specgram(data,fft_len,Fs) / (fft_len/2);
s = 20*log10(abs(s));

t = (0:length(s)-1)/length(s) * t_max;
f = (0:fft_len/2-1)/(fft_len/2)*(Fs/2);

imagesc(t, f, s,[-120 -20]);

axis([0 t_max 0 Fs/2]);
grid on;
 
xlabel('t [s]');
ylabel('f [Hz]');

h = colorbar ('EastOutside');
set(get(h).ylabel,'string','Amplitude [dB]');

Regards,

/Fredrik

mathgl....@gmail.com

unread,
Apr 29, 2014, 6:24:17 AM4/29/14
to mat...@googlegroups.com


--
You received this message because you are subscribed to the Google Groups "MathGL" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathgl+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Kind regards,
Prof. Alexey Balakin

mathgl....@gmail.com

unread,
Apr 29, 2014, 6:31:46 AM4/29/14
to mat...@googlegroups.com
Hi,
You need first create mglData from yours spectrogram (see http://mathgl.sourceforge.net/doc_en/Array-creation.html#Array-creation ). After it set proper axis and color range, call Axis() if you need one. Finally, just call Dens() function to plot it. If you need to draw huge data arrays then you probably need to call SetQuality(6) before any drawing. If you need fastest but less accurate drawing then you need to call SetQuality(0) or SetQuality(1) (correspondingly SetQuality(4) or SetQuality(5) for low memory usage) -- see http://mathgl.sourceforge.net/doc_en/Export-picture.html#quality .


--
You received this message because you are subscribed to the Google Groups "MathGL" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathgl+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kb...@mst.edu

unread,
Apr 29, 2014, 12:29:02 PM4/29/14
to mat...@googlegroups.com
Hi Alexey,

Thanks for your reply. Could you provide me with a specific code sample? I have a few questions:
1. the array that I have is a float array consisting decibal values from 0-96 DB scaled to 0-1 DB, the array is like (M,N) where M is the no of windows used and N is the total no of samples in each window

i am using C++ to call in Mathgl, so what headers needs to be included? I want to link that spectrogram(M,N) matrix as mglData which throws me error can you show me the correct way to do it? I do it as 
spect[m][n]= certain values input

then
mglGraph gr;
  mglData arr;
mglData x;//not using at the moment but would like to show x and y dimension
mglData y;//not using at the moment but would like to show x and y dimension
arr.Link(m_gl);
y.Link(ydat,num

gr->SetQuality(6);

   gr.Alpha(true);
gr.Box();
gr.Dens(m_gl);
gr.Colorbar();
gr.WriteJPEG("/home/koyel/test.jpg");
delete gr;


errors:
error: no matching function for call to ‘mglData::Link(float [(((long unsigned int)(((long int)num_rows) + -0x00000000000000001)) + 1)][(((long unsigned int)(((long int)windowSize) + -0x00000000000000001)) + 1)], int&, int&)’
specto.cpp:181:34: note: candidates are:
/usr/local/include/mgl2/data.h:77:14: note: void mglData::Link(mreal*, long int, long int, long int)
/usr/local/include/mgl2/data.h:77:14: note:   no known conversion for argument 1 from ‘float [(((long unsigned int)(((long int)num_rows) + -0x00000000000000001)) + 1)][(((long unsigned int)(((long int)windowSize) + -0x00000000000000001)) + 1)]’ to ‘mreal* {aka double*}’
/usr/local/include/mgl2/data.h:79:14: note: void mglData::Link(mglData&)
/usr/local/include/mgl2/data.h:79:14: note:   candidate expects 1 argument, 3 provided


second error with dens function:

error: no matching function for call to ‘mglGraph::Dens(float [(((long unsigned int)(((long int)num_rows) + -0x00000000000000001)) + 1)][(((long unsigned int)(((long int)windowSize) + -0x00000000000000001)) + 1)])’
specto.cpp:188:13: note: candidates are:
/usr/local/include/mgl2/mgl.h:793:14: note: void mglGraph::Dens(const mglDataA&, const mglDataA&, const mglDataA&, const char*, const char*)
/usr/local/include/mgl2/mgl.h:793:14: note:   candidate expects 5 arguments, 1 provided
/usr/local/include/mgl2/mgl.h:795:14: note: void mglGraph::Dens(const mglDataA&, const char*, const char*)
/usr/local/include/mgl2/mgl.h:795:14: note:   no known conversion for argument 1 from ‘float [(((long unsigned int)(((long int)num_rows) + -0x00000000000000001)) + 1)][(((long unsigned int)(((long int)windowSize) + -0x00000000000000001)) + 1)]’ to ‘const mglDataA&’
specto.cpp:191:8: error: type ‘class mglGraph’ argument given to ‘delete’, expected pointer

Can you please help me? It would be great to get MathGL to work on my project!

kb...@mst.edu

unread,
Apr 29, 2014, 12:37:48 PM4/29/14
to mat...@googlegroups.com
a little update on the code:

mglGraph gr;
  mglData arr;
mglData x;//not using at the moment but would like to show x and y dimension
mglData y;//not using at the moment but would like to show x and y dimension
arr.Link(m_gl);//this is what i mean by the spect[m][n] 2d array i name it as m_gl instead of spect



gr->SetQuality(6);

   gr.Alpha(true);
gr.Box();
gr.Dens(m_gl);
gr.Colorbar();
gr.WriteJPEG("/home/koyel/test.jpg");
delete gr;

also yes i can use axis also i need help for correctly setting the axis values. Like the example here I have also generated an x matrix and y matrix holding time and frequency to be plotted along X and y axis. please help me.

kb...@mst.edu

unread,
Apr 29, 2014, 4:36:14 PM4/29/14
to mat...@googlegroups.com
Hi,

another update:  I changed all data types to double and passed on the pointer to those arrays as is said by the previous error but now I get a different error type: I'll copy it here, also it would be great help if you can provide me with the correct syntax code for what i'm trying to do

error:
 g++ specto.cpp -o specto -lmgl -lfftw3 -lm
/tmp/ccqZsUir.o: In function `mglData::Link(double*, long, long, long)':
specto.cpp:(.text._ZN7mglData4LinkEPdlll[mglData::Link(double*, long, long, long)]+0x37): undefined reference to `mgl_data_link'
/tmp/ccqZsUir.o: In function `mglGraph::mglGraph(int, int, int)':
specto.cpp:(.text._ZN8mglGraphC2Eiii[_ZN8mglGraphC5Eiii]+0x57): undefined reference to `mgl_create_graph'
/tmp/ccqZsUir.o: In function `mglGraph::~mglGraph()':
specto.cpp:(.text._ZN8mglGraphD2Ev[_ZN8mglGraphD5Ev]+0x28): undefined reference to `mgl_use_graph'
/tmp/ccqZsUir.o: In function `mglGraph::SetRange(char, double, double)':
specto.cpp:(.text._ZN8mglGraph8SetRangeEcdd[mglGraph::SetRange(char, double, double)]+0x37): undefined reference to `mgl_set_range_val'
/tmp/ccqZsUir.o: In function `mglGraph::SetQuality(int)':
specto.cpp:(.text._ZN8mglGraph10SetQualityEi[mglGraph::SetQuality(int)]+0x20): undefined reference to `mgl_set_quality'


what does it mean to have undefined reference to those specific functions!

kb...@mst.edu

unread,
Apr 29, 2014, 10:16:29 PM4/29/14
to mat...@googlegroups.com
my ubuntu sytem had math1.9 installed but i reinstalled mathgl 2.2 version for linux and i checked the lib is installed in /usr/local/lib/mathgl....so file. and i call it while compiling using the -lmgl option. what am i missing? can you tell me how to uninstall mathgl from my system so that maybe i can reinstall and try again?

kb...@mst.edu

unread,
May 1, 2014, 5:51:50 PM5/1/14
to mat...@googlegroups.com
Hi,

I could get the program to work but i get the plot image as a bmp but it only shows the colorbar the actual plot is empty...what could be the reason? is there any specific way to pass in data for the dens function?

mathgl....@gmail.com

unread,
May 2, 2014, 4:27:03 PM5/2/14
to mat...@googlegroups.com
Dear kbnm6,

About e-mails from 29 April -- I couldn't add nothing more except to ask you read the error messages you put into this e-mails.

About e-mail from 30 April. It look as you have conflict with different versions of MathGL. Most probably you get headers from v.2.*, but try to link with v.1.* which come with older Ubuntu releases. Try to use ldconfig to proper setup which library you want to use, or specify its path exactly.



Christopher Luna

unread,
Nov 15, 2014, 11:43:48 AM11/15/14
to mat...@googlegroups.com
I am having the exact same issue, what is your suggested workaround?
Reply all
Reply to author
Forward
0 new messages