-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphs.py
More file actions
93 lines (69 loc) · 2.58 KB
/
Graphs.py
File metadata and controls
93 lines (69 loc) · 2.58 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
import numpy as np
import matplotlib.pyplot as plt
def fft_data(data):
"""
This function takes the data and returns the fft of the data
:param data:
:return: fft of the data (x+iy) and absolute value of the fft (magnitude of the fft)
"""
fft_result = np.fft.fft(data)
return fft_result.tolist(), np.abs(fft_result).tolist()
def visualize_data(x, y, z, sps, plot_type, fig, axs):
"""
This function visualizes the data in x, y, z directions with respect to time
:param x:
:param y:
:param z:
:param sps samples per second:
:return: plots the data
"""
# fig, axs = plt.subplots(1, 3, figsize=(15, 5))
names = ["x - axis", "y - axis", "z - axis"]
if plot_type == "time":
for i, ax in enumerate(axs[0]):
ax.clear()
data = [x, y, z][i]
time = len(data) / sps # time = number of samples / samples per second
horizontal_axis = np.linspace(0, time, len(data))
ax.plot(horizontal_axis, data)
ax.set_title(names[i])
ax.set_xlabel("Time (s)")
ax.set_ylabel('Magnitude')
elif plot_type == "frequency":
for i, ax in enumerate(axs[1]):
ax.clear()
data = [x, y, z][i]
horizontal_axis = np.linspace(int(-sps / 2), int(sps / 2), len(data))
ax.stem(horizontal_axis, data[int(np.ceil(len(data) / 2)) + 1:] + data[:int(np.ceil(len(data) / 2)) + 1])
ax.set_title(names[i])
ax.set_xlabel("Frequency (Hz)")
ax.set_ylabel('Magnitude')
plt.tight_layout()
plt.pause(0.01)
fig.canvas.flush_events()
# plt.show()
def visualize_data_time_only(x, y, z, sps, fig, axs):
names = ["x - axis", "y - axis", "z - axis"]
data_all = [x, y, z]
time = len(x) / sps # time = number of samples / samples per second
horizontal_axis = np.linspace(0, time, len(x))
for i, ax in enumerate(axs):
ax.clear()
data = data_all[i]
ax.plot(horizontal_axis, data)
ax.set_title(names[i])
ax.set_xlabel("Time (s)")
ax.set_ylabel('Magnitude')
plt.tight_layout()
plt.pause(0.01)
fig.canvas.flush_events()
def visualize_anomalies(x, y, z, x_in, y_in, z_in, sps, fig, axs):
data_all = [np.array(x)[x_in], np.array(y)[y_in], np.array(z)[z_in]]
indices_all = [x_in / sps, y_in / sps, z_in / sps]
for i, ax in enumerate(axs):
data = data_all[i]
indices = indices_all[i]
ax.scatter(indices, data, c="r")
plt.tight_layout()
plt.pause(0.01)
fig.canvas.flush_events()