← Back to the chapter · Next detail →
Technical detail · Edge Acquisition: Power Budgets and Pipelines
Implement the IMU aggregation window
The class buffers each axis, processes a complete window, and removes only the samples it has consumed.
import math
import time
class IMUEdgeAggregator:
"""Aggregate high-frequency IMU data into 1 Hz statistical summaries.
Reduces 600 samples/second (100 Hz x 6 axes) to 9 summary values
per second, cutting transmission from 1200 bytes/s to 18 bytes/s
(67x reduction).
"""
def __init__(self, sample_rate_hz=100, window_sec=1):
self.sample_rate = sample_rate_hz
self.window_size = sample_rate_hz * window_sec
self.buffer_accel = {"x": [], "y": [], "z": []}
self.buffer_gyro = {"x": [], "y": [], "z": []}
def add_sample(self, ax, ay, az, gx, gy, gz):
"""Add one raw IMU sample (called at 100 Hz)."""
self.buffer_accel["x"].append(ax)
self.buffer_accel["y"].append(ay)
self.buffer_accel["z"].append(az)
self.buffer_gyro["x"].append(gx)
self.buffer_gyro["y"].append(gy)
self.buffer_gyro["z"].append(gz)
def _rms(self, values):
"""Root mean square -- captures vibration energy."""
if not values:
return 0.0
return math.sqrt(sum(v * v for v in values) / len(values))
def _peak(self, values):
"""Peak absolute value -- detects impacts."""
if not values:
return 0.0
return max(abs(v) for v in values)
def window_ready(self):
"""Check if enough samples collected for one summary."""
return len(self.buffer_accel["x"]) >= self.window_size
def compute_summary(self):
"""Compute 1 Hz summary from buffered samples.
Returns dict with RMS and peak for each axis -- enough
to detect vibration anomalies without raw data.
"""
summary = {"timestamp": int(time.time()), "samples": self.window_size}
for axis in ["x", "y", "z"]:
accel = self.buffer_accel[axis][:self.window_size]
gyro = self.buffer_gyro[axis][:self.window_size]
summary[f"accel_{axis}_rms"] = round(self._rms(accel), 4)
summary[f"accel_{axis}_peak"] = round(self._peak(accel), 4)
summary[f"gyro_{axis}_rms"] = round(self._rms(gyro), 2)
# Clear processed samples
for axis in ["x", "y", "z"]:
self.buffer_accel[axis] = self.buffer_accel[axis][self.window_size:]
self.buffer_gyro[axis] = self.buffer_gyro[axis][self.window_size:]
return summary
def estimate_bandwidth(self):
"""Compare raw vs aggregated data rates."""
raw_bytes_per_sec = self.sample_rate * 6 * 2 # 6 axes, 2 bytes each
summary_bytes = 9 * 2 # 9 summary values, 2 bytes each
ratio = raw_bytes_per_sec / summary_bytes
return {
"raw_bytes_per_sec": raw_bytes_per_sec,
"summary_bytes_per_sec": summary_bytes,
"reduction_ratio": f"{ratio:.0f}x",
}