Results 21 to 40 of 48
Thread: sine wave in java2D graphics.

Re: sine wave in java2D graphics.
Look at these two graphs
y=x2 (quadratic)
http://www.bbc.co.uk/schools/gcsebit...s/graph_24.gif
y=x3 (cubic)
http://www.bbc.co.uk/schools/gcsebit...s/graph_17.gif
which one looks more like a sine wave?
 03042012, 06:00 PM #22
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,017
 Blog Entries
 7
 Rep Power
 23
Re: sine wave in java2D graphics.
I have the stamina of a seal; I lie on the beach instead of running on it.
 03042012, 06:28 PM #23
 03042012, 06:40 PM #24
 03042012, 06:48 PM #25

Re: sine wave in java2D graphics.
Yes there are FFT libraries out there that are best found via Google.
 03042012, 07:33 PM #27

Re: sine wave in java2D graphics.
 03042012, 08:02 PM #29
 03042012, 08:07 PM #30
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,017
 Blog Entries
 7
 Rep Power
 23
Re: sine wave in java2D graphics.
I have the stamina of a seal; I lie on the beach instead of running on it.


 03042012, 08:19 PM #33
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,017
 Blog Entries
 7
 Rep Power
 23
Re: sine wave in java2D graphics.
As in "bŭßƨƮǒƤ" 'squire? But then it doesn't even look like "muffin" ...
kindest regards,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 03042012, 08:19 PM #34
Re: sine wave in java2D graphics.
i've done scaling , and dont know ow to plot em next.. heres my coding
Java Code:import java.io.*; class fourier extends operations { fourier(String filename, int ntrm) { boolean debug = true; double Pi = 3.1415926535897932384626433832795028841971; double x[] = new double[100]; double y[] = new double[100]; double a[] = new double[100]; double b[] = new double[100]; double xmin, xmax, ymin, ymax, xs, xs1, xs2; double err, avgerr=0.0, rmserr=0.0, maxerr=0.0; double sum, sumsq=0.0; int npts, index, last; double tpi = 2.0*Pi; double dx, ti; FileReader file_in; BufferedReader in; String input_line, intStr; // read data try { file_in = new FileReader(filename); in = new BufferedReader(file_in); } catch(Exception e) { System.out.println("read unable to open file "+filename); return; } npts = 0; try { input_line = in.readLine(); // System.out.println("line="+input_line); while(input_line!=null) { index = 2; // x allow for two blanks last = input_line.indexOf(' ',index); // System.out.println("xindex="+0+", xlast="+last); intStr = input_line.substring(0,last); // System.out.println("xstring="+intStr); x[npts] = Double.parseDouble(intStr); index = last+1; // y last = input_line.length(); // System.out.println("yindex="+index+", ylast="+last); intStr = input_line.substring(index,last); // System.out.println("ystring="+intStr); y[npts] = Double.parseDouble(intStr); if(debug) System.out.println("npts="+npts+", x="+x[npts]+", y="+y[npts]); npts++; input_line = in.readLine(); // System.out.println("line="+input_line); } file_in.close(); } catch(Exception e) { System.out.println("read exception"+e); return; } if(ntrm==1) ntrm = npts; ntrm = Math.min(ntrm,(npts2)/2); System.out.println(npts+" points read, using "+ntrm+" terms"); xmin = x[0]; xmax = x[0]; ymin = y[0]; ymax = y[0]; for(int j=1; j<npts; j++) { xmin = Math.min(x[j],xmin); xmax = Math.max(x[j],xmax); ymin = Math.min(y[j],ymin); ymax = Math.max(y[j],ymax); } System.out.println("xmin="+xmin+", xmax="+xmax+ ", ymin="+ymin+", ymax="+ymax); // integration, simple trapezoidal rule System.out.println("coefficients"); for(int i=0; i<ntrm; i++) { a[i] = 0.0; b[i] = 0.0; ti = (double)i; for(int j=0; j<npts1; j++) { xs1 = (x[j]xmin)/(xmaxxmin); xs2 = (x[j+1]xmin)/(xmaxxmin); dx = xs2xs1; a[i] = a[i] + dx*(y[j]*Math.cos(ti*tpi*xs1)+y[j+1]* Math.cos(ti*tpi*xs2)); b[i] = b[i] + dx*(y[j]*Math.sin(ti*tpi*xs1)+y[j+1]* Math.sin(ti*tpi*xs2)); } System.out.println("a["+i+"]="+a[i]+", b["+i+"]="+b[i]); } System.out.println(" "); // check fit for(int j=0; j<npts; j++) { sum = 0.0; sumsq = 0.0; xs = (x[j]xmin)/(xmaxxmin); for(int i=0; i<ntrm; i++) { ti = (double)i; sum = sum + a[i]*Math.cos(ti*tpi*xs) + b[i]*Math.sin(ti*tpi*xs); } err = y[j]sum; sumsq = sumsq + err*err; avgerr = avgerr + Math.abs(err); maxerr = Math.max(Math.abs(err),maxerr); if(debug) System.out.println("y["+j+"]="+y[j]+", approx="+sum+ ", err"+err); } rmserr = Math.sqrt(sumsq/(double)npts); avgerr = avgerr/(double)npts; System.out.println("avgerr="+avgerr+", rmserr="+rmserr+", maxerr="+maxerr); System.out.println("fourier.java finished "); } // end constructor fourier public static void main (String[] args) { String filename; int ntrm = 1; int argc = args.length; System.out.println("fourier.java running "); if(argc>1) // file and ntrm { filename = new String(args[0]); ntrm = Integer.parseInt(args[1]); System.out.println("fourier series of "+filename+" for "+ntrm+" terms"); new fourier(filename, ntrm); } else if(argc>0) // just file, all terms { filename = new String(args[0]); System.out.println("fourier series of "+filename+" for all terms"); new fourier(filename, ntrm); } else { ntrm = 6; filename = new String("curve.dat"); System.out.println("fourier series of "+filename+" for "+ntrm); new fourier(filename, ntrm); } } // end main } // end class fourier class operations extends fourier { public static int a1[i],b1[i],as[i],bs[i]; void scaling(a[],b[]){ a1[i]=(a[i]0)/(2*math.PI0);//dmax is 2pi dmin is zero b1[i]=(b[i]0)/(10);//cmax is 1 cmin is zero as[i]=a1[i]*(SXmaxSXmin)+SXmin ; //sxmin,max not set yet,... will set it based on screen values.. bs[i]=b1[i]*(SYmaxSYmin)+SYmin; } }
Last edited by Fubarable; 03042012 at 08:24 PM. Reason: code tags added
 03042012, 09:49 PM #35
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,017
 Blog Entries
 7
 Rep Power
 23
Re: sine wave in java2D graphics.
If you have arrays 'sx' and 'sy' containing the scaled points and if you have a Graphics object 'g' (so you have something to draw on), all you have to do is 'connect the dots':
Java Code:for (int i= 1; i < sx.length; i++) g.drawLine(sx[i1], sy[i1], sx[i], sy[i]);
kind regards,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 03042012, 09:52 PM #36
 03042012, 10:00 PM #37
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,017
 Blog Entries
 7
 Rep Power
 23
 03042012, 10:01 PM #38
 03062012, 02:24 PM #39
 03062012, 02:27 PM #40
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,017
 Blog Entries
 7
 Rep Power
 23
Similar Threads

Do number series represent sine wave?
By rng in forum New To JavaReplies: 12Last Post: 01182012, 04:46 PM 
sine and cosine
By Dennis in forum Advanced JavaReplies: 9Last Post: 11132010, 06:45 AM 
Java2D
By Alex j in forum Java 2DReplies: 1Last Post: 04072009, 04:59 PM 
Drawing the sine curve
By bumblyb33 in forum Java 2DReplies: 7Last Post: 03262009, 11:29 PM 
Demonstration of drawing points. It draws a sine wave
By Java Tip in forum SWT TipsReplies: 0Last Post: 06282008, 09:25 PM
Bookmarks