Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions that Deal with Arrays CS 1111 Ryan Layer Feb 23, 2010

Similar presentations


Presentation on theme: "Functions that Deal with Arrays CS 1111 Ryan Layer Feb 23, 2010"— Presentation transcript:

1 Functions that Deal with Arrays CS 1111 Ryan Layer Feb 23, 2010

2 Plotting a Function Graph

3 Sampling y=sin(4x) + sin(20x)
public class PlotFunction { // Plot y = sin(4x) + sin(20x) public static void main(String[] args) { int N = Integer.parseInt(args[0]); double[] a = new double[N+1]; for (int i = 0; i <= N; i++) a[i] = Math.sin(4*Math.PI*i/N) + Math.sin(20*Math.PI*i/N); StdDraw.setXscale(0, N); StdDraw.setYscale(-2.0,2.0); for (int i = 1; i <= N; i++) StdDraw.line(i-1, a[i-1], i , a[i]); //StdDraw.save("sine.png"); } 5 20 200

4 Random Numbers import java.awt.Color; 500
int N = Integer.parseInt(args[0]); double scale = 1.0/N; double[] rands = new double[N]; StdDraw.setPenRadius(0.005); for (int i = 0; i < N; i++) { rands[i] = Math.random(); StdDraw.point(i*scale, rands[i]); } StdDraw.setPenRadius(0.01); StdDraw.setPenColor(new Color(255,0,0)); double sum = 0; sum += rands[i]; StdDraw.point(i*scale, sum/i); //StdDraw.save("rand.png"); 500

5 Sound C:523.25 B:493.88 A:440.0 G:391.59 sin(2пt × f) F:348.50

6 Sampling Sound Hz:440 int sps = Integer.parseInt(args[0]);
double hz = Double.parseDouble(args[1]); double duration = 1.0/40.0; int N = (int) (sps * duration); double[] a = new double[N+1]; for (int i = 0; i <= N; i++) a[i] = Math.sin(2*Math.PI*i*hz/sps); StdDraw.setXscale(0, N); StdDraw.setYscale(1.1,-1.1); for (int i = 1; i <= N; i++) StdDraw.point(i , a[i]); StdDraw.setPenRadius(0.002); StdDraw.line(i-1, a[i-1], i , a[i]); 5512 11025 44100

7 Sampling Sound Hz:440 int sps = Integer.parseInt(args[0]);
double hz = Double.parseDouble(args[1]); double duration = 1.0/40.0; int N = (int) (sps * duration); double[] a = new double[N+1]; for (int i = 0; i <= N; i++) a[i] = Math.sin(2*Math.PI*i*hz/sps); StdAudio.play(a); 5512 11025 44100

8 Notes = Songs A A B C A A B C A int sps = Integer.parseInt(args[0]);
double[] notes = {440.0, , , 440.0}; double duration = 0.5; int N = (int) (sps * duration); double[] a = new double[2*notes.length*(N+1)]; for (int i = 0; i < 2; i++) { for (int j = 0; j < notes.length; j++) { for (int k = 0; k < N; k++) { int c = i * notes.length*N + j*N + k; a[c] = Math.sin(2*Math.PI* c*notes[j]/sps); } StdAudio.play(a); A N samples per note A B C A A B C A 4 notes

9 Array Input/Output //double[] notes = {440.0, 493.88, 523.25, 440.0};
int sps = Integer.parseInt(args[0]); double[] notes = {440.0, , , 440.0}; double duration = 0.5; int N = (int) (sps * duration); double[] a = new double[2*notes.length*(N+1)]; for (int i = 0; i < 2; i++) { for (int j = 0; j < notes.length; j++) { for (int k = 0; k < N; k++) { int c = i * notes.length*N + j*N + k; a[c] = Math.sin(2*Math.PI* c*notes[j]/sps); } StdAudio.play(a); //double[] notes = {440.0, , , 440.0}; double[] notes = StdArrayIO.readDouble1D();

10 Arrays as Arguments public static void plotPoints(double[] a) {
int N = a.length; StdDraw.setXscale(0, N-1); StdDraw.setPenRadius(1.0 / (3.0 * N)); for (int i = 0; i < N; i++) { StdDraw.point(i, a[i]); } public static void plotLines(double[] a) { StdDraw.setPenRadius(); for (int i = 1; i < N; i++) { StdDraw.line(i-1, a[i-1], i, a[i]); public static void plotBars(double[] a) { StdDraw.setPenRadius(0.5 / N); StdDraw.line(i, 0, i, a[i]);


Download ppt "Functions that Deal with Arrays CS 1111 Ryan Layer Feb 23, 2010"

Similar presentations


Ads by Google