-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathpythonconv_1d.py
More file actions
66 lines (46 loc) · 1.23 KB
/
pythonconv_1d.py
File metadata and controls
66 lines (46 loc) · 1.23 KB
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
import numpy as np
import matplotlib.pyplot as plt
##########################################
# Exemple du cours
f = np.array([1,0,3,5,1])
g = np.array([1,2,3])
h = np.convolve(f,g,'same')
print("=== Exemple ===")
print('f =',f)
print('g =',g)
print('h =',h)
##########################################
# Exemple du cours
f = np.array([1,0,3,5,1])
g = np.array([1,2,3])
h = np.convolve(f,g,'full')
print("=== Exemple ===")
print('f =',f)
print('g =',g)
print('h =',h)
# Visualisation
def affichage_convolution(f,g,mode='same'):
h = np.convolve(f,g,mode=mode)
ax = plt.subplot(2,1,1)
ax.set_title("Entrée f")
plt.plot(f)
# ax = plt.subplot(3,1,2)
# ax.set_title("fonction g")
# plt.plot(g,color='orange')
ax = plt.subplot(2,1,2)
ax.set_title("Sortie h = f*g")
plt.plot(h,color='red')
plt.subplots_adjust(top=0.9, bottom=0.1, left=0.1, right=0.95, hspace=1.0,wspace=0.5)
# plt.tight_layout()
# plt.savefig('convolution-1d-2.png')
plt.show()
return
# Exemple 1
f = np.array([4,2,1,4,5,1,3])
g = 1/3*np.array([1,1,1])
# affichage_convolution(f,g,mode='same')
# Exemple 2 : moyenne mobile et bruit
N = 100
f = np.sin(np.linspace(0,2*np.pi,N)) + 1*np.random.random(N)
g = 1/5*np.ones(5)
affichage_convolution(f,g,mode='same')