首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >FUXA可用的滤波函数

FUXA可用的滤波函数

作者头像
科控物联
发布2026-03-19 13:39:06
发布2026-03-19 13:39:06
510
举报
代码语言:javascript
复制
/**
 * 创建一个带限幅的一阶低通滤波器(指数加权移动平均)
 * @param {number} alpha - 平滑系数 (0.0 ~ 1.0),越小滤波越强
 * @param {number} minValue - 输入/输出最小允许值(钳位下界)
 * @param {number} maxValue - 输入/输出最大允许值(钳位上界)
 * @param {number} [initialValue] - 初始滤波值;若不传,则首次调用时自动设为 clamp(newValue)
 * @returns {(newValue: number) => number} 滤波函数,接收新值,返回滤波后值
 */
function createLowPassFilter(alpha, minValue, maxValue, initialValue) {
  if (alpha < 0 || alpha > 1) throw new Error('alpha must be in [0, 1]');
  if (minValue >= maxValue) throw new Error('minValue must be < maxValue');

  let filtered = initialValue !== undefined 
    ? Math.max(minValue, Math.min(initialValue, maxValue)) 
    : undefined;

  return function(newValue) {
    if (typeof newValue !== 'number' || isNaN(newValue)) {
      throw new Error('newValue must be a valid number');
    }

    // 钳位输入
    const clamped = Math.max(minValue, Math.min(newValue, maxValue));

    // 首次调用:用钳位后的值初始化
    if (filtered === undefined) {
      filtered = clamped;
      return filtered;
    }

    // 指数滤波:y[n] = α·x[n] + (1−α)·y[n−1]
    filtered = alpha * clamped + (1 - alpha) * filtered;

    // 输出也做钳位(防御性设计,防止浮点累积误差越界)
    return Math.max(minValue, Math.min(filtered, maxValue));
  };
}

// ✅ 使用示例(直接复制即可运行)
const filter = createLowPassFilter(0.1, 0, 500); // alpha=0.1, range [0,500]

console.log(filter(100)); // → 100   (首次调用,初始化)
console.log(filter(450)); // → 135   (0.1×450 + 0.9×100 = 135)
console.log(filter(520)); // → 173.5 (0.1×500(已钳位) + 0.9×135 ≈ 173.5)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2026-01-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 科控物联 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档