1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. # 生成模拟传感器数据(示例数据)
    4. sensor_data = np.random.randn(200) # 正态分布随机数据
    5. # 定义低通滤波函数
    6. def low_pass_filter(data, cutoff_freq):
    7. filtered_data = np.copy(data)
    8. for i in range(1, len(data)):
    9. filtered_data[i] = (1 - cutoff_freq) * filtered_data[i - 1] + cutoff_freq * data[i]
    10. return filtered_data
    11. # 设置截止频率
    12. cutoff_frequency = 0.2
    13. # 应用低通滤波
    14. filtered_sensor_data = low_pass_filter(sensor_data, cutoff_frequency)
    15. # 绘制原始数据和滤波后数据
    16. plt.figure(figsize=(10, 6))
    17. plt.plot(sensor_data)
    18. plt.plot(filtered_sensor_data)
    19. plt.show()

    image.png