28
1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

Embed Size (px)

DESCRIPTION

3 Q3. Consider the following function definition: def sum(a, b): c = a + b return c print “Hello World” What is the output when you execute sum(4,5) from the command window in JES? A) 9 B) Hello World C) 9 Hello World D) There is no output A)

Citation preview

Page 1: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

1

CS 177 Week 7 Recitation Slides

Modifying Sounds using Loops+

Discussion of some Exam Questions

Page 2: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

2

ANY QUESTIONS?

Page 3: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

3

Q3. Consider the following function definition: def sum(a, b): c = a + b return c print “Hello World” What is the output when you execute sum(4,5) from the

command window in JES? A) 9B) Hello WorldC) 9 Hello WorldD) There is no output

A)

Page 4: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

4

Q5. How many times does the following code print “CS177” ?

for i in range(0,5): for j in range(4): print “CS177”print “CS177” 

A) 20 timesB) 21 timesC) 30 timesD) 31 times

B)

Page 5: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

5

Q9. Consider that the numbers from 1 to 20 are arranged in a two dimensional matrix with 4 rows and 5 columns, filling one row after the other. What are the co-ordinates for the number 14 within the matrix?

 A) (3, 2)B) (3, 4)C) (2, 3)D) (4, 3)

C)

Page 6: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

6

Q11. Assume that we have a picture with width of 300 and height of 50. We want to write a program that scales the picture up by a factor of 4 and then rotates the picture to the left by 90 degrees. Which of the following statements should we use to make the empty picture that will be used by our program?

A) newPicture = makeEmptyPicture(200, 1200)B) newPicture = makeEmptyPicture(1200, 200)C) newPicture = makeEmptyPicture(300, 200)D) newPicture = makeEmptyPicture(300, 50)

B)

Page 7: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

7

Q13. Assume that we have a function max(a, b, c, d) that returns the maximum of the given parameters. Which of the following is NOT a legal statement?

A) value = max(7, 5, 11, 9) + 10B) print max(1, 3, 2, 4)C) max(11, 17, 13, 15)D) result = max(4, 2, 3)

D)

Page 8: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

8

Q25. Consider the following function: def myFunc(pict): allPixels = getPixels(pict) for p in allPixels: value = getRed(p) setRed(p, value*0.5)What does myFunc function do?A) it increases the RED component of all the pictures’ pixelsB) it increases the RED component of the last pixel of the

pictureC) it decreases the RED component of all the pictures’ pixelsD) it decreases the RED component of the last pixel of the

picture

D)

Page 9: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

9

ANY QUESTIONS?

Page 10: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

10

ANY QUESTIONS?

Page 11: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

The Physics of Sound

11

AMPLITUDE of a wave:Distance from the zero point to the greatest (or least) pressure. Amplitude is the most important factor in

perception of volume. The loudness of the volume is proportional to the

amplitude of the underlying wave.

Page 12: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

The Physics of Sound (continued…)

12

Our perception of volume is logarithmically related to the changes in amplitude/intensity. So what does this mean???

This means that doubling the amplitude or the intensity of the sound wave doesn’t make us hear it twice as loud. Change in intensity of sound (unit is decibels dB) = 10 * log10 (I2/I1) = 20 * log10 (A2/A1)

where I1 and I2 are the initial and final intensities and A1 and A2 are the initial and final amplitudes

Page 13: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

13

A numerical example

What is the change in the intensity of sound in decibels when we double the amplitude? Here A2 = 2 * A1

Therefore, change in intensity = 20 * log10 (2 * A1 /A1) = 20 log10 (2) = 6dB.

What is the change in the intensity of sound in decibels when we double the intensity? Here I2 = 2 * I1

Therefore, change in intensity = 10 * log10 (2 * I1 /I1) = 10 log10 (2) = 3dB.

Page 14: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

14

FREQUENCY of a wave:Number of cycles per second in the wave. It is measured in Hertz (Hz) or

