-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_2.py
More file actions
48 lines (38 loc) · 1.07 KB
/
1_2.py
File metadata and controls
48 lines (38 loc) · 1.07 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
import numpy as np
import matplotlib.pyplot as plt
def plotSpectrum(y,Fs):
n = len(y) # length of the signal
k = np.arange(n)
T = n/Fs
frq = k/T # two sides frequency range
frq = frq[range(n//2)] # one side frequency range
Y = np.fft.fft(y)/n # fft computing and normalization
Y = Y[range(n//2)]
plt.plot(frq,np.abs(Y),'r') # plotting the spectrum
plt.xlabel('Freq (Hz)')
plt.ylabel('|Y(freq)|')
time = np.arange(0, 2 * np.pi, 2 * np.pi /1024);
# f1 = 10
# f2 = 20
# fd = 1024
# w1 = 2*np.pi *f1/fd
# print(w1)
# w2 = 2*np.pi * f2/fd
# print(w2)
period = np.arange(0, 2 * np.pi / 10, 2 * np.pi/ 10 /1024);
amplitude = np.sin(10 * time) + np.sin(20 * time)
a_period = np.sin(10 * period) + np.sin(20 * period)
fig = plt.figure(figsize=(9, 9))
fig.add_axes([0, 0, 1, 1])
plt.subplot(2, 1, 1)
plt.plot(period, a_period, color = 'blue')
plt.title('A')
plt.xlabel('T')
plt.ylabel('Amplitude = sin(10t) + sin(20t)')
plt.grid(True, which='both')
plt.axhline(y=0, color='k')
plt.subplot(2, 1, 2)
axes = plt.gca()
axes.set_xlim([0, 30])
plotSpectrum(amplitude, 1024)
plt.show()