SIGNAL PROCESSING
Laboratory #1

Introduction to Python and Canopy environment

M. Kociński, P. Strumiłło, A. Borowska-Terka

Medical Electronics Division
Institute of Electronics


In [1]:
import funkcje_do_lab1 as f
f.hide()
Out[1]:
The raw code for this IPython notebook is by default hidden for easier reading. To toggle on/off the raw code, click here.

PURPOSE:

To get acquainted with the Canopy environment – declaring variables, arrays and array operations.

TASKS:
1. Create your directory on the hard drive, eg. ‘D:/Biomed2014_1’. All the scripts, functions and files are to be saved in this directory.
2. Start the Editor of Canopy
3. Change the current working directory of Canopy environment to the directory you created. (In the main command window, type >>cd D:/Biomed2014_1)
4. To double-check what is your current directory type >>pwd

EXPECTED RESULTS:

In [2]:
home = f.ad1()
*****
current directory:
H:\Signal processing\lab_1

*****
changed directory for D
D:\

*****
create and change directory: D:/Biomed2014_1
D:\Biomed2014_1

*****
listing the contents of current directory
[]

5.In the Editor of Canopy, declare the following variables (hint: use http://wiki.scipy.org/Tentative_NumPy_Tutorial and go to ‘Array creation’)

In [3]:
f.fig(f.w1)
Out[3]:

Check type of each of the defined matrices by entering variable_name.ndim, variable_name.shape, variable_name.size, variable_name.dtype, variable_name.itemsize.
Type f=zeros_like(d) to create a new array f of the same size and data type as array d but filled with zeros.
Create array b1 with the same size and values as for array b, but of dtype=’uint8’

EXPECTED RESULTS:

In [4]:
f.ad5()

a= 5

b= [ 1 -2  4]

c= [[ 2.]
 [ 0.]
 [-1.]]
c2= [[5]
 [4]]

d= [[ 1  0  2]
 [ 4 -3  1]
 [ 2  2  3]]
e= [[ 1.+1.j]
 [ 1.+0.j]]

Create complex number: 1+3j, using command >>complex
e1= (1+3j)

**********************************************************************
Parameters of matrices (dimensions, shape, size, dtype, itemsize):
**********************************************************************
[ 1 -2  4] 1 (3,) 3 int32 4
*****
[[ 2.]
 [ 0.]
 [-1.]] 2 (3, 1) 3 float64 8
*****
[[ 1  0  2]
 [ 4 -3  1]
 [ 2  2  3]] 2 (3, 3) 9 int32 4
*****
[[ 1.+1.j]
 [ 1.+0.j]] 2 (2, 1) 2 complex128 16
*****

******************************
Zeros like and change of dtype:
******************************
parameters of f: int32 (3, 3)
parameters of b: [ 1 -2  4] int32
parameters of b1: [  1 254   4] uint8

6.Creating vectors:

Type in help(arange)
Type in the commands: g=arange(10); h=arange(10.), i=arange(5,15,3)
Create the following vectors:

In [5]:
f.fig(f.w2)
Out[5]:

Hints: check Canopy help for the following functions:zeros, ones
(to print out the help content type >>help(function_name))

EXPECTED RESULTS:

In [6]:
f.ad6()
g= [0 1 2 3 4 5 6 7 8 9]

h= [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]

i= [ 5  8 11 14]

j= [  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
  91  92  93  94  95  96  97  98  99 100 101]

k= [ 500  490  480  470  460  450  440  430  420  410  400  390  380  370  360
  350  340  330  320  310  300  290  280  270  260  250  240  230  220  210
  200  190  180  170  160  150  140  130  120  110  100   90   80   70   60
   50   40   30   20   10    0  -10  -20  -30  -40  -50  -60  -70  -80  -90
 -100 -110 -120 -130 -140 -150 -160 -170 -180 -190 -200 -210 -220 -230 -240
 -250 -260 -270 -280 -290 -300 -310 -320 -330 -340 -350 -360 -370 -380 -390
 -400 -410 -420 -430 -440 -450 -460 -470 -480 -490 -500 -510 -520 -530 -540
 -550 -560 -570 -580 -590 -600 -610 -620 -630 -640 -650 -660 -670 -680 -690
 -700 -710 -720 -730 -740 -750 -760 -770 -780 -790 -800 -810 -820 -830 -840
 -850 -860 -870 -880 -890 -900]

l= [  0.    0.1   0.2   0.3   0.4   0.5   0.6   0.7   0.8   0.9   1.    1.1
   1.2   1.3   1.4   1.5   1.6   1.7   1.8   1.9   2.    2.1   2.2   2.3
   2.4   2.5   2.6   2.7   2.8   2.9   3.    3.1   3.2   3.3   3.4   3.5
   3.6   3.7   3.8   3.9   4.    4.1   4.2   4.3   4.4   4.5   4.6   4.7
   4.8   4.9   5.    5.1   5.2   5.3   5.4   5.5   5.6   5.7   5.8   5.9
   6.    6.1   6.2   6.3   6.4   6.5   6.6   6.7   6.8   6.9   7.    7.1
   7.2   7.3   7.4   7.5   7.6   7.7   7.8   7.9   8.    8.1   8.2   8.3
   8.4   8.5   8.6   8.7   8.8   8.9   9.    9.1   9.2   9.3   9.4   9.5
   9.6   9.7   9.8   9.9  10. ]

m= [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]

p= [[1 0]
 [0 1]]


7.Type in %whos to see the defined variables in Canopy workspace, use %xdel command to delete some of the defined variables. Note that %reset command clears all the variables, imported modules and defined functions.
8.Create vectors by using the array() commands: r=[1 -2 4], s=[7 2 3] and compute: r+s, r*s, r**s, dot(r,s)

EXPECTED RESULTS:

In [7]:
f.ad8()
r + s = [8 0 7]
r * s = [ 7 -4 12]
r ** s = [ 1  4 64]
np.dot(r,s)= 15

9.Add, multiply, divide by 2 and compute the power of 2 of vector r by using commands: r+=2, r*=2, r/=2, r**=2

EXPECTED RESULTS:

In [8]:
f.ad9()
r =  [ 1 -2  4]
r += 2: [3 0 6]
r *= 2: [ 6  0 12]
r /= 2: [3 0 6]
r **= 2: [ 9  0 36]


10.Define array t:

In [9]:
f.fig(f.m)
Out[9]:

and compute: t.min(), t.max(),: t.min(axis=0), t.min(axis=1), t.max(axis=0), t.max(axis=1).
Consult help prod, average, std, var, round, clip, ptp, floor, ceil, flipud, fliplr, rot90 and use these commands for array t.

EXPECTED RESULTS:

In [10]:
f.ad10()
t= [[ 1.1    4.56   8.999]
 [ 2.     3.     4.   ]
 [ 6.78   2.44   5.55 ]]

t.min(): 1.1
t.max(): 8.999
t.min(axis=0): [ 1.1   2.44  4.  ]
t.min(axis=1): [ 1.1   2.    2.44]
t.max(axis=0): [ 6.78   4.56   8.999]
t.max(axis=1): [ 8.999  4.     6.78 ]
t.prod(): 99466.1995825
np.average(t): 4.26988888889
t.std(): 2.37165064152
t.var(): 5.62472676543
t.round():
[[ 1.  5.  9.]
 [ 2.  3.  4.]
 [ 7.  2.  6.]]
t.clip(1,3):
[[ 1.1   3.    3.  ]
 [ 2.    3.    3.  ]
 [ 3.    2.44  3.  ]]
np.floor(t):
[[ 1.  4.  8.]
 [ 2.  3.  4.]
 [ 6.  2.  5.]]
np.ceil(t):
[[ 2.  5.  9.]
 [ 2.  3.  4.]
 [ 7.  3.  6.]]
np.flipud(t):
[[ 6.78   2.44   5.55 ]
 [ 2.     3.     4.   ]
 [ 1.1    4.56   8.999]]
np.fliplr(t):
[[ 8.999  4.56   1.1  ]
 [ 4.     3.     2.   ]
 [ 5.55   2.44   6.78 ]]
np.rot90(t):
[[ 8.999  4.     5.55 ]
 [ 4.56   3.     2.44 ]
 [ 1.1    2.     6.78 ]]