modified from UC Berkeley EE 123 Sp16 tutorial by Miki Lustig, Frank Ong, and Jon Tamir

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:

In [ ]:

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

In [ ]:

```
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

In [ ]:

```
one_plus_one = 1+1
# type `one_` then hit TAB will auto-complete the variable
print(one_plus_one)
```

In [ ]:

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

In [ ]:

```
a = 5
```

- To add a new cell, either select
`"Insert->Insert New Cell Below"`

or click the white plus button - You can change the cell mode from code to text in the pulldown menu. I use
`Markdown`

for text - You can change the texts in the
`Markdown`

cells by double-clicking them. `Help->Keyboard Shortcuts`

has a list of keyboard shortcuts

These 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`

In [ ]:

```
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

In [ ]:

```
1 / 4
```

In [ ]:

```
1 / 4.0
```

In [ ]:

```
1 // 4
```

In [ ]:

```
1 // 4.
```

Unlike MATLAB/C, doubles quotes and single quotes are the same thing. Both represent strings. `'+'`

concatenates strings

In [ ]:

```
# 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

In [ ]:

```
x = [1, 2, 3] + ["DSP"]
print(x)
```

In [ ]:

```
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

In [ ]:

```
t = ("D", "S", "P") + (1, 2, 3)
print(t)
```

In [ ]:

```
# 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.

In [ ]:

```
x = np.array( [ [1, 2], [3 , 4] ] )
print(x)
```

In [ ]:

```
x.shape
```

One major advantage of using numpy arrays is that arithmetic operations on numpy arrays correspond to elementwise operations.

In [ ]:

```
print(x + 2)
```

You can use np.matrix to do matrix multiplication

In [ ]:

```
np.matrix(x) * np.matrix(x)
```

You can also use the dot product to do matrix multiplication

In [ ]:

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

In [ ]:

```
x = np.array([1,2,3,4,5,6])
print(x)
```

We slice an array from a to b-1 with `[a:b]`

In [ ]:

```
y = x[0:4]
print(y)
```

Because slicing does not copy the array, changing `y`

changes `x`

In [ ]:

```
y[0] = 7
print(x)
print(y)
```

To actually copy x, we should use .copy()

In [ ]:

```
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

In [ ]:

```
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`

In [ ]:

```
import matplotlib.pyplot as plt # by convention, we import pyplot as plt
# plot in browser instead of opening new windows
%matplotlib inline
```

In [ ]:

```
# 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)
```

In [ ]:

```
plt.figure()
plt.plot( x, y1 )
```

In [ ]:

```
plt.figure()
plt.plot( x, y1 )
plt.plot( x, y2 )
```

In [ ]:

```
plt.figure()
plt.plot( x, y1 )
plt.figure()
plt.plot( x, y2 )
```

`plt.title()`

and `plt.legend()`

¶In [ ]:

```
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") )
```

In [ ]:

```
# 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

In [ ]:

```
# 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!)

In [ ]:

```
```