-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotting.py
More file actions
128 lines (102 loc) · 3.31 KB
/
plotting.py
File metadata and controls
128 lines (102 loc) · 3.31 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
import json
import matplotlib.pyplot as plt
import numpy as np
from tqdm import trange
from scipy import ndimage
from scipy import signal
def schmoof(t, pos, num_vals, window_size):
t_base = np.linspace(min(t), max(t), num_vals)
smooth = np.empty_like(t_base)
for i in trange(len(t_base)):
t_local = t_base[i]
selected_idxs = np.where(np.abs(t - t_local) < window_size)
selected_ts = t[selected_idxs]
weights = 1 / (1 + (np.abs(t_local - selected_ts)))
weights /= sum(weights)
smooth[i] = np.sum(pos[selected_idxs] * weights)
return t_base, smooth
def rotate(x, y, theta):
c = np.cos(theta)
s = np.sin(theta)
x[:] = x * c - y * s
y[:] = x * s + y * c
def main():
with open("data.json", "r") as f:
positions = json.load(f)
m = len(positions) / 50.0185
tm = np.array([t % m for t in range(len(positions))])
sorted_idxs = np.argsort(tm)
t = tm[sorted_idxs]
x_raw = np.array([p[0] for p in positions])
y_raw = np.array([p[1] for p in positions])
x = x_raw[sorted_idxs]
y = y_raw[sorted_idxs]
rotate(x_raw, y_raw, 0.016)
rotate(x, y, 0.016)
t_smooth, x_smooth = schmoof(t, x, 2000, 1)
x_smooth = ndimage.gaussian_filter1d(x_smooth, 5)
peaks = t_smooth[signal.find_peaks(x_smooth)[0]]
valleys = t_smooth[signal.find_peaks(-x_smooth)[0]]
if True:
fig, ax = plt.subplots()
plt.suptitle("Raw X Y over time")
ax.plot(x_raw, c="r")
ax.set_ylabel("x")
ax.set_xlabel("frame number")
ax2 = ax.twinx()
ax2.plot(y_raw, c="g")
ax2.set_ylabel("y")
plt.title("Positions over time")
plt.show()
if True:
fig, ax = plt.subplots()
plt.suptitle("Overlayed")
ax.scatter(list(range(len(x))), x, c="r")
ax.set_ylabel("x")
ax.set_xlabel("frame number (50x oversample)")
ax2 = ax.twinx()
ax2.scatter(list(range(len(y))),y, c="g")
ax2.set_ylabel("y")
plt.title("Positions over time")
plt.show()
if False:
fig, ax = plt.subplots()
for peak in peaks:
ax.axvline(peak, c="r")
for valley in valleys:
ax.axvline(valley, c="b")
ax.scatter(t, x, c="r")
ax.plot(t_smooth, x_smooth, c="b")
ax.set_ylabel("x")
ax.set_xlabel("frame number")
ax2 = ax.twinx()
ax2.scatter(t, y, c="g")
ax2.set_ylabel("y")
plt.title("Positions over time")
plt.show()
if True:
plt.suptitle("Raw X Y positions")
plt.scatter(x, y)
plt.show()
if True:
plt.suptitle("FFT of X")
fft = np.absolute(np.fft.fft(x_raw))
freqs = np.fft.fftfreq(x_raw.size, 1/240)
plt.plot(freqs, fft)
plt.show()
if True:
plt.suptitle("FFT of Y")
fft = np.absolute(np.fft.fft(y_raw))
freqs = np.fft.fftfreq(y_raw.size, 1/240)
plt.plot(freqs, fft)
plt.show()
if False:
deltas = np.diff(sorted(np.hstack((peaks, valleys))))
plt.hist(deltas)
plt.show()
mean = np.mean(deltas)
std = np.std(deltas)
print(f"frame deltas are roughly: {mean} +- {std}")
print(f"at 240fps, in hz: {240/mean} +- {240/std}")
if __name__ == '__main__':
main()