Beruflich Dokumente
Kultur Dokumente
LECTURE TOPICS
Mid Semester Recess Period More Picture Techniques Sound and Arrays More Sound and Arrays Program Design and Strings Lists, Files and Modules Web, Representations, Steganography Turtles and Other Classes Revision and Look Ahead No formal classes - MUST be available normal & supplementary period
Assignment due 3:00 Tuesday May 21 Practical Test 2 in Lab class Practical Test 1 in Lab class
Lecture Topics and Lab topics are the same for each week
Revision Indentation
Indentation how far across the page each statement starts is absolutely integral to Python programming Statements that have bodies (eg def, if, for) end with colons The body of a statement must be indented further than the statement itself
Revision Indentation
2 or 3 spaces is good; its not too much to type - with only one space it can be hard to see the indentation. After the body, indentation must go back to the same as the statement it was the body of In all other cases, a statement must be indented the same amount as the statement before it
Revision Indentation
Revision Indentation
Revision Indentation
Revision Indentation
Indentation is not just pretty, not just arbitrary It is what tells Python (and readers) the structure of the program. For the sake of readers, it helps if the comments are indented the same as their surrounding statements
Revision Indentation
Warning be very careful with the return statement when you are using loops if it accidentally gets to be part of the loop it will stop the loop on the first time through!
10
Revision Arrays
An array is a collection of items all have the same name but each has a different index, an identifying number
11
Revision Arrays
An array is a collection of items all have the same name but each has a different index, an identifying number eg. An array called height
height[2] height[0] if i has the value 4 this is height[i]
height index
4
12
Revision Arrays
An array is a collection of items all have the same name but each has a different index, an identifying number eg. An array called pixels
pixels = getPixels(picture) pixels[3]
pixels index
0 1 2 3 4
. . .
13
Revision - Functions
Programs are made up of one of more functions Functions can be called from the command area, but, more importantly, from other functions defined in the program If we want a function to take arguments, we include corresponding parameters when defining it Whenever we find the same piece of code appearing several times in a program, we should extract it, define it as a function, and replace the multiple occurrences with multiple calls to the function If there are variations between the occurrences, we use parameters and arguments to deal with the variations
14
15
16
17
18
19
JES Options
Some useful things you can do on the Edit/Options menu
1.Change Mode to Expert gives more help on errors, which might or might not be more helpful 2.Auto save on load means you dont have to agree to save every time you load the program
20
JES Options
Some useful things you can do on the Edit/Options menu
3. Consider turning off logging 4. Consider turning off saving backup copies 5. Look at the modulo option for pixel colour values; we discussed this in an earlier lecture
21
22
23
24
But a digitised sound is broken into individual samples, each representing the sound frequency at one small instant in time
When the samples are small enough and close enough together, it sounds the same to us
25
...
samples
26
Features of Sound
Amplitude
Amplitude, the height of the wave, relates to loudness Frequency, number of cycles per second, relates to pitch
27
Features of Sound
28
Features of Sound
Overtones are additional frequencies that turn pure sound into rich sound Real waves have different shapes (sine, square, triangle, indeterminate) Very few real sounds are pure in pitch or wave shape
29
Features of Sound
Sounds also have a quality called Timbre This is a quality of a sound that make them sound different (even though they have the same amplitude and frequency (e.g. flute compared to a violin, different voices)
30
Features of Sound
Loudness and pitch both relate logarithmically to amplitude and frequency:
Doubling the amplitude increases loudness by same amount Doubling the frequency increases pitch by same amount
31
Features of Sound
Loudness and pitch both relate logarithmically to amplitude and frequency:
Doubling the amplitude increases loudness by same amount Doubling the frequency increases pitch by same amount
Actually its more complex than this - we respond differently to low and high frequencies at low amplitudes (loudness button) - and responses to different frequencies change with age.
32
33
Binary numbers
Aside computers use binary, off/on representing 0/1
off 0 on 1
34
0 0 0 0 0 0
0 4 0 3 5 8
Decimal
base 10
10 4
10 0
10
10 5
10 8
0
35
11011 = 1 x 24 + 1 x 23 + 0 x 22 + 0 x 21 + 1 x 20
0 0 0 0 0 0
0 1 1 0 0 1
Binary
base 2
(25 in base 10)
2 1
2 0
2 1
2 1
36
37
38
39
40
41
index 0
...
43
index 0
...
45
...
46
47
48
Once weve made a sound from a file, its easy to adjust the amplitude (and thus the volume)
49
Note that if we adjust too far, we get clipping the biggest (positive and negative) values are chopped off And we cant reverse it even if we reduce the amplitude the sound remains clipped.
50
Normalising sound
Normalising sound means increasing the amplitude just as far as we can without clipping
51
Normalising sound
Normalising sound means increasing the amplitude just as far as we can without clipping
1. We need to find the biggest amplitude (positive or negative) 2. Increase that up to the maximum possible (32767) 3. Multiply every other sample by the same amount
52
Normalising sound
Read the book and the code (lecture6sample.py) very carefully to understand how it works Why doesnt it look normalised? Because each sample is the average of many samples How would you find where it does reach the top or bottom?
53
54
20000
40000
60000
80000
100000
120000
140000
160000
180000
55
56
Then we write another function that calls that function three times
def boostThreeBits(aSound, mult, startA, endA, startB, endB, startC, endC): # A highly specific function to boost the amplitude by multiplier just in # three specific ranges adjustAmp(aSound, mult, startA, endA) adjustAmp(aSound, mult, startB, endB) adjustAmp(aSound, mult, startC, endC)
57
boostThreeBits(sound,32,32000,46000, 62000,76000,130000,148000)
58
Assignment
Assignment has been posted. Please start now! I will go through it now. We will stop to look more closely at a few of the concepts described in this assignment 1. Thresholding grey-scale images to create binary images. 2. Using masks to process images (e.g. Sobel)
59
60
p1 p4 p7
p2 p5 p8
p3 p6 p9
62
Masks usually 3 x 3 are placed over each pixel in the image one at a time.
The pixel under the centre of the mask is the one being processed.
63
64
65
66
67
68
69
etc, etc,
70
71
Calculate a new grey value at p5 by using the weights and the original pixel grey values at (p1,p2,p3,p4,p5,p6,p7,p8,p9)
72
greyVertical = (p1 x -1) + (p2 x 0) + (p3 x 1) + (p4 x -2) + (p5 * 0) + (p6 x 2) + (p7 x -1) + (p8 x 0) + (p9 x 1) (you need to take the absolute value to make sure it stays positive)
73
greyVertical = (p1 x -1) + (p2 x 0) + (p3 x 1) + (p4 x -2) + (p5 * 0) + (p6 x 2) + (p7 x -1) + (p8 x 0) + (p9 x 1) (Dont change the original p5)
74
greyHorizontal = (p1 x 1) + (p2 x 2) + (p3 x 1) + (p4 x 0) + (p5 * 0) + (p6 x 0) + (p7 x -1) + (p8 x -2) + (p9 x -1) (you need to take the absolute value to make sure it stays positive)
75
greyHorizontal = (p1 x 1) + (p2 x 2) + (p3 x 1) + (p4 x 0) + (p5 * 0) + (p6 x 0) + (p7 x -1) + (p8 x -2) + (p9 x -1) (you need to take the absolute value to make sure it stays positive)
76
Sobel Mask
Combines both the vertical and horizontal calculation
greyHorizontal = (p1 x 1) + (p2 x 2) + (p3 x 1) + (p4 x 0) + (p5 * 0) + (p6 x 0) + (p7 x -1) + (p8 x -2) + (p9 x -1)
greyVertical = (p1 x -1) + (p2 x 0) + (p3 x 1) + (p4 x -2) + (p5 * 0) + (p6 x 2) + (p7 x -1) + (p8 x 0) + (p9 x 1)
sobelGrey =
greyHorizontal + greyVertical
Make sure gVertical is positive by taking absolute value of the result
77
Practical Test
You can pick up the mark sheets at the end of the lecture. I will go through the mark sheet now. If we have time I will also do the exam in front of your eyes.
78