[技术问答] 一阶低通滤波的C语言实现

[复制链接]
 楼主| jonas222 发表于 2025-3-31 11:23 | 显示全部楼层 |阅读模式

  1. #include <stdio.h>
  2. #include <string.h>

  3. /*************************** 模拟输入数据 ********************************/
  4. float input_data[251] = {
  5.     0.00, 0.86, 1.59, 2.10, 2.35, 2.35, 2.18, 1.94, 1.76, 1.73, 1.90, 2.26, 2.75,
  6.     3.24, 3.63, 3.80, 3.70, 3.30, 2.68, 1.93, 1.18, 0.54, 0.11, -0.10, -0.11,
  7.     0.00, 0.11, 0.10, -0.11, -0.54, -1.18, -1.93, -2.68, -3.30, -3.70, -3.80,
  8.     -3.63, -3.24, -2.75, -2.26, -1.90, -1.73, -1.76, -1.94, -2.18, -2.35, -2.35,
  9.     -2.10, -1.59, -0.86, 0.00, 0.86, 1.59, 2.10, 2.35, 2.35, 2.18, 1.94, 1.76,
  10.     1.73, 1.90, 2.26, 2.75, 3.24, 3.63, 3.80, 3.70, 3.30, 2.68, 1.93, 1.18,
  11.     0.54, 0.11, -0.10, -0.11, 0.00, 0.11, 0.10, -0.11, -0.54, -1.18, -1.93,
  12.     -2.68, -3.30, -3.70, -3.80, -3.63, -3.24, -2.75, -2.26, -1.90, -1.73,
  13.     -1.76, -1.94, -2.18, -2.35, -2.35, -2.10, -1.59, -0.86, 0.00, 0.86, 1.59,
  14.     2.10, 2.35, 2.35, 2.18, 1.94, 1.76, 1.73, 1.90, 2.26, 2.75, 3.24, 3.63,
  15.     3.80, 3.70, 3.30, 2.68, 1.93, 1.18, 0.54, 0.11, -0.10, -0.11, 0.00, 0.11,
  16.     0.10, -0.11, -0.54, -1.18, -1.93, -2.68, -3.30, -3.70, -3.80, -3.63, -3.24,
  17.     -2.75, -2.26, -1.90, -1.73, -1.76, -1.94, -2.18, -2.35, -2.35, -2.10, -1.59,
  18.     -0.86, 0.00, 0.86, 1.59, 2.10, 2.35, 2.35, 2.18, 1.94, 1.76, 1.73, 1.90, 2.26,
  19.     2.75, 3.24, 3.63, 3.80, 3.70, 3.30, 2.68, 1.93, 1.18, 0.54, 0.11, -0.10, -0.11,
  20.     0.00, 0.11, 0.10, -0.11, -0.54, -1.18, -1.93, -2.68, -3.30, -3.70, -3.80, -3.63,
  21.     -3.24, -2.75, -2.26, -1.90, -1.73, -1.76, -1.94, -2.18, -2.35, -2.35, -2.10, -1.59,
  22.     -0.86, 0.00, 0.86, 1.59, 2.10, 2.35, 2.35, 2.18, 1.94, 1.76, 1.73, 1.90, 2.26, 2.75,
  23.     3.24, 3.63, 3.80, 3.70, 3.30, 2.68, 1.93, 1.18, 0.54, 0.11, -0.10, -0.11, 0.00, 0.11,
  24.     0.10, -0.11, -0.54, -1.18, -1.93, -2.68, -3.30, -3.70, -3.80, -3.63, -3.24, -2.75,
  25.     -2.26, -1.90, -1.73, -1.76, -1.94, -2.18, -2.35, -2.35, -2.10, -1.59, -0.86, 0.00};

  26. float get_data(void)
  27. {
  28.   static int i = 0;
  29.   return (input_data[i++]);
  30.   if (i == 251) //轮回
  31.     i = 0;
  32. }

  33. float fc = 2.0f;     //截止频率
  34. float Ts = 0.02f;    //采样周期
  35. float pi = 3.14159f; //π
  36. float alpha = 0;     //滤波系数

  37. /************************ 滤波器初始化 alpha *****************************/
  38. void low_pass_filter_init(void)
  39. {
  40.   float b = 2.0 * pi * fc * Ts;
  41.   alpha = b / (b + 1);
  42. }

  43. float low_pass_filter(float value)
  44. {
  45.   static float out_last = 0; //上一次滤波值
  46.   float out;

  47.   /***************** 如果第一次进入,则给 out_last 赋值 ******************/
  48.   static char fisrt_flag = 1;
  49.   if (fisrt_flag == 1)
  50.   {
  51.     fisrt_flag = 0;
  52.     out_last = value;
  53.   }

  54.   /*************************** 一阶滤波 *********************************/
  55.   out = out_last + alpha * (value - out_last);
  56.   out_last = out;

  57.   return out;
  58. }

  59. void main(void)
  60. {
  61.   float result[251];

  62.   low_pass_filter_init();
  63.   for (int i = 0; i < 251; i++)
  64.   {
  65.     result[i] = low_pass_filter(get_data());
  66.     printf("%f,", result[i]);
  67.   }
  68. }


600967e94d37bab71.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

44

主题

1654

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部