java - Flat line instead wave -
i want plot sine wave, instead application generates flat line. when use series random jchartfree
example works fine. use debugger check if values good. values different zero
public void createdataset() { xyseriescollection dataset = new xyseriescollection(); xyseries series1 = new xyseries("object 1"); double = 1; double t = 1; double fs = 10; double f = 200; int rozmiar = (int) (t*fs); double[] x = new double[rozmiar]; (int = 0; < rozmiar; i++) { x[i] = * math.sin(2 * math.pi * f * / fs); series1.add(i, x[i]); } dataset.addseries(series1); data = dataset; } //... public void createchartpanel() { //pwykres = new jpanel(); //if(java.util.arrays.aslist(getcomponents()).contains(pwykres)){ //getcontentpane().remove(pwykres); //} if(pwykres != null){ pwykres.removeall(); pwykres.revalidate(); } string charttitle = "objects movement chart"; string xaxislabel = "x"; string yaxislabel = "y"; jfreechart chart = chartfactory.createxylinechart(charttitle, xaxislabel, yaxislabel, dataset); customizechart(chart); pwykres = new chartpanel(chart); getcontentpane().add(pwykres, borderlayout.center); setsize(620, 460); //validate(); pwykres.repaint(); } //endregion //... //region private void customizechart(jfreechart chart) { xyplot plot = chart.getxyplot(); xysplinerenderer renderer; renderer = new xysplinerenderer(); renderer.setseriesshapesvisible(0, false); // sets paint color each series renderer.setseriespaint(0, color.red); // sets thickness series (using strokes) renderer.setseriesstroke(0, new basicstroke(1.0f)); // sets paint color plot outlines //plot.setoutlinepaint(color.blue); //plot.setoutlinestroke(new basicstroke(2.0f)); // sets renderer lines plot.setrenderer(renderer); // sets plot background plot.setbackgroundpaint(color.white); // sets paint color grid lines plot.setrangegridlinesvisible(true); plot.setrangegridlinepaint(color.black); plot.setdomaingridlinesvisible(true); plot.setdomaingridlinepaint(color.black); }
you taking sine of values whole multiples of 2 * pi
, of these values (approximately) zero, hence graph end appearing flat unless scaled show these tiny values (which floating point errors).
x[i] = * math.sin(2 * math.pi * f * / fs);
where a
= 1 , f/fs
= 20 , i
integer
for example:
math.sin(0) // 0.0 math.sin(2 * math.pi) // -2.4492935982947064e-16 (approximately zero) math.sin(4 * math.pi) // -4.898587196589413e-16 (approximately zero)
to see characteristic shape of sine wave, need vary input sin
function smaller increments, e.g. pi/10 or less.
Comments
Post a Comment