简体   繁体   中英

Spectogram using JavaPlot not working

I'm trying to display a spectogram using JavaPlot with this piece of code:

    PlotStyle style = new PlotStyle();
    style.setStyle(Style.IMAGE);


    DataSetPlot data = new DataSetPlot(points);
    data.setPlotStyle(style);


    JavaPlot plot = new JavaPlot();
    plot.addPlot(data);
    plot.plot();

Where the points are valid as they work when using gnuplot in the command line like this:

 gnuplot --persist -e 'plot "data.dat" w image'

The problem is that the above piece of code doesn't show any image at all, it is all white. However the previous command works fine, and data.dat contains the points in the exactly same format. No error appears when running the code.

Here is a example of some points data:

0.0 193.0 0.18183826861232413
0.0 194.0 0.2467637036800797
0.0 195.0 0.43531750893288235
0.0 196.0 0.3893599780473012
0.0 197.0 0.3220816458659573
0.0 198.0 0.25389713892289173
0.0 199.0 0.22935136709597423
1.0 0.0 0.23021155472288352
1.0 1.0 0.33383157107150707
1.0 2.0 0.3745792715533692
1.0 3.0 2.028348052193793
1.0 4.0 2.4150235476868978
1.0 5.0 2.4169194327766736
1.0 6.0 1.8633442057577019
1.0 7.0 4.2682342944471054
1.0 8.0 3.22544345282322

And this are the commands sended to gnuplot by JavaPlot:

set multiplot layout 1,2 rowsfirst downwards
_gnuplot_error = 1
plot '-' title 'Datafile 1' with image ; _gnuplot_error = 0
0.0 9.0 6.612583996900796 
1.0 9.0 4.719585678813712 
2.0 9.0 0.5475948495661151 
3.0 9.0 0.7385211622757041 
4.0 9.0 0.711512824841686 
5.0 9.0 3.7572382303712604 
6.0 9.0 1.0818137070547578 
7.0 9.0 0.057188125070687344 
8.0 9.0 0.8218555010675036 
9.0 9.0 5.754170136586405 
e
if (_gnuplot_error == 1) print '_ERROR_'
unset multiplot
quit

For this example I took 10x10 points, so the passed coordinates should be something like:

0.0 0.0 6.612583996900796 
0.0 1.0 4.719585678813712 
0.0 2.0 0.5475948495661151 
0.0 3.0 0.7385211622757041 
0.0 4.0 0.711512824841686 
0.0 5.0 3.7572382303712604 
0.0 6.0 1.0818137070547578 
0.0 7.0 0.057188125070687344 
0.0 8.0 0.8218555010675036 
0.0 9.0 5.754170136586405 
1.0 0.0 6.612583996900796 
1.0 1.0 4.719585678813712 
1.0 2.0 0.5475948495661151 
1.0 3.0 0.7385211622757041 
1.0 4.0 0.711512824841686 
1.0 5.0 3.7572382303712604 
1.0 6.0 1.0818137070547578 
1.0 7.0 0.057188125070687344 
1.0 8.0 0.8218555010675036 
1.0 9.0 5.754170136586405 
...
9.0 9.0 xxxxxxxxxxxxx

It seems that JavaPlot is not iterating the Y coordinates.

Does someone know what am I doing wrong?

How big of a data set are you using?

Something I noticed when working with JavaPlot is that it really doesn't like large data sets. Its like there's a bug when JavaPlot talks to gnuplot and each time a data point is graphed, there's a small chance it'll flip out. Its not an issue with small amounts of data but makes graphing larger ones (> 1000) almost impossible. It looks a line is improperly entered and you end up with all lines after that one getting something like this:

multiplot> 53.86510713480712 67.8601980449745 53.557782725560635 
       ^
       invalid command

Try using a smaller set of data and see if it works. If this is the case, the only way I know to deal with it is to break apart your data set or to try using another wrapper library like jgnuplot--> http://jgnuplot.sourceforge.net/

Also, I notice you never say you're using a new graph. Try adding changing it to

JavaPlot plot = new JavaPlot();
plot.newGraph();  //added this line
plot.plot();

As I couldn't fix that error I finally decided to call gnuplot and pass the points correctly by myself. Here is the piece of code I've used to draw the spectogram:

Process p = Runtime.getRuntime().exec("gnuplot --persist");

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
String s = "plot '-' title 'Spectogram' w image\n";
writer.write(s);
writer.flush();

for(int m = 0; m < x.length; m++){
   for(int k = 0; k < x[0].length; k++){
       s = m + " " + k + " " + x[m][k] + "\n";
       writer.write(s);  
       writer.flush();
   }
}

s = "e\n";
writer.write(s);
writer.flush();

And it works! :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM