This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proj10_main.py
165 lines (123 loc) · 3.48 KB
/
proj10_main.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
# -*- coding: utf-8 -*-
################################################################
# #
# Seth Cram #
# ECE351-53 #
# Project 9 #
# Due: 3/29/2022 #
# Any other necessary information needed to navigate the file #
#
#
################################################################
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as sig
import control as con
#default vals
R = 1000
L = 27e-3
C = 100e-9
steps = 1000
#in rad/s (DONT USE 1**3 AS ARG (fails))
w = np.arange(1e3, 1e6+steps, steps)
#TASK 1.1
#prelab eqts
mag = (w/(R*C)) / np.sqrt(w**4 + ((1/(R*C))**2 - 2/(L*C))*(w**2) + (1/(L*C))**2)
phase = np.pi/2 - np.arctan((w/(R*C)) / (-1*(w**2)+(1/(L*C))))
#conv to db and degs
dbMag = 20*np.log10(mag)
degPhase = np.rad2deg(phase)
#need to shift by 180 degs later half to make look better
i=0
while(i < len(w)):
if(degPhase[i] > 90):
degPhase[i] -= 180
i += 1
#TASK 1.2
num = [1/(R*C), 0]
denom = [1, 1/(R*C), 1/(L*C)]
syst = sig.lti(num,denom)
(bodeW, bodeMag, bodePhase) = sig.bode(syst, w)
#PLOTS
font = {'family' : 'normal',
'weight' : 'bold',
'size' : 22}
#plot1:
plt.rc('font', **font)
plt.figure(figsize = (30,30))
plt.subplot(2, 1, 1)
plt.semilogx(w, dbMag)
plt.grid() #add a grid to graph
plt.title('Bode plot for Task 1.1')
plt.ylabel('|H(jw)| (dB)')
#plot2:
plt.subplot(2, 1, 2)
plt.semilogx(w, degPhase)
plt.grid() #add a grid to graph
plt.ylabel('angle of H(jw) (degs)')
plt.xlabel('w (rads/s)')
plt.show() #display figure
#plot1:
plt.rc('font', **font)
plt.figure(figsize = (30,30))
plt.subplot(2, 1, 1)
plt.semilogx(bodeW, bodeMag)
plt.grid() #add a grid to graph
plt.title('Bode plot for Task 1.2')
plt.ylabel('|H(jw)| (dB)')
#plot2:
plt.subplot(2, 1, 2)
plt.semilogx(bodeW, bodePhase)
plt.grid() #add a grid to graph
plt.ylabel('angle of H(jw) (degs)')
plt.xlabel('w (rads/s)')
plt.show() #display figure
#TASK 1.3
sys3 = con.TransferFunction(num, denom)
plt.figure(figsize = (15,15))
#used _ = .... to suppress the output (function auto-plots)
_ = con.bode(sys3, w, dB = True, Hz = True, deg = True, plot = True)
plt.title('Bode plot for Task 1.3')
"""#(conMag, conPhase, conW) = con.bode(sys3, w, dB = True, Hz = True, deg = True, plot = True)
#plot1:
plt.rc('font', **font)
plt.figure(figsize = (30,30))
plt.subplot(2, 1, 1)
plt.semilogx(conW, conMag)
plt.grid() #add a grid to graph
plt.title('Bode plot for Task 1.3')
plt.ylabel('|H(jw)| (dB)')
#plot2:
plt.subplot(2, 1, 2)
plt.semilogx(conW, conPhase)
plt.grid() #add a grid to graph
plt.ylabel('angle of H(jw) (degs)')
plt.xlabel('w (Hz)')
plt.show() #display figure
"""
#TASK 2.1
#chosen as x(t)'s max w_0
fs = 2*np.pi*50000
steps = 1/fs
t = np.arange(0, 1e-2 + steps , steps)
x = np.cos(2*np.pi*100*t) + np.cos(2*np.pi*3024*t) + np.sin(2*np.pi*50000*t)
#plot:
plt.figure(figsize = (15,15))
plt.plot(t, x)
plt.grid() #add a grid to graph
plt.title('x(t) vs t')
plt.ylabel('x(t)')
plt.xlabel('t (s)')
plt.show() #display figure
#TASK 2.2: conv H(s) to z-dom
(zNum, zDenom) = sig.bilinear(num, denom, 500000)
#TASK 2.3: pass x(t) thru z-dom filter
y = sig.lfilter(zNum, zDenom, x)
#plot:
plt.figure(figsize = (15,15))
plt.plot(t, y)
plt.grid() #add a grid to graph
plt.title('y(t) vs t')
plt.ylabel('y(t)')
plt.xlabel('t (s)')
plt.show() #display figure