/
=singnal.py
206 lines (170 loc) · 5.42 KB
/
=singnal.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 15 17:13:08 2016
@author: TanMingjun
"""
from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
from copy import deepcopy
class waves1:
def _init_(self):
self.x = np.linspace(0,1,101)
self.y = np.exp(-1000*(self.x-0.3)**2)+0.5*np.exp(-1000*(self.x-0.7)**2)
self.y[0] = 0
self.y[-1] = 0
def iteration(self):
self.x = np.linspace(0,1,101)
self.y_now = np.exp(-1000*(self.x-0.3)**2)+0.5*np.exp(-1000*(self.x-0.7)**2)
self.y_now[0] = 0
self.y_now[-1] = 0
self.y_old = deepcopy(self.y_now)
self.y_new = np.zeros(101)
self.T=[]
self.N=[]
for j in range(1200):
for i in range(101):
if i== 0 or i== 100:
self.y_new[i] = 0
else:
self.y_new[i] = - self.y_old[i] + self.y_now[i+1] + self.y_now[i-1]
self.y_old = deepcopy(self.y_now)
self.y_now = deepcopy(self.y_new)
self.N.append(self.y_now[5])
self.T.append(0.01/300*j)
return self.y_now, self.T,self.N
class waves2:
def _init_(self):
self.x = np.linspace(0,1,101)
self.y = np.sqrt(0.25-(self.x-0.5)**2)
self.y[0] = 0
self.y[-1] = 0
def iteration(self):
self.x = np.linspace(0,1,101)
self.y_now = np.sqrt(0.25-(self.x-0.5)**2)
self.y_now[0] = 0
self.y_now[-1] = 0
self.y_old = deepcopy(self.y_now)
self.y_new = np.zeros(101)
self.T=[]
self.N=[]
for j in range(1200):
for i in range(101):
if i== 0 or i== 100:
self.y_new[i] = 0
else:
self.y_new[i] = - self.y_old[i] + self.y_now[i+1] + self.y_now[i-1]
self.y_old = deepcopy(self.y_now)
self.y_now = deepcopy(self.y_new)
self.N.append(self.y_now[5])
self.T.append(0.01/300*j)
return self.y_now, self.T,self.N
class waves3:
def _init_(self):
self.x = np.linspace(0,1,101)
self.y= np.zeros(101)
for i in range(101):
if 10<=i<=30:
self.y[i] = 0.5
else:
self.y[i] = 0
self.y[0] = 0
self.y[-1] = 0
def iteration(self):
self.x = np.linspace(0,1,101)
self.y_now= np.zeros(101)
for i in range(101):
if 10<=i<=30:
self.y_now[i] = 0.5
else:
self.y_now[i] = 0
self.y_now = np.exp(-1000*(self.x-0.3)**2)
self.y_now[0] = 0
self.y_now[-1] = 0
self.y_old = deepcopy(self.y_now)
self.y_new = np.zeros(101)
self.T=[]
self.N=[]
for j in range(1200):
for i in range(101):
if i== 0 or i== 100:
self.y_new[i] = 0
else:
self.y_new[i] = - self.y_old[i] + self.y_now[i+1] + self.y_now[i-1]
self.y_old = deepcopy(self.y_now)
self.y_now = deepcopy(self.y_new)
self.N.append(self.y_now[5])
self.T.append(0.01/300*j)
return self.y_now, self.T,self.N
class waves4:
def _init_(self):
self.x = np.linspace(0,1,101)
self.y= np.zeros(101)
for i in range(101):
if 0<=i<=25:
self.y[i] = 1/50*i
else:
self.y[i] = -1/150*i+2/3
self.y[0] = 0
self.y[-1] = 0
def iteration(self):
self.x = np.linspace(0,1,101)
self.y_now= np.zeros(101)
for i in range(101):
if 0<=i<=25:
self.y_now[i] = 1/50*i
else:
self.y_now[i] = -1/150*i+2/3
self.y_now[0] = 0
self.y_now[-1] = 0
self.y_old = deepcopy(self.y_now)
self.y_new = np.zeros(101)
self.T=[]
self.N=[]
for j in range(1200):
for i in range(101):
if i== 0 or i== 100:
self.y_new[i] = 0
else:
self.y_new[i] = - self.y_old[i] + self.y_now[i+1] + self.y_now[i-1]
self.y_old = deepcopy(self.y_now)
self.y_now = deepcopy(self.y_new)
self.N.append(self.y_now[5])
self.T.append(0.01/300*j)
return self.y_now, self.T,self.N
plt.subplot(221)
A = waves1()
A.iteration()
plt.plot(A.T,A.N,'r')
plt.xlabel('Time(s)')
plt.ylabel('Signal(arbitrary units)')
plt.title('two Gauss')
plt.grid(True)
plt.show()
plt.subplot(222)
A = waves2()
A.iteration()
plt.plot(A.T,A.N,'g')
plt.xlabel('Time(s)')
plt.ylabel('Signal(arbitrary units)')
plt.title('Semicircle')
plt.grid(True)
plt.show()
plt.subplot(223)
A = waves3()
A.iteration()
plt.plot(A.T,A.N,'k')
plt.xlabel('Time(s)')
plt.ylabel('Signal(arbitrary units)')
plt.title('Square wave')
plt.grid(True)
plt.show()
plt.subplot(224)
A = waves4()
A.iteration()
plt.plot(A.T,A.N,'b')
plt.xlabel('Time(s)')
plt.ylabel('Signal(arbitrary units)')
plt.title('Triangular wave')
plt.grid(True)
plt.show()