cycles per second (cps). As the frequency increases, we

perceive the pitch to increase. Like intensity, our perception of

pitch is proportional to the log of the frequency.

The Physics of Sound (continued…)

Page 15: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

Digitizing Sound

15

Analog-to-Digitizing-Conversion (ADC). Sound is a continuously changing pressure wave. Question : How do we store it on the computer? Solution : We need to sample the points on the wave and

capture the “sound” at that moment. Calculus does this by computing the area of infinite number of

rectangles. We can’t have infinite sample points due to memory

constraints.

Page 16: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

16

Nyquist Theorem

The theorem states that “To capture a sound of at most n cycles per second,

you need to capture 2n samples per second.

Consequence of the theorem (using an example): We know that human voices don’t typically get over 4000Hz. Therefore, telephonic systems are designed to capture around

8000 cycles per second.

Page 17: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

More details on Digitizing sound

Each sample point of the sound wave are encoded in 2 bytes (16bits).

A sample point in a sound wave can have both positive and negative amplitude.

Therefore, we reserve the most significant bit of the 16 bits to indicate whether the number is positive (0) or negative(1).

The other 15 bits can be used to indicate the magnitude of the amplitude.

17

Page 18: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

Two’s Complement Numbers

18

Decimal number It’s 2’s complement form+3 011

+2 010

+1 001

0 000

-1 111

-2 110

-3 101

-4 100

Page 19: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

19

Two’s Complement Numbers (continued…)

Method to convert a number to 2’s complement (using n bits) Ignore the sign of the number for the time being. Compute the binary encoding for that number. If the number is positive, then stop. Else, flip the bits and add a 1 to it. Then stop.

Page 20: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

20

Two’s Complement Numbers (continued…)

Example 1 Let the number be +3 and

the number of bits be 3. Ignore the sign. Binary of 3 is 011. Since the initial number is

positive, the representation is

011

Example 2 Let the number be -4 and the

number of bits be 3. Ignore the sign. Binary of 4 is 100. Since the initial number is

negative, flip the bits. This gives us 011

Add 1 to it. This gives us 100 Therefore the representation is

100

Page 21: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

Two’s Complement Numbers (continued…)

Here is a check to ensure that you have computed the 2’s complement correctly.

For any number, say +x and –x, the sum of their 2’s complements should give zero.

For example, +9 is 00001001 and -9 is 11110111 Adding +9 and -9 we get

21

Page 22: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

22

Two’s Complement Numbers (continued…)

RANGE of values expressible using an n bit 2’s complement representation n bits => we can have 2n values possible (including both

negative and positive values). This range is from -2(n-1) to +2(n-1) – 1 For 16 bit numbers, this range is between -32768 and 32767

Page 23: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

Sounds as Arrays

An Array is a sequence of bytes right next to one another in memory.

Each value in an array is called an element. A sound wave can be thought of as an ordered list of

sample points. It is pretty intuitive to use arrays to store sound waves,

where each element of the array stores one sample point.

23

Page 24: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

Working with sounds

24

pickAFile() This function is used to select a file as input. makeSound(filename) This function is used to create a sound object associated

with the file. getSamples(sound) This function takes a sound object as input and outputs

an array of all the samples as sample objects. getSampleValue(sampleobject) This function takes a sample object as input and outputs

the value of the sample object.

Page 25: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

25

Working with sounds (continued…)

setSampleValue(sampleobject, value) This takes a sampleobject and a value as input and sets

the object with the specified value. getSampleValueAt(soundobject, indexno) This function is used to get the sample value at the

indexno position of the soundobject array getLength(soundobject) This function takes a sound object as input and output

the number of samples in the soundobject. getSamplingRate(soundobject) This function takes a sound object as input and outputs

the sampling rate.

Page 26: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

Example

26

Page 27: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

Example to deal with large number of samples

27

Loops can be used to iterate over all the samples as follows:

Page 28: 1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions

28

Final QUESTIONS???