-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
41 lines (33 loc) · 1.47 KB
/
utils.py
File metadata and controls
41 lines (33 loc) · 1.47 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
import numpy as np
from random import randint
import os
class ActivityCounter:
def __init__(self, threshold, window_size_sec):
self.threshold = threshold
self.window_size_sec = window_size_sec
self.buffer_x = []
self.buffer_y = []
self.buffer_z = []
self.activity_counts = []
def add_data(self, x, y, z):
# Append the incoming data point to buffers
self.buffer_x.append(x)
self.buffer_y.append(y)
self.buffer_z.append(z)
# Process if we have enough data for at least one window
if len(self.buffer_x) >= self.window_size_sec:
# Extract window data
window_x = self.buffer_x[:self.window_size_sec]
window_y = self.buffer_y[:self.window_size_sec]
window_z = self.buffer_z[:self.window_size_sec]
# Remove the processed window data from the buffers
self.buffer_x = self.buffer_x[self.window_size_sec:]
self.buffer_y = self.buffer_y[self.window_size_sec:]
self.buffer_z = self.buffer_z[self.window_size_sec:]
# Calculate magnitude of the window
magnitude = np.sqrt(np.square(window_x) + np.square(window_y) + np.square(window_z))
# Count the number of times the magnitude exceeds the threshold
count = np.sum(magnitude > self.threshold)
self.activity_counts.append(int(count))
def get_activity_counts(self):
return self.activity_counts