Transcript ppt

CS 177 Week 7 Recitation Slides
Modifying Sounds using Loops
+
Discussion of some Exam Questions
1
ANY QUESTIONS?
2
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)
3
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 times
B) 21 times
C) 30 times
D) 31 times
B)
4
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)
5
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)
6
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)
7
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
D) 8
picture
ANY QUESTIONS?
9
ANY QUESTIONS?
10
The Physics of Sound

AMPLITUDE of a wave:
Distance from the zero point to the greatest
(or least) pressure.


11
Amplitude is the most important factor in
perception of volume.
The loudness of the volume is proportional to the
amplitude of the underlying wave.
The Physics of Sound
(continued…)

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.



12
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
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?



13
Here I2 = 2 * I1
Therefore, change in intensity = 10 * log10 (2 * I1 /I1)
= 10 log10 (2) = 3dB.
The Physics of Sound
(continued…)

FREQUENCY of a wave:
Number of cycles per second in
the wave.



14
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.
Digitizing Sound

Analog-to-Digitizing-Conversion (ADC).





15
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.
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):


16
We know that human voices don’t typically get over 4000Hz.
Therefore, telephonic systems are designed to capture around
8000 cycles per second.
More details on Digitizing
sound




17
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.
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
Two’s Complement Numbers
(continued…)

Method to convert a number to 2’s complement (using
n bits)




19
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.
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
20
Two’s Complement Numbers
(continued…)



21
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
Two’s Complement Numbers
(continued…)

RANGE of values expressible using an n bit 2’s
complement representation



22
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
Sounds as Arrays




23
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.
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.
Working with sounds
(continued…)




25
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.
Example
26
Example to deal with large
number of samples

27
Loops can be used to iterate over all the samples as
follows:
Final QUESTIONS???
28