Step detection and IMU odometry

Phát hiện bước chân và IMU

Every smartphone carries an inertial measurement unit — a cluster of tiny MEMS chips that report how the device accelerates and rotates. The accelerometer measures specific force in meters per second squared; the gyroscope measures angular rate in radians per second. Together they form what engineers call an IMU. The tempting first project in any sensor course is to integrate those signals into position: double-integrate acceleration for displacement, integrate gyroscope for heading, and draw a trail on a map. Within thirty seconds the trail has wandered into the next building. Consumer MEMS biases sit at milligram scale, and integration squares time — small errors become large distances very quickly.

Wheria does not try to be a general-purpose inertial navigator. Underground, where GPS horizontal error often exceeds thirty meters, we use pedestrian dead reckoning: a method that treats each footfall as a discrete event rather than a continuous integral. Detect a step, multiply by an estimated stride length, rotate by current heading, and advance a point on a local map. Error still accumulates, but it grows roughly with the square root of step count instead of exploding exponentially with elapsed time. That difference is what makes a four-minute garage walk usable instead of absurd.

What a step looks like to an accelerometer

When you walk at a normal pace of about 1.2 to 1.5 meters per second, your body produces a rhythmic vertical bounce. That bounce repeats at a cadence of roughly 1.5 to 3 hertz — one complete up-down cycle every third to two-thirds of a second. The accelerometer magnitude, computed as the square root of the sum of squared axis values, shows clear peaks in that band even when the phone sits in a pocket at an odd angle.

Our step detector band-passes that magnitude signal around the walking band, then hunts for peaks. The threshold is not fixed: over a sliding two-second window we compute the local mean and standard deviation, then declare a step when a peak exceeds mean plus k times sigma, where k ranges from about 1.2 to 1.8 depending on whether the phone is in a trouser pocket or held in hand. A minimum interval of 280 milliseconds between accepted steps filters out shuffling false positives. Between steps, the gyroscope z-axis — rotation about the vertical axis when the phone is upright — helps confirm that the user actually turned rather than merely jostled the device.

iOS and Android deliver IMU samples at different native rates, anywhere from fifty to two hundred hertz depending on device and power mode. Before the detector runs, shared logic resamples both platforms to a fixed one-hundred hertz so the same peak-picking code produces the same step count on an iPhone and a Pixel given identical motion.

Stride length is the hidden variable

A detected step tells you that the user moved, but not how far. That distance comes from stride length — the horizontal span of one footfall. Wheria starts with a default of about 0.72 meters, which matches average adult walking on flat ground. If the user enters height in settings, we apply a common anthropometric estimate: stride approximately equals 0.415 times height in meters. For users who want tighter accuracy, an optional calibration walk asks them to traverse a known distance — twenty meters in a straight corridor works well — and we solve stride as distance divided by step count.

Some research papers tie stride to step frequency through power-law models such as Weinberg's relation, where stride scales with the fourth root of cadence. We expose a modest version of that tie-in, but it is not magic: a five percent stride error on an eighty-meter walk with a hundred and ten steps produces four meters of longitudinal error before heading is even considered. That is why Wheria shows distance bands instead of fake precision, and why we encourage calibration for users who regularly park in large structures.

Building an honest error budget

Suppose you walk eighty meters through a spiral ramp with a hundred and ten detected steps. Stride is wrong by five percent: you are four meters off along the path. Heading at the end is wrong by ten degrees: lateral error is eighty times sine of ten degrees, roughly fourteen meters in the worst geometry. These numbers are not pessimistic fiction — they are what ktuyen's garage test matrix produces when compass trust is low and the user skipped calibration. The UI responds with a fading polyline whose older segments dim, a confidence chip derived from fusion covariance, and language like "about 45 m" rather than "44.7 m." Each new parked event resets the odometry state so errors stay bounded per trip instead of compounding across weeks of history.

PDR has hard limits we do not hide. Escalators increment step count without equivalent horizontal travel. Moving walkways decouple footfalls from ground speed. Running breaks the cadence band the detector expects. A phone clamped to a shopping cart handle produces an acceleration profile that looks nothing like walking. When accelerometer variance spikes in ways that do not match normal gait, Wheria flags unusual motion rather than silently drawing a path that pretends the user walked.

Step detection is only one layer of the indoor stack. Heading comes from gyroscope and magnetometer fusion described in our compass post; floor hints from the barometer in barometer math; and the full pipeline in indoor navigation. The Kalman filter in Kalman filters for parking ties those measurements together so a single step update does not overconfidence the map. Phyzix exposes raw accelerometer and gyroscope traces if you want to see the peaks yourself before trusting an app to count them.

Mỗi smartphone mang theo một bộ đo quán tính — cụm chip MEMS nhỏ báo cáo thiết bị gia tốc và xoay thế nào. Gia tốc kế đo lực riêng, tính bằng mét trên giây bình phương; con quay đo tốc độ góc, tính bằng radian trên giây. Hai cảm biến ghép lại thành IMU. Dự án đầu tiên ai học cảm biến cũng muốn thử: tích phân tín hiệu thành vị trí — tích phân gia tốc hai lần cho quãng đường, tích con quay cho hướng — rồi vẽ đường trên bản đồ. Chưa đầy ba mươi giây, đường đã trôi sang tòa nhà bên cạnh. Bias MEMS trên điện thoại consumer cỡ milligram, và tích phân làm lỗi bình phương theo thời gian — sai số nhỏ thành khoảng cách lớn rất nhanh.

