week 7 - wednesday. what did we talk about last time? introduction to arrays lab 6
TRANSCRIPT
![Page 1: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/1.jpg)
CS 121Week 7 - Wednesday
![Page 2: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/2.jpg)
Last time
What did we talk about last time? Introduction to arrays Lab 6
![Page 3: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/3.jpg)
Questions?
![Page 4: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/4.jpg)
Project 3
![Page 5: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/5.jpg)
Mid-Semester Evaluations
![Page 6: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/6.jpg)
Arrays
![Page 7: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/7.jpg)
Instantiation of an array
When you declare an array, you are only creating a variable that can hold an array
At first, it holds nothing, also know as null
To use it, you have to create an array, supplying a specific size:
This code creates an array of 100 ints
int[] list;list = new int[100];
![Page 8: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/8.jpg)
Accessing elements of an array You can access an element of an array by
indexing into it, using square brackets and a number
Once you have indexed into an array, that variable behaves exactly like any other variable of that type
You can read values from it and store values into it Indexing starts at 0 and stops at 1 less than
the length
list[9] = 142;System.out.println(list[9]);
![Page 9: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/9.jpg)
Length of an array
When you instantiate an array, you specify the length
Sometimes (like in the case of args) you are given an array of unknown length
You can use its length member to find out
int[] list = new int[42];int size = list.length;System.out.println("List has " + size + " elements"); //prints 42
![Page 10: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/10.jpg)
Array Examples
![Page 11: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/11.jpg)
Statistics
Recently, we showed you how to add a set of numbers together as they were input by a user
Although this is a useful technique, not every operation is possible
We can find the sum or the average of the numbers because we only need to see the numbers once
What operation needs to see the numbers more than once?
![Page 12: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/12.jpg)
Variance
Variance is a measurement of how spread out numbers are from their mean
To calculate it, you have to calculate the mean first
The formula is:
Where N is the number of elements, xi is the ith element, and is the mean
N
ii xx
N 1
22 )(1
x
![Page 13: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/13.jpg)
Code for variance
Given an array of doubles called numbers, here’s the code for finding their variancedouble average = 0;
double variance = 0;double temp;
for( int i = 0; i < numbers.length; i++ )average += numbers[i];
average /= numbers.length;
for( int i = 0; i < numbers.length; i++ ){
temp = numbers[i] – average;variance += temp*temp;
}
variance /= numbers.length;
![Page 14: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/14.jpg)
Cards
We can represent a deck of cards as an array of 52 items
One easy way is to make each item a String giving the name of the card
We can extend last time's lab and store each of these names in an array
![Page 15: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/15.jpg)
Array swap
Swapping the values of two variables is a fundamental operation in programming
It is going to become more important in arrays because now the order of variables has become important
The simplest way to swap two variables involves using a third variable as a temporary location
![Page 16: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/16.jpg)
Swap code
Here is an example of swapping two Strings indexed i and j in an array of Strings called array
int i = in.nextInt();int j = in.nextInt();
int temp;temp = array[i];array[i] = array[j];array[j] = temp;
![Page 17: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/17.jpg)
Shuffling Cards
Using the swap code, we can do a random shuffling of a deck
To do so, we go through each element of the array, and randomly swap it with any of the later elementsfor( int i = 0; i < n; i++ )
{exchange = i + (int)(Math.random() * (n - i));temp = deck[i];deck[i] = deck[exchange];deck[exchange] = temp;
}
![Page 18: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/18.jpg)
Searching
Searching through an array is an important operation
The simplest way to do so is just linear search: check every element in the array
Searching and sorting are really key to all kinds of problems
We’ll cover both topics in depth in a few weeks
![Page 19: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/19.jpg)
Sound
![Page 20: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/20.jpg)
Sound
Like light, sound is a wave For those physics buffs here,
sound is usually transmitted as a compression wave
In contrast, light is a transverse wave
It doesn’t really matter, we can pretend that sound is a transverse wave
![Page 21: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/21.jpg)
Frequency
The human ear can hear between about 12 Hz and 20,000 Hz
The higher the frequency of the wave, the higher the frequency of the note
Note (ha, ha) that the A an octaveabove A440 has twice the frequency
Each half-step is an increase in the frequency by a factor of about 1.06
Note Frequency
A 440
B 493.88
C 523.25
D 587.33
E 659.26
F 698.46
G 783.99
A 880
![Page 22: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/22.jpg)
Example of frequency change We can take a sound:
And reproduce that sound at double the frequency:
Notice that we have to add twice as much information to have the sound fill the same amount of time
![Page 23: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/23.jpg)
Amplitude
The amplitude of a wave is the distance from the trough of a wave to its peak
In sound, amplitude is a measure of volume
The larger the amplitude, the louder the sound
Amplitude
![Page 24: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/24.jpg)
Example of amplitude change
We can take a sound:
And make the sound with half the amplitude:
The frequency is exactly the same, but the sound is half is loud
![Page 25: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/25.jpg)
Real sounds
Something that looks like a sine wave is called a pure tone
No real instruments play anything like that
Even the purest real sound has overtones and harmonics
Real sound is the result of many messy waves added together:
![Page 26: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/26.jpg)
Digital Sampling
![Page 27: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/27.jpg)
Sampling
On a computer, we cannot record a wave form directly
As usual, we have to figure out a way to store a wave as a series of numbers
We are going to use these numbers to approximate the heights of the wave at various points
![Page 28: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/28.jpg)
Sample rate
As we all know by now, Hertz (Hz) is a unit that means a number of times per second
Equivalent to Hz is s-1
We are going to break down the wave into lots of slices
We are going to have 44,100 slices in a second
Thus, we are slicing at 44,100 Hz
![Page 29: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/29.jpg)
Sample values
We slice up a wave and record the height of the wave
Each height value is called a sample
By getting 44,100 samples per second, we get a pretty accurate picture of the wave
![Page 30: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/30.jpg)
Sample format
There are many different formats for sampling audio
In our system, each sample will be recorded as a double
The minimum value of a sample will be -1.0 and the maximum value of a sample is 1.0
A series of samples with value 0.0 represents silence
Our samples will be stored in an array
![Page 31: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/31.jpg)
StdAudio Class
![Page 32: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/32.jpg)
Purpose of the StdAudio class Audio data on Windows machines is
sometimes stored in a WAV file A WAV file is much simpler than an MP3
because it has no compression Even so, it contains two channels (for stereo)
and can have many different sample rates and formats for recording sound
The StdAudio class lets you read and write a WAV file easily and always deal with a single array of sound, sampled at 44,100 Hz
![Page 33: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/33.jpg)
StdAudio methods
Everything you’d want to do with sound:
To do interesting things, you have to manipulate the array of samples
Make sure you added StdAudio.java to your project before trying to use it
Method Use
double[] read(String file) Read a WAV file into an array of doubles
void save(String file, double[] input)
Save an array of doubles (samples) into a WAV file
void play(String file) Play a WAV file
void play(double[] input) Play an array of doubles (samples)
![Page 34: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/34.jpg)
StdAudio example
Let’s load a file into an array:
If the song has these samples:
Perhaps samples will contain:
String file = "song.wav";double[] samples = StdAudio.read(file);
-.9 -.7 -.6 -.4 -.2 -.1 .1 .2 .3 .4 .5 .6 .6 .5 .4 .3 .2 0 -.2 -.4
![Page 35: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/35.jpg)
StdAudio example
With the audio samples loaded into the array named samples, we can play them as follows:
StdAudio.play(samples);
![Page 36: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/36.jpg)
Generating sound with StdAudio
Or, we could generate sound from scratch with StdAudio
This example from the book creates 1 second of the pitch A440:
double[] sound = new double[StdAudio.SAMPLE_RATE + 1];for( int i = 0; i < sound.length; i++ )
sound[i] = Math.sin(2 * Math.PI * i * 440 / StdAudio.SAMPLE_RATE);
StdAudio.play(sound);
![Page 37: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/37.jpg)
Breaking a sound into parts
What if we wanted to play the second half of a sound followed by the first half? I know, why would we want to do that?double[] samples = StdAudio.read(file);
double[] switched = new double[samples.length];
for(int i = 0; i < samples.length/2; i++ )switched[i + samples.length/2] = samples[i];
for(int i = samples.length/2; i < samples.length; i++ )switched[i - samples.length/2] = samples[i];
StdAudio.play(switched);
![Page 38: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/38.jpg)
Quiz
![Page 39: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/39.jpg)
Upcoming
![Page 40: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/40.jpg)
Next time…
Finish StdAudio Lab 7
![Page 41: Week 7 - Wednesday. What did we talk about last time? Introduction to arrays Lab 6](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649e165503460f94b01d29/html5/thumbnails/41.jpg)
Reminders
Keep reading Chapter 6 of the textbook
Start working on Project 3