-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
175 lines (140 loc) · 3.71 KB
/
main.py
File metadata and controls
175 lines (140 loc) · 3.71 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
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
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import find_peaks
from scipy import signal
from scipy.fft import fft
import matplotlib.pyplot as plt
from matplotlib import patches
from matplotlib.figure import Figure
from matplotlib import rcParams
s = np.load('input_signal.npy')
#### A
Xn=np.arange(0,2,0.0002)
plt.plot(Xn, s)
plt.xlabel('time')
plt.ylabel('amplitude ')
plt.show()
plt.grid()
#### B
plt.plot(Xn, s)
plt.axis([0.1,0.125,-1,1])
plt.xlabel('time')
plt.ylabel('amplitude ')
plt.show()
plt.grid()
#### C
Sfft=fft(s)
D=20*np.log10(abs(Sfft)/5000)
Dn=np.linspace(0,((10000-1)/10000)*5000,10000)
plt.plot(Dn,D)
plt.xlabel('freq')
plt.ylabel('amplitude[dB]')
plt.show()
plt.grid()
##### find peak D
peak = find_peaks(D,-100)
print(peak)
print(Dn[1000])
print(Dn[9000])
###### E + H
fs = 5000
fr1 = 500
fr2 = 4500
w1 = 2*np.pi*(fr1/fs)
w2 = 2*np.pi*(fr2/fs)
w = np.linspace(0 , 2*np.pi , 1000)
z = np.exp(-1j*w)
H = ((z - np.exp(1j*w1))*(z - np.exp(1j*w2)))/((z - 0.9*np.exp(1j*w1))*(z - 0.9*np.exp(1j*w2)))
plt.plot(w,H)
plt.xlabel('w[rad\smp]')
plt.ylabel('amplitude')
plt.show()
plt.grid()
####### G
def zplane(b, a, filename=None):
"""Plot the complex z-plane given a transfer function."""
# get a figure/plot
ax = plt.subplot(111)
# create the unit circle
uc = patches.Circle((0, 0), radius=1, fill=False, color='black', ls='dashed')
ax.add_patch(uc)
# The coefficients are less than 1, normalize the coeficients
if np.max(b) > 1:
kn = np.max(b)
b = b / float(kn)
else:
kn = 1
if np.max(a) > 1:
kd = np.max(a)
a = a / float(kd)
else:
kd = 1
# Get the poles and zeros
p = np.roots(a)
z = np.roots(b)
k = kn / float(kd)
# Plot the zeros and set marker properties
t1 = plt.plot(z.real, z.imag, 'go', ms=10)
plt.setp(t1, markersize=10.0, markeredgewidth=1.0,
markeredgecolor='k', markerfacecolor='g')
# Plot the poles and set marker properties
t2 = plt.plot(p.real, p.imag, 'rx', ms=10)
plt.setp(t2, markersize=12.0, markeredgewidth=3.0,
markeredgecolor='r', markerfacecolor='r')
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
# set the ticks
r = 1.5;
plt.axis('scaled');
plt.axis([-r, r, -r, r])
ticks = [-1, -.5, .5, 1];
plt.xticks(ticks);
plt.yticks(ticks)
if filename is None:
plt.show()
else:
plt.savefig(filename)
return z, p, k
Poles =[1.0 , -(0.9*np.exp(1j*-w1)+0.9*np.exp(1j*w1)) ,0.81]
ZERO = [1.0, -(np.exp(1j*-w1)+np.exp(1j*w1)) ,1.0]
PZ = zplane(ZERO ,Poles , filename=None)
##### I+J+k
nw = np.linspace(0 ,2 , 10000)
y = np.zeros(10000)
for n in range( 2 ,9998 ):
y[n]=s[n]-2*np.cos(np.pi/5)*s[n-1]+s[n-2]+1.8*np.cos(np.pi/5)*y[n-1]-0.81*y[n-2]
plt.plot(nw,y)
plt.xlabel('time')
plt.ylabel('amplitude')
plt.show()
plt.grid()
##### L
nw = np.linspace(0 ,2 , 10000)
y = np.zeros(10000)
for n in range( 2 ,9998 ):
y[n]=s[n]-2*np.cos(np.pi/5)*s[n-1]+s[n-2]+1.8*np.cos(np.pi/5)*y[n-1]-0.81*y[n-2]
plt.plot(nw,y)
plt.xlim(0.1,0.5)
plt.xlabel('time')
plt.ylabel('amplitude')
plt.show()
plt.grid()
####### M
def trash(y):
Ny = np.zeros(10000)
for n in range (0,10000):
if y[n]>=0:
Ny[n]=1
elif y[n]<0:
Ny[n] = 0
return Ny
ny = trash(y)
plt.plot(nw,ny)
plt.xlim(0.1,0.5)
plt.xlabel('time')
plt.ylabel('amplitude')
plt.show()
plt.grid()
##### o 5ms