Wheria không cố làm bộ dẫn đường quán tính đa năng. Dưới hầm, khi sai số ngang GPS thường vượt ba mươi mét, app dùng dead reckoning cho người đi bộ: coi mỗi bước chân là một sự kiện rời rạc thay vì tích phân liên tục. Phát hiện một bước, nhân với độ dài sải chân ước lượng, xoay theo heading hiện tại, tiến một điểm trên bản đồ local. Lỗi vẫn tích lũy, nhưng tăng xấp xỉ theo căn bậc hai của số bước thay vì bùng theo hàm mũ của thời gian. Khác biệt đó là lý do một lượt đi hầm bốn phút dùng được thay vì vô lý.

Bước chân trông thế nào với gia tốc kế

Khi bạn đi bộ bình thường khoảng 1,2 đến 1,5 mét mỗi giây, cơ thể tạo nhịp nảy dọc đều. Nhịp đó lặp ở cadence khoảng 1,5 đến 3 hertz — một chu kỳ lên xuống mỗi hai phần ba đến một phần ba giây. Biên độ gia tốc, tính bằng căn bậc hai tổng bình phương ba trục, vẫn có đỉnh rõ trong dải đó dù điện thoại nằm lệch trong túi quần.

Bộ phát hiện bước của Wheria lọc dải biên độ quanh band đi bộ, rồi tìm đỉnh. Ngưỡng không cố định: trong cửa sổ trượt hai giây, app tính trung bình và độ lệch chuẩn cục bộ, rồi công nhận bước khi đỉnh vượt trung bình cộng k nhân sigma, với k từ khoảng 1,2 đến 1,8 tùy điện thoại trong túi hay cầm tay. Khoảng cách tối thiểu 280 mili giây giữa hai bước lọc bước lê lóng. Giữa các bước, trục z của con quay — xoay quanh phương thẳng đứng khi cầm máy đứng — giúp xác nhận người dùng thật sự quẹo chứ không chỉ rung máy.

iOS và Android cung cấp mẫu IMU ở tốc độ gốc khác nhau, từ năm mươi đến hai trăm hertz tùy máy và chế độ pin. Trước khi detector chạy, logic dùng chung resample cả hai nền tảng về một trăm hertz cố định để cùng một thuật toán đếm bước cho kết quả giống nhau trên iPhone và Pixel nếu chuyển động giống hệt.

Độ dài sải chân là biến ẩn

Phát hiện bước chỉ biết người dùng đã di chuyển, không biết bao xa. Quãng đó đến từ sải chân — độ dài ngang của một bước. Wheria bắt đầu với mặc định khoảng 0,72 mét, phù hợp người trưởng thành đi phẳng. Nếu nhập chiều cao, app dùng ước lượng nhân trắc phổ biến: sải chân xấp xỉ 0,415 nhân chiều cao mét. Người muốn chính xác hơn có thể calibrate: đi một quãng biết trước — hai mươi mét hành lang thẳng là ổn — rồi app tính sải bằng quãng đường chia số bước.

Một số bài báo gắn sải chân với tần số bước qua mô hình lũy thừa như quan hệ Weinberg, trong đó sải tỉ lệ với căn bậc bốn của cadence. Wheria có phiên bản nhẹ của liên kết đó, nhưng không phải phép màu: sai sải năm phần trăm trên quãng tám mươi mét với một trăm mười bước tạo lỗi dọc bốn mét trước khi tính heading. Vì vậy app hiện dải khoảng cách thay vì số giả chính xác, và khuyến khích calibrate với người hay đỗ hầm lớn.

Ngân sách lỗi thật thà

Giả sử bạn đi tám mươi mét qua ramp xoắn với một trăm mười bước. Sải sai năm phần trăm: lệch bốn mét dọc đường. Heading cuối sai mười độ: lỗi ngang là tám mươi nhân sin mười độ, xấp xỉ mười bốn mét trong hình học xấu nhất. Đây không phải số bi quan giả tạo — ma trận test hầm của ktuyen cho ra vậy khi la bàn kém tin và user bỏ qua calibrate. Giao diện đáp lại bằng polyline phai dần, chip confidence từ covariance fusion, và câu kiểu "khoảng 45 m" thay vì "44,7 m." Mỗi lần đỗ mới reset odometry để lỗi giới hạn trong một chuyến thay vì cộng dồn qua nhiều tuần.

PDR có giới hạn cứng mà team không che. Thang cuốn tăng số bước không tương ứng quãng ngang. Băng chuyền tách bước chân khỏi tốc độ mặt đất. Chạy phá band cadence detector mong đợi. Điện thoại gắn tay cầm xe đẩy cho profile gia tốc không giống đi bộ. Khi phương sai gia tốc tăng theo cách không khớp dáng đi bình thường, Wheria báo chuyển động bất thường thay vì im lặng vẽ đường giả vờ bạn đã đi.

Phát hiện bước chỉ là một lớp của stack indoor. Heading đến từ fusion con quay và từ kế trong bài la bàn; gợi ý tầng từ áp kế trong toán áp kế; pipeline đầy đủ trong dẫn đường indoor. Bộ lọc Kalman ở Kalman cho đỗ xe buộc các đo lường lại để một bước không làm bản đồ quá tự tin. Phyzix mở trace gia tốc và con quay thô nếu bạn muốn tự thấy đỉnh trước khi tin app đếm bước.