Collecting sensor data
A TinyML model is only as good as its data. Learn how accelerometers and microphones produce data, how to label it, and how to cut it into windows.
In 60 seconds
Collecting sensor data
A TinyML model is only as good as its data. Learn how accelerometers and microphones produce data, how to label it, and how to cut it into windows.
Two common sensors
| Sensor | Typical data | Good for |
|---|---|---|
| Accelerometer | 3 numbers (x, y, z), tens to hundreds of times a second | Gestures, activity (still, walking, running), falls, machine vibration |
| Gyroscope | 3 numbers for rotation speed | Turning and twisting gestures, often used with an accelerometer |
| Microphone | Thousands of samples a second | Wake words, sounds such as coughs, alarms or machine noise |
| Small camera | Low-resolution images | Person present or not, simple objects |
Sampling rate and windows
import numpy as np
import pandas as pd
RATE = 50 # samples per second
SECONDS = 2
t = np.arange(RATE * SECONDS) / RATE
rng = np.random.default_rng(0)
# 2 seconds of made-up "walking" on the z axis: gravity + a step rhythm + noise
z = 1.0 + 0.3 * np.sin(2 * np.pi * 1.8 * t) + rng.normal(0, 0.02, len(t))
df = pd.DataFrame({"time_s": t, "z_g": z.round(3), "label": "walking"})
print(len(df), "samples in", SECONDS, "seconds")
print(df.head(6))Labelling
- 1
Plan the classes
Decide the few classes you need. Always include "none of these" or "background". - 2
Record in real conditions
Use the same board, the same sensor placement and the places where it will really be used. - 3
Label while recording
Press a button or say the label, so you do not have to guess later. - 4
Vary people and places
Different people, speeds, rooms, noise levels and ways of holding the device. - 5
Keep some data aside
Hold back recordings from some people or days for testing. Never train on them.
- All "walking" recorded by one person on one day
- Only quiet room recordings for a wake word used in a busy kitchen
- Test windows cut from the same recording as training windows
- No "background" class, so every sound must be a command
- Many people, several days, different shoes and floors
- Recordings with fans, TV, traffic and pressure cooker whistles
- Test data from different people or sessions than training
- A large, varied "background" or "unknown" class
Watch and read more
Lab
A working windowing function that turns a labelled accelerometer recording into training examples.
The problem
The starter makes a made-up 23-second accelerometer recording (still, walking, shaking, still) at 50 samples per second, with one label per sample. It also draws the signal.
- Write
make_windows(signal, labels, size, step). Start a window at 0,step, 2 ×stepand so on, while a full window still fits. Each window issignal[start:start+size]. - Keep a window only if all its labels are the same, and give it that label. Drop windows that mix two activities.
- Return
Xwith shape (windows, size, 3) andywith one label per window. - Run it with size 50 and step 25. Then try step 50 (no overlap). How many windows do you get now?
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1Why drop windows that mix two labels, instead of giving them the majority label?Reveal
A window that is half "still" and half "walking" is not a clean example of either. Training on it with one label teaches the model a blurry boundary. Dropping it is simple and safe. Some projects keep such windows with the majority label, or add a "transition" class, but you should decide that on purpose.
Q2Your gesture model works for you but fails for your friend. Name two likely data problems.Reveal
First, all the training data came from one person, so the model learned your speed and style, not the gesture in general. Second, the device may have been held or worn in a different position, which changes which axis sees gravity and movement. Collect from more people and placements, and test on people who are not in the training data.
Questions people ask
How much data do I need?
There is no fixed number. Start with enough to cover the variety you expect: several people, places and ways of doing each action. Train, test on held-out data, and look at the mistakes. If a class keeps failing, collect more of exactly those cases.
Can I use data from the internet?
Public datasets are useful for practice and for some sounds. But your sensor, board and placement matter a lot. A model trained on phone data may do poorly on a band worn on the wrist. Always add data from your own device, and check each dataset's licence.
Why do windows overlap?
A gesture or a word does not know where your windows start. With overlap, an event that is cut in half by one window is likely to be whole in the next. It also gives more training examples from the same recording.
Can I make fake data?
Synthetic data is great for learning and testing code, as in the labs here. It can also help add variety, such as mixing background noise into clean recordings. But a model must be tested on real recordings before you trust it.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.