http://inst.eecs.berkeley.edu/~ee123/sp16/
</small>
jtamir@utexas.edu
Follow the instructions on the class website to install python:
The Jupyter notebook is divided into cells. Each cell can contain texts, codes or html scripts. Running a non-code cell simply advances to the next cell. To run a code cell using Shift-Enter or pressing the play button in the toolbar above:
1+2
For debugging, often we would like to interupt the current running process. This can be done by pressing the stop button.
When a processing is running, the circle on the right upper corner is filled. When idle, the circle is empty.
import time
while(1):
print('error')
time.sleep(1)
Interupting sometimes does not work. You can reset the state by restarting the kernel. This is done by clicking Kernel/Restart or the Refresh button in the toolbar above.
To save your notebook, either select "File->Save and Checkpoint"
or hit Command-s
for Mac and Ctrl-s
for Windows
To undo changes in each cell, hit Command-z
for Mac and Ctrl-z
for Windows
To undo Delete Cell
, select Edit->Undo Delete Cell
One useful feature of Jupyter Notebook is tab completion
one_plus_one = 1+1
# type `one_` then hit TAB will auto-complete the variable
print(one_plus_one)
print(a)
Another useful feature is the help command. Type any function followed by ?
returns a help window. Hit the x
button to close it.
a = 5
"Insert->Insert New Cell Below"
or click the white plus buttonMarkdown
for textMarkdown
cells by double-clicking them.Help->Keyboard Shortcuts
has a list of keyboard shortcutsThese are the libraries that we will be using in this class:
Numpy
NumPy is the fundamental package for scientific computing with Python.
Scipy
The SciPy library is a collection of numerical algorithms and domain-specific toolboxes, including signal processing, optimization, statistics and much more.
matplotlib
matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.
To import a specific library x
, simply type import x
To access the library function f
, type x.f
If you want to change the library name to y
, type import x as y
import numpy as np
np.ones((3,1))
Unlike MATLAB, there is a difference between int
and float
in Python. Mainly, integer division returns the floor. Always check this when debugging!
In Python 3+, the division operator /
was changed to always perform floating-precision division, casting the output to a float. A new operator, the double-slash //
was introduced to perform integer division
1 / 4
1 / 4.0
1 // 4
1 // 4.
Unlike MATLAB/C, doubles quotes and single quotes are the same thing. Both represent strings. '+'
concatenates strings
# This is a comment
"123 " + 'DSP'
A list is a mutable array of data. That is we can change it after we create it. They can be created using square brackets []
Important functions:
'+'
appends lists.
len(x)
to get length
x = [1, 2, 3] + ["DSP"]
print(x)
print(len(x))
A tuple is an unmutable list. They can be created using round brackets ().
They are usually used as inputs and outputs to functions
t = ("D", "S", "P") + (1, 2, 3)
print(t)
# cannot do assignment
t[0] = 10
# errors in jupyter notebook appear inline
Numpy array is like a list with multidimensional support and more functions. This will be the primary data structure in our class.
x = np.array( [ [1, 2], [3 , 4] ] )
print(x)
x.shape
One major advantage of using numpy arrays is that arithmetic operations on numpy arrays correspond to elementwise operations.
print(x + 2)
You can use np.matrix to do matrix multiplication
np.matrix(x) * np.matrix(x)
You can also use the dot product to do matrix multiplication
x.dot(x)
Numpy uses pass-by-reference semantics so it creates views into the existing array, without implicit copying. This is particularly helpful with very large arrays because copying can be slow.
x = np.array([1,2,3,4,5,6])
print(x)
We slice an array from a to b-1 with [a:b]
y = x[0:4]
print(y)
Because slicing does not copy the array, changing y
changes x
y[0] = 7
print(x)
print(y)
To actually copy x, we should use .copy()
x = np.array([1,2,3,4,5,6])
y = x.copy()
y[0] = 7
print(x)
print(y)
Sometimes we use r_
to create integer sequences in numpy arrays
r_[0:N]
creates an array listing every integer from 0 to N-1
r_[0:N:m]
creates an array listing every m
th integer from 0 to N-1
import numpy as np # by convention, import numpy as np
from numpy import r_ # import r_ function from numpy directly, so that we can call r_ directly instead of np.r_
print(np.r_[-5:5]) # every integer from -5 ... 4
print(np.r_[0:5:2]) # every other integer from 0 ... 4
In this class, we will use matplotlib.pyplot
to plot signals and images.
By convention, we import matplotlib.pyplot
as plt
.
To display the plots inside the browser, we use the command %matplotlib inline
import matplotlib.pyplot as plt # by convention, we import pyplot as plt
# plot in browser instead of opening new windows
%matplotlib inline
# Generate signals
x = np.r_[:1:0.01] # if you don't specify a number before the colon, the starting index defaults to 0
y1 = np.exp( -x )
y2 = np.sin( x*10.0 )/4.0 + 0.5
print(x)
plt.figure()
plt.plot( x, y1 )
plt.figure()
plt.plot( x, y1 )
plt.plot( x, y2 )
plt.figure()
plt.plot( x, y1 )
plt.figure()
plt.plot( x, y2 )
plt.title()
and plt.legend()
¶plt.figure()
plt.plot( x, y1 )
plt.plot( x, y2 )
plt.xlabel( "x axis" )
plt.ylabel( "y axis" )
plt.title( "Title" )
plt.legend( ("blue", "red") )
# xkcd style
with plt.xkcd() :
plt.figure()
plt.plot( x, y1 )
plt.plot( x, y2 )
plt.xlabel( "x axis" )
plt.ylabel( "y axis" )
plt.title( "Title" )
plt.legend( ("blue", "red") )
imshow
will be our default function to plot images
# image
image = np.outer( y1, y2 ) # plotting the outer product of y1 and y2
plt.figure()
plt.imshow(image)
When you finish the work, you can save and share the .ipynb file for an interactive environment, save as html for a static webpage, or File->Print Preview and save as pdf (useful for homework!)