Daily Tech Briefing
AI 科技速览
每天 5 分钟内学习 AI。获取最新的人工智能新闻,理解其重要性,并学习如何将其应用于您的工作。
Hacker News · 2026/7/28 06:11:18

Recursive Filters: SMA, EMA, Low‑Pass, and a Tiny Kalman
AI 中文解读
这篇技术新闻的核心亮点是:为资源受限的设备提供了一套轻量级、高效的数据平滑算法,让机器在嘈杂的实时数据中快速滤除噪声。
通俗来说,当传感器或测量数据因环境干扰而上下跳动时,需要一种手段让数值变得平滑稳定。文章介绍了四种“数据平滑器”:简单移动平均(SMA)像取最近几个数的平均值,但需要缓存旧数据;指数移动平均(EMA)只需记住上一个结果,用新数据稍作调整,计算极快;低通滤波本质与EMA相同,只是从信号处理角度命名;而迷你一维卡尔曼滤波器更聪明——它能根据你对“传感器误差”和“预测误差”的估计,动态调整平滑力度,实现更精准的追踪。所有这些方法都只用极少的计算和内存,适合在单片机、手机传感器或自动驾驶控制回路里实时运行。
这些技术看似高深,实则已融入日常生活。比如智能手环的心率监测,需要用滤波器剔除运动带来的抖动;手机导航中的方向指示,依靠类似算法保持稳定不晃;甚至空调的恒温控制,也是通过递归滤波让温度读数平稳,避免频繁开关。它们让设备反应更快、功耗更低,同时输出更可靠的数据,最终提升用户使用体验。
18 October 2025 / 4 min read Recursive Filters: SMA, EMA, Low‑Pass, and a Tiny Kalman filters , signal-processing , math While exploring optimizers I fell down the rabbit hole of recursive filters. This post is a compact, practical tour of smoothers you can use when measurements are noisy but latency and compute are tight. We’ll keep the math minimal, the intuition high, and focus on when and why to use SMA, EMA/low‑pass, and a tiny 1D Kalman.
If you are here just for code, my Kaggle Notebook can be found here.
Why recursive filters
Recursive filters shine when compute and memory are scarce, or when data arrives as a stream and you must react immediately.
O(1) memory: you keep just the last state (and maybe a running sum) rather than a long buffer.
O(1) compute per sample: one or two multiply‑adds per step—perfect for microcontrollers and tight loops.
Online, low‑latency: produce an updated estimate as soon as a sample arrives; no need to wait for a full window.
Simple, robust implementations: easy to port, vectorize, or run in reduced precision.
Graceful with irregular sampling: updates are incremental and don’t assume batch availability.
Intuition note
Recursive filters that we’ll use, all use constant‑size state. That’s why they’re common in embedded systems, robotics control loops, mobile sensor smoothing and telemetry.
Notation
xtx_txt: the raw observation at time step ttt.
sts_tst: the smoothed estimate at time ttt.
kkk: window size (for moving averages).
α∈[0,1]\alpha\in[0,1]α∈[0,1]: smoothing factor (higher α\alphaα = smoother, but laggier).
Recursive Average Filter (also known as EMA)
The one‑liner you’ll use most:
st=α st−1+(1−α) xt.s_t = \alpha\, s_{t-1} + (1-\alpha)\, x_t.st=αst−1+(1−α)xt.
Many libraries choose α\alphaα directly; our example consists of kkk data points and we define alpha as α=k−1k\alpha = \tfrac{k-1}{k}α=kk−1 to make notation easier to hold in our memory.
Intuition note
This behaves like a leaky integrator. Bigger α\alphaα forgets the past more slowly (smoother, more lag). The initial line takes a few iterations before it can get closer to the real mean value.
Simple Moving Average (SMA)
Windowed average over the last kkk samples:
st=1k∑i=0k−1xt−i.s_t = \frac{1}{k} \sum_{i=0}^{k-1} x_{t-i}.st=k1i=0∑k−1xt−i.
Intuition note
Great at crushing noise, but it lags and needs a buffer of the last kkk points. Spikes are “diluted” equally across the window.
First‑Order Low‑Pass Filter
In discrete time, the classic low‑pass is algebraically the same as the EMA:
yt=α yt−1+(1−α) xt.y_t = \alpha\, y_{t-1} + (1-\alpha)\, x_t.yt=αyt−1+(1−α)xt.
Different name, same form. You pick α\alphaα to trade off noise suppression versus responsiveness.
A Tiny 1D Kalman Update
This is just scratching the surface of a Kalman Filter - a tiny 1D Kalman Update. This is my first attempt at building intuition before I can jump to an actual KF.
When you know your sensor noise (RRR) and process noise (QQQ), Kalman gives you an adaptive gain that automatically balances trust between the prediction and the measurement. For a constant‑value model (A=H=1A=H=1A=H=1):
Predict:
x^t−=x^t−1,Pt−=Pt−1+Q.\hat{x}_t^{-} = \hat{x}_{t-1},\quad P_t^{-} = P_{t-1} + Q.x^t−=x^t−1,Pt−=Pt−1+Q.
Update:
Kt=Pt−Pt−+R,x^t=x^t−+Kt (zt−x^t−),Pt=(1−Kt)Pt−.K_t = \frac{P_t^{-}}{P_t^{-} + R},\quad
\hat{x}_t = \hat{x}_t^{-} + K_t\,(z_t - \hat{x}_t^{-}),\quad
P_t = (1 - K_t) P_t^{-}.Kt=Pt−+RPt−,x^t=x^t−+Kt(zt−x^t−),Pt=(1−Kt)Pt−.
Intuition note
If measurements are noisy (large RRR), KtK_tKt shrinks and you trust the prior more. If the process is volatile (large QQQ), Pt−P_t^{-}Pt− grows and KtK_tKt increases—trust the new measurement more.
Choosing parameters
SMA window kkk: larger kkk = smoother but laggier. Needs a buffer.
EMA/low‑pass α\alphaα: start around 0.80.80.8–0.950.950.95. Tune by eyeballing lag vs. noise.
Kalman (Q,R)(Q, R)(Q,R): set RRR to your sensor variance; set QQQ to how mu
分享
阅读原文 ↗