This shows you the differences between two versions of the page.
|
iothings:laboratoare:2025:lab7 [2025/11/06 18:26] dan.tudose [On-Device Audio Anomaly Detection] |
iothings:laboratoare:2025:lab7 [2025/11/08 19:01] (current) dan.tudose [K-Means: IMU Activity Clustering] |
||
|---|---|---|---|
| Line 59: | Line 59: | ||
| ===== K-Means: IMU Activity Clustering ===== | ===== K-Means: IMU Activity Clustering ===== | ||
| - | K-means clustering is an unsupervised learning technique that groups data points into clusters based on how similar they are to one another. Unlike supervised methods that rely on labeled examples, K-means works with unlabeled data to reveal underlying patterns or structure. For instance, an wearable device might use K-means to discern between different activities, such as sleeping, walkinr or running. | + | K-means clustering is an unsupervised learning technique that groups data points into clusters based on how similar they are to one another. Unlike supervised methods that rely on labeled examples, K-means works with unlabeled data to reveal underlying patterns or structure. For instance, an wearable device might use K-means to discern between different activities, such as sleeping, walking or running. |
| + | |||
| + | {{ :iothings:laboratoare:2025:har.jpg?600 |}} | ||
| <note tip>You can learn more about K-means Clustering [[https://www.geeksforgeeks.org/machine-learning/k-means-clustering-introduction/|here]]!</note> | <note tip>You can learn more about K-means Clustering [[https://www.geeksforgeeks.org/machine-learning/k-means-clustering-introduction/|here]]!</note> | ||
| Line 121: | Line 123: | ||
| ===== On-Device Light Anomaly Detection ===== | ===== On-Device Light Anomaly Detection ===== | ||
| + | |||
| + | This example turns an ESP32-C6 “Sparrow” into a light-state recognizer. | ||
| + | |||
| + | Each loop tick runs every 100 ms to achieve 10 Hz sampling. The raw brightness is compressed to a [0,1] scale with a log normalization that treats 64k as a generous upper bound, then smoothed with an exponential moving average using α=0.3. The code also maintains a short ring buffer covering roughly three seconds of these smoothed values to estimate a windowed standard deviation, which acts as a quick measure of short-term variability. | ||
| + | |||
| + | Unsupervised clustering is done online with a very small, one-dimensional k-means-like scheme. Five cluster centroids are seeded across the [0,1] range, and for about forty seconds (TRAIN_SAMPLES=400 at 10 Hz) the system is in a training phase: each new smoothed sample is assigned to its nearest cluster by absolute distance and that cluster’s running mean and variance are updated. During training it prints progress and the evolving means. After training, each new sample is again assigned to its nearest cluster to produce a cluster index, along with the cluster’s standard deviation and the short-window deviation from the ring buffer. A human-readable room state is then chosen. If the user has previously assigned a manual label to that cluster via a tiny serial REPL, that label is used; otherwise a heuristic converts normalized level and short-term variability into categories like “night”, “full_sun”, “lights_on”, “shade/day_indirect”, or “transition”. To avoid label flicker, a simple hysteresis keeps a new label “pending” until it appears twice in a row. The cluster means continue to adapt slowly during inference so the model can track gradual daylight changes. | ||
| + | |||
| + | Labels can be managed over Serial with commands such as setlabel <k> <name>, savelabels, and labels. The Preferences API persists these names in NVS under a “labels” namespace so they survive reboots. Throughout, the program reports the sensor type, the raw brightness, the normalized and EMA values, the chosen cluster and its statistics, the short-window deviation, and the stable label, making it easy to tune thresholds or swap sensors without changing application logic. | ||
| + | |||
| + | Download the code [[iothings:laboratoare:2025_code:lab7_3|here]]. | ||
| Add these two lines to your ''lib_deps'' in ''platformio.ini'': | Add these two lines to your ''lib_deps'' in ''platformio.ini'': | ||