Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions."— Presentation transcript:

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

2 2 ANY QUESTIONS?

3 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)

4 4 Q5. How many times does the following code print “CS177” ? for i in range(0,5): for j in range(4): print “CS177” A) 20 times B) 21 times C) 30 times D) 31 times B)

5 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)

6 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)

7 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) + 10 B) print max(1, 3, 2, 4) C) max(11, 17, 13, 15) D) result = max(4, 2, 3) D)

8 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’ pixels B) it increases the RED component of the last pixel of the picture C) it decreases the RED component of all the pictures’ pixels D) it decreases the RED component of the last pixel of the picture D)

9 9 ANY QUESTIONS?

10 10 ANY QUESTIONS?

11 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.

12 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 * log 10 (I 2 /I 1 ) = 20 * log 10 (A 2 /A 1 )  where I 1 and I 2 are the initial and final intensities and A 1 and A 2 are the initial and final amplitudes

13 13 A numerical example What is the change in the intensity of sound in decibels when we double the amplitude?  Here A 2 = 2 * A 1  Therefore, change in intensity = 20 * log 10 (2 * A 1 /A 1 )  = 20 log 10 (2) = 6dB. What is the change in the intensity of sound in decibels when we double the intensity?  Here I 2 = 2 * I 1  Therefore, change in intensity = 10 * log 10 (2 * I 1 /I 1 )  = 10 log 10 (2) = 3dB.

14 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…)

15 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.

16 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.

17 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

18 Two’s Complement Numbers 18 Decimal numberIt’s 2’s complement form +3011 +2010 +1001 0000 111 -2110 -3101 -4100

19 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.

20 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

21 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

22 22 Two’s Complement Numbers (continued…) RANGE of values expressible using an n bit 2’s complement representation  n bits => we can have 2 n 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

23 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

24 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.

25 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.

26 Example 26

27 Example to deal with large number of samples 27 Loops can be used to iterate over all the samples as follows:

28 28 Final QUESTIONS???


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

Similar presentations


Ads by Google