2d chart tutorial can benchmark large time serie (500k points)

This commit is contained in:
Martin Pernollet 2014-11-20 14:23:30 +01:00
parent 75f5ff3b54
commit 97c28bcaf8
1 changed files with 56 additions and 25 deletions

View File

@ -25,37 +25,67 @@ import org.jzy3d.plot3d.primitives.axes.layout.renderers.PitchTickRenderer;
import org.jzy3d.ui.LookAndFeel; import org.jzy3d.ui.LookAndFeel;
/** /**
* Showing a pair of 2d charts to represent pitch and amplitude variation of an audio signal * Showing a pair of 2d charts to represent pitch and amplitude variation of an
* audio signal.
*
* When using large number of samples, run program with VM argument : -Xmx1024m
* *
* @author Martin Pernollet * @author Martin Pernollet
*/ */
public class Chart2dDemo { public class Chart2dDemo {
public static float duration = 15f; public static float duration = 60f;
/** milisecond distance between two generated samples*/
public static int interval = 50;
public static int maxfreq = 880; public static int maxfreq = 880;
public static int nOctave = 5; public static int nOctave = 5;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
PitchAmpliControlCharts log = new PitchAmpliControlCharts(duration, maxfreq, nOctave); PitchAmpliControlCharts log = new PitchAmpliControlCharts(duration, maxfreq, nOctave);
new TimeChartWindow(log.getCharts()); new TimeChartWindow(log.getCharts());
start(); generateSamplesInTime(log);
// generateSamples(log, 500000);
while(elapsed()<duration){ }
public static void generateSamples(PitchAmpliControlCharts log, int n) throws InterruptedException {
System.out.println("will generate " + n + " samples");
for (int i = 0; i < n; i++) {
// Random audio info // Random audio info
double pitch = Math.random() * maxfreq; double pitch = Math.random() * maxfreq;
double ampli = Math.random(); double ampli = Math.random();
// Add to time series
log.seriePitch.add(time(n, i), pitch);
log.serieAmpli.add(time(n, i), ampli);
}
}
public static double time(int n, int i) {
return ((double) i / n) * duration;
}
public static void generateSamplesInTime(PitchAmpliControlCharts log) throws InterruptedException {
System.out.println("will generate approx. " + duration * 1000 / interval + " samples");
start();
while (elapsed() < duration) {
// Random audio info
double pitch = Math.random() * maxfreq;
double ampli = Math.random();
// Add to time series // Add to time series
log.seriePitch.add(elapsed(), pitch); log.seriePitch.add(elapsed(), pitch);
log.serieAmpli.add(elapsed(), ampli); log.serieAmpli.add(elapsed(), ampli);
// Wait a bit // Wait a bit
Thread.sleep(50); Thread.sleep(interval);
} }
} }
/** Hold 2 charts, 2 time series, and 2 drawable lines*/ /** Hold 2 charts, 2 time series, and 2 drawable lines */
public static class PitchAmpliControlCharts { public static class PitchAmpliControlCharts {
public Chart2d pitchChart; public Chart2d pitchChart;
public Chart2d ampliChart; public Chart2d ampliChart;
public Serie2d seriePitch; public Serie2d seriePitch;
@ -66,17 +96,17 @@ public class Chart2dDemo {
public PitchAmpliControlCharts(float timeMax, int freqMax, int nOctave) { public PitchAmpliControlCharts(float timeMax, int freqMax, int nOctave) {
pitchChart = new Chart2d(); pitchChart = new Chart2d();
pitchChart.asTimeChart(timeMax, 0, freqMax, "Time", "Frequency"); pitchChart.asTimeChart(timeMax, 0, freqMax, "Time", "Frequency");
IAxeLayout axe = pitchChart.getAxeLayout(); IAxeLayout axe = pitchChart.getAxeLayout();
axe.setYTickProvider(new PitchTickProvider(nOctave)); axe.setYTickProvider(new PitchTickProvider(nOctave));
axe.setYTickRenderer(new PitchTickRenderer()); axe.setYTickRenderer(new PitchTickRenderer());
seriePitch = pitchChart.getSerie("frequency", Serie2d.Type.LINE); seriePitch = pitchChart.getSerie("frequency", Serie2d.Type.LINE);
seriePitch.setColor(Color.BLUE); seriePitch.setColor(Color.BLUE);
pitchLineStrip = (ConcurrentLineStrip) seriePitch.getDrawable(); pitchLineStrip = (ConcurrentLineStrip) seriePitch.getDrawable();
ampliChart = new Chart2d(); ampliChart = new Chart2d();
ampliChart.asTimeChart(timeMax, 0, 1f, "Time", "Amplitude"); ampliChart.asTimeChart(timeMax, 0, 1.1f, "Time", "Amplitude");
serieAmpli = ampliChart.getSerie("amplitude", Serie2d.Type.LINE); serieAmpli = ampliChart.getSerie("amplitude", Serie2d.Type.LINE);
serieAmpli.setColor(Color.RED); serieAmpli.setColor(Color.RED);
amplitudeLineStrip = (ConcurrentLineStrip) serieAmpli.getDrawable(); amplitudeLineStrip = (ConcurrentLineStrip) serieAmpli.getDrawable();
@ -89,18 +119,19 @@ public class Chart2dDemo {
return charts; return charts;
} }
} }
/** A frame to show a list of charts*/ /** A frame to show a list of charts */
public static class TimeChartWindow extends JFrame { public static class TimeChartWindow extends JFrame {
private static final long serialVersionUID = 7519209038396190502L; private static final long serialVersionUID = 7519209038396190502L;
public TimeChartWindow(List<Chart> charts) throws IOException { public TimeChartWindow(List<Chart> charts) throws IOException {
LookAndFeel.apply(); LookAndFeel.apply();
String lines = "[300px]"; String lines = "[300px]";
String columns = "[500px,grow]"; String columns = "[500px,grow]";
setLayout(new MigLayout("", columns, lines)); setLayout(new MigLayout("", columns, lines));
int k=0; int k = 0;
for(Chart c: charts){ for (Chart c : charts) {
addChart(c, k++); addChart(c, k++);
} }
windowExitListener(); windowExitListener();
this.pack(); this.pack();
@ -108,14 +139,14 @@ public class Chart2dDemo {
setVisible(true); setVisible(true);
} }
public void addChart(Chart chart, int id){ public void addChart(Chart chart, int id) {
JPanel chartPanel = new JPanel(new BorderLayout()); JPanel chartPanel = new JPanel(new BorderLayout());
Border b = BorderFactory.createLineBorder(java.awt.Color.black); Border b = BorderFactory.createLineBorder(java.awt.Color.black);
chartPanel.setBorder(b); chartPanel.setBorder(b);
chartPanel.add((java.awt.Component) chart.getCanvas(), BorderLayout.CENTER); chartPanel.add((java.awt.Component) chart.getCanvas(), BorderLayout.CENTER);
add(chartPanel, "cell 0 "+id+", grow"); add(chartPanel, "cell 0 " + id + ", grow");
} }
public void windowExitListener() { public void windowExitListener() {
addWindowListener(new WindowAdapter() { addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
@ -125,7 +156,7 @@ public class Chart2dDemo {
}); });
} }
} }
/** Simple timer */ /** Simple timer */
protected static long start; protected static long start;