Course / Tutorials / Week 6 / Q-18
Quick tutorial Q-18 · Week 6 · about 15 min · Foundation
Count steps from an accelerometer
Band-pass a chest accelerometer, count footstrikes with a minimum gap between peaks, turn them into a cadence, and check it against the spectrum and the known answer of a synthetic record.
- 1Gather (used)
- 2Statistics
- 3Visualize (used)
- 4Cleanse
- 5Features (used)
- 6Balance
- 7Scale
- 8Split
- 9Deploy
- Ask
- Collect (this week)
- Check (this week)
- Interpret (this week)
- Coach
- Reassess
Download notebook ·Python file ·Read as Markdown ·Data folder ·Chapter 6 (sections 6.4)
Every cell runs in this page; the first run downloads Python (about 30 s). Nothing you load leaves your browser. Helpful first: Q-13 Find the beats in ten seconds of ECG.
The decision
A runner wants a cadence from a chest sensor. Before trusting a step count, check the method on a record whose answer is known.
The raw data
raw/chest-acc-synthetic.csv: SYNTHETIC chest accelerometer, 50 samples a second, in g (t_s, vert_g along the body, fwd_g out of the chest); 30 s of running at 172 steps a minute, then lying still. Made bybook/ch06-gps-motion/code/make_chest.pyfor Chapter 6 of Data-Driven Athletics; the same file asbook/ch06-gps-motion/data/chest-acc.csv.
Try it
Each footstrike sends a jolt up the body, and an accelerometer on the chest sees it as a spike on the axis along the body. Find the spikes and you have the steps. The file is the synthetic chest record of Chapter 6 (Section 6.4): it was made by a script with a known answer, 30 seconds of running at 172 steps a minute with a little natural variation from step to step, then lying still. Because the answer is known, you can check your count exactly.
Open the record
Type a prediction to enable Run.
Show the expected result without running
4750 samples at 50 samples/s; running part 1500 samples vertical axis while running: mean 1.17 g, from 0.15 to 2.89 g
The mean is 1.17 g, not 0: standing or running, the sensor feels 1 g of gravity as well as motion (Section 6.4), and the footstrike spikes push it up from there, to almost 3 g.
Filter, then find peaks with a minimum gap
The recipe is the one used for R peaks in Q-13. A band-pass from 0.5 to 5 Hz keeps the step rhythm and drops gravity and sensor noise; find_peaks then refuses any peak closer than 0.25 s to the last one, because no runner takes more than 240 steps a minute.
Show the expected result without running
86 steps; cadence 171.9 steps/min
The script that made the file put exactly 86 steps in the first 30 seconds, at 171.9 steps a minute, so both numbers are right. The cadence comes from the time between the first and last step found, not from the count divided by 30 s: a step at the very edge of the window cannot be found, and that would bias the count but not the spacing.
How much does the minimum gap matter?
Type a prediction to enable Run.
Show the expected result without running
minimum gap 0.00 s: 86 steps minimum gap 0.25 s: 86 steps minimum gap 0.50 s: 43 steps spectrum peak 2.87 Hz = 172 steps/min

A minimum gap of 0.5 s is longer than one step (0.35 s at 172 steps a minute), so the detector skips every other step and counts 43. With no minimum gap the count here barely changes, because the band-pass already smoothed each spike into one hump; on a real, noisier recording the small wiggles would count. The spectrum gives a second, independent check: one tall peak at the step frequency. The smaller peak at twice that frequency is a harmonic: the spikes are sharper than a sine wave.
For the athlete: "Your chest sensor counted your steps as well as a stopwatch could: about 172 a minute, and two different methods agree."
Action card
- Finding
- The detector finds all 86 steps; cadence 171.9 steps a minute.
- Evidence
- Peak counting and the spectrum (2.87 Hz, 172 steps a minute) agree with the record's known 86 steps at 171.9.
- How sure
- High on this synthetic record; a real recording needs the same check on a counted stretch.
- Action
- Use a minimum gap of about 0.25 s for running, and report cadence from the spacing of the steps.
- Test again
- Count 15 seconds of steps by ear on your own recording and compare.
With your own data
Strap a phone to your chest with an elastic band, log its accelerometer with a sensor-logging app while you run for 30 seconds, and use the axis along your body in place of vert_g. Count your steps by ear for the same 30 seconds to have a known answer.
Check yourself
Why report cadence from the first-to-last step spacing rather than the count divided by the window length?
At 172 steps a minute, how long is one step, in seconds?
References
- Data-Driven Athletics, Chapter 6, Motion Sensing (Section 6.4): from acceleration to steps, steps and breaths from the chest.
Built 2026-11-30 with Python packages numpy 2.5.5, pandas 3.1.1, matplotlib 3.11.2, scipy 1.18.1. In the browser the same code runs on Pyodide, so the last digit can differ.