Sei sulla pagina 1di 7

First_program

January 28, 2019

1 First task with Python


The first step that must be carried out is the call of packages (or load modules).

In [17]: import numpy as np


import matplotlib.pyplot as plt

Generation of 3 matrices

In [18]: A = np.random.rand(5,5)
B = np.random.rand(5,5)
C = [[0,0,0,1,1],[0,0,0,2,3],[4,5,0,0,0],[4,5,6,7,8],[9,1,2,3,4]]
vt = np.random.rand(5,5)
C = np.array(C)
A = np.array(A)
print A
print B
print A + B

[[0.82802652 0.01624846 0.39132376 0.45885072 0.68168468]


[0.99178191 0.99409247 0.15802919 0.41944572 0.75771355]
[0.49011006 0.98235826 0.94634754 0.45918086 0.80075928]
[0.46792773 0.4899225 0.30787048 0.47710911 0.97429609]
[0.89249286 0.91194707 0.33887166 0.31304281 0.73856632]]
[[0.51854074 0.6544809 0.08080222 0.53876329 0.93327811]
[0.84109251 0.18792337 0.63330178 0.479632 0.38443537]
[0.39417528 0.84805629 0.77006965 0.12840686 0.84252565]
[0.54648175 0.0616771 0.04599922 0.31422468 0.83991187]
[0.45412582 0.19763325 0.21991419 0.0854058 0.89783963]]
[[1.34656726 0.67072937 0.47212598 0.99761402 1.61496279]
[1.83287442 1.18201584 0.79133097 0.89907772 1.14214892]
[0.88428534 1.83041455 1.7164172 0.58758772 1.64328493]
[1.01440948 0.5515996 0.3538697 0.79133379 1.81420796]
[1.34661869 1.10958032 0.55878585 0.39844861 1.63640595]]

1
2 Operations with matrices
In [19]: # EigenValues from a Matrix
from numpy import linalg as LA
w, v = LA.eig(A)
print w,v

[3.0040977 +0. j 0.41206348+0.42098785j 0.41206348-0.42098785j


0.07795866+0.19801972j 0.07795866-0.19801972j] [[ 0.33011191+0. j 0.01486583+0.51974451
-0.18026633-0.24278969j -0.18026633+0.24278969j]
[ 0.46062757+0. j 0.30259466-0.30926963j 0.30259466+0.30926963j
0.02188218+0.02200214j 0.02188218-0.02200214j]
[ 0.56321915+0. j -0.64714436+0. j -0.64714436-0. j
-0.09209418-0.22613636j -0.09209418+0.22613636j]
[ 0.39415916+0. j -0.17293554-0.21731136j -0.17293554+0.21731136j
0.82202419+0. j 0.82202419-0. j]
[ 0.45417079+0. j 0.15063872-0.15431995j 0.15063872+0.15431995j
-0.2320929 +0.34407035j -0.2320929 -0.34407035j]]

In [20]: # Matrix trace


tr = np.trace(A.dot(B))
print tr

7.045690837196286

In [21]: # Inverse of a matrix


A_inv = np.linalg.inv(A)
B_inv = np.linalg.inv(B)
print A
print B

[[0.82802652 0.01624846 0.39132376 0.45885072 0.68168468]


[0.99178191 0.99409247 0.15802919 0.41944572 0.75771355]
[0.49011006 0.98235826 0.94634754 0.45918086 0.80075928]
[0.46792773 0.4899225 0.30787048 0.47710911 0.97429609]
[0.89249286 0.91194707 0.33887166 0.31304281 0.73856632]]
[[0.51854074 0.6544809 0.08080222 0.53876329 0.93327811]
[0.84109251 0.18792337 0.63330178 0.479632 0.38443537]
[0.39417528 0.84805629 0.77006965 0.12840686 0.84252565]
[0.54648175 0.0616771 0.04599922 0.31422468 0.83991187]
[0.45412582 0.19763325 0.21991419 0.0854058 0.89783963]]

In [23]: # Product between different matrices


a = [[1,2,3],[4,5,6], [1,2,3]]
a = np.array(a);
b = [[1,2,1],[1,0,1],[2,0,1]]

2
b = np.array(b)
c = np.dot(a,b)
print c

[[ 9 2 6]
[21 8 15]
[ 9 2 6]]

3 Generate a time vector to plot line


In [24]: t = np.linspace(-0.02, 0.05, 1000)
plt.plot(t, 325 * np.sin(2*np.pi*50*t));
plt.xlabel('t');
plt.ylabel('x(t)');
plt.title(r'Plot of CT signal $x(t)=325 \sin(2\pi 50 t)$');
plt.xlim([-0.02, 0.05]);
#plt.ylim([-200, 200]);
plt.show()

In [25]: n = np.arange(50);
nn = np.arange(5,-10,-2);
dt = 0.07/50

3
x = np.sin(2 * np.pi * 50 * n * dt)
plt.xlabel('n');
plt.ylabel('x[n]');
plt.title(r'Plot of DT signal $x[n] = 325 \sin(2\pi 50 n \Delta t)$');
plt.stem(n, x);
print nn,n

[ 5 3 1 -1 -3 -5 -7 -9] [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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]

In [26]: t = np.linspace(-0.02, 0.05, 1000)


plt.subplot(2,1,1); plt.plot(t, np.abs(np.exp(2j*np.pi*50*t)) );
plt.xlabel(r'$t$');
plt.ylabel(r'$|x(t)|$');
plt.title(r'Absolute value of $x(t)=e^{j 100 \pi t}$');
plt.xlim([-0.02, 0.05]);
plt.subplot(2,1,2); plt.plot(t, np.angle(np.exp(2j*np.pi*50*t))*360/(2*np.pi) );
plt.xlabel('$t$');
plt.ylabel(r'$\angle x(t)$');
plt.title(r'Phase of $x(t)=e^{j 100 \pi t}$');
plt.xlim([-0.02, 0.05]);
plt.show()

4
In [27]: %matplotlib inline
t = np.linspace(-0.02, 0.05, 1000)
plt.subplot(2,1,1); plt.plot(t, np.exp(2j*np.pi*50*t).real );
plt.xlabel('t');
plt.ylabel('Re x(t)');
plt.title(r'Real part of $x(t)=e^{j 100 \pi t}$');
plt.xlim([-0.02, 0.05]);
plt.subplot(2,1,2); plt.plot(t, np.exp(2j*np.pi*50*t).imag );
plt.xlabel('t');
plt.ylabel('Im x(t)');
plt.title(r'Imaginary part of $x(t)=e^{j 100 \pi t}$');
plt.xlim([-0.02, 0.05]);
plt.show()

5
4 The second part: Load image
In [2]: import skimage.io as io
from skimage.color import rgb2gray
img = io.imread('coqueta.png')
img_grayscale = rgb2gray(img)
show_grayscale = io.imshow(img_grayscale)
io.show()

/Users/jipadilla7/anaconda2/lib/python2.7/site-packages/skimage/io/_plugins/matplotlib_plugin.py
out_of_range_float = (np.issubdtype(image.dtype, np.float) and

6
7

Potrebbero piacerti